File: index.js

package info (click to toggle)
node-detect-file 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112 kB
  • ctags: 13
  • sloc: makefile: 4; sh: 2
file content (110 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
 * detect-file (https://github.com/doowb/detect-file)
 *
 * Copyright (c) 2016, Brian Woodward.
 * Licensed under the MIT License.
 */

'use strict';

var fs = require('fs');
var path = require('path');
var exists = require('fs-exists-sync');

/**
 * Resolve the given `filepath` if it exists.
 *
 * ```js
 * var res = detect('package.json');
 * console.log(res);
 * //=> "package.json"
 *
 * var res = detect('fake-file.json');
 * console.log(res)
 * //=> null
 * ```
 *
 * @param  {String} `filepath` filepath to detect.
 * @param  {Object} `options` Additional options.
 * @param  {Boolean} `options.nocase` Set this to `true` force case-insensitive filename checks. This is useful on case sensitive file systems.
 * @return {String} Returns the resolved filepath if it exists, otherwise returns `null`.
 * @api public
 */

module.exports = function detect(filepath, options) {
  if (!filepath || (typeof filepath !== 'string')) {
    return null;
  }
  if (exists(filepath)) {
    return path.resolve(filepath);
  }

  options = options || {};
  if (options.nocase === true) {
    return nocase(filepath);
  }
  return null;
};

/**
 * Check if the filepath exists by falling back to reading in the entire directory.
 * Returns the real filepath (for case sensitive file systems) if found.
 *
 * @param  {String} `filepath` filepath to check.
 * @return {String} Returns found filepath if exists, otherwise null.
 */

function nocase(filepath) {
  filepath = path.resolve(filepath);
  var res = tryReaddir(filepath);
  if (res === null) {
    return null;
  }

  // "filepath" is a directory, an error would be
  // thrown if it doesn't exist. if we're here, it exists
  if (res.path === filepath) {
    return res.path;
  }

  // "filepath" is not a directory
  // compare against upper case later
  // see https://nodejs.org/en/docs/guides/working-with-different-filesystems/
  var upper = filepath.toUpperCase();
  var len = res.files.length;
  var idx = -1;

  while (++idx < len) {
    var fp = path.resolve(res.path, res.files[idx]);
    if (filepath === fp || upper === fp) {
      return fp;
    }
    var fpUpper = fp.toUpperCase();
    if (filepath === fpUpper || upper === fpUpper) {
      return fp;
    }
  }

  return null;
}

/**
 * Try to read the filepath as a directory first, then fallback to the filepath's dirname.
 *
 * @param  {String} `filepath` path of the directory to read.
 * @return {Object} Object containing `path` and `files` if succesful. Otherwise, null.
 */

function tryReaddir(filepath) {
  var ctx = { path: filepath, files: [] };
  try {
    ctx.files = fs.readdirSync(filepath);
    return ctx;
  } catch (err) {}
  try {
    ctx.path = path.dirname(filepath);
    ctx.files = fs.readdirSync(ctx.path);
    return ctx;
  } catch (err) {}
  return null;
}