How to obtain case-exact path of a file in Node.js

2020-03-01 10:15发布

I have a path, let's say C:\temp\something.js and I want to get case-exact version of the path on Windows - so if there is C:\Temp\someThing.js stored on disk, I would like to get this value (path).

How can I get from the former path the later one in Node.js?

I have already gone through FS API (https://nodejs.org/api/fs.html) and I have not found anything useful (namely fs.realpathSync, fs.statSync, fs.accessSync did not return what I need).

2条回答
再贱就再见
2楼-- · 2020-03-01 10:40

I believe the only way is to loop over the files in the parent directory, and then find the file that will match. A sample implementation for case insensitive systems is provided below.

If the file system is case insensitive, then there cannot be two files in the same folder that share the same name in lowercase letter. The following implementation takes advantage of that.

/**
 * @param {string} filePath
 * @returns {string|undefined}
 */
function getRealPath(filePath) {
    /** @type {number} */
    var i;
    /** @type {string} */
    var dirname = path.dirname(filePath);
    /** @type {string} */
    var lowerFileName = path.basename(filePath).toLowerCase();
    /** @type {Array.<string>} */
    var fileNames = fs.readdirSync(dirname);

    for (i = 0; i < fileNames.length; i += 1) {
        if (fileNames[i].toLowerCase() === lowerFileName) {
            return path.join(dirname, fileNames[i]);
        }
    }
}

If your use case need to handle case sensitive file systems, I suggest to keep a list of potential matches, and then open the potential matches to check the content in order to determine the good one.

查看更多
家丑人穷心不美
3楼-- · 2020-03-01 11:03

Platforms with case-INsensitive filesystems (Windows, macOS) make it surprisingly hard to get the case-exact form of a given, possibly case-variant path - there seem to be no system APIs for it, so environments such as Node.js (or Python, Perl, ...) are not to blame.

Update: @barsh was nice enough to package up the code below for use with npm, so you can install it easily with
npm install true-case-path
.

The glob npm package with its nocase option comes to the rescue here (though it needed some tweaking on Windows); basically, treating the input path as a glob - even if it is a literal path - makes glob() return the true case as stored in the filesystem:

  • Install package glob in your project folder: npm install glob (add --save or --save-dev as needed).

  • Use the trueCasePathSync() function below; see the comments for usage and limitations; notably, while the input path is also normalized, paths starting with .. are not supported, because path.normalize() doesn't resolve them relative to the current dir.

    • NOTE: trueCasePathSync() does not return a canonical path: if you pass in a relative path, you'll get a relative output path as well, and no symlinks are resolved. If you want the canonical path, apply fs.realPathSync() to the result.
  • Should work on Windows, macOS, and Linux (though with limited usefulness on case-sensitive filesystems), tested with Node.js v4.1.1

    • NOTE: On Windows, no attempt is made to case-correct the drive letter or UNC-share component of the path (server name, share name).
/*
SYNOPSIS
  trueCasePathSync(<fileSystemPath>)
DESCRIPTION
  Given a possibly case-variant version of an existing filesystem path, returns
  the case-exact, normalized version as stored in the filesystem.
  Note: If the input path is a globbing *pattern* as defined by the 'glob' npm
        package (see prerequisites below), only the 1st match, if any,
        is returned.
        Only a literal input path guarantees an unambiguous result.
  If no matching path exists, undefined is returned.
  On case-SENSITIVE filesystems, a match will also be found, but if case
  variations of a given path exist, it is undefined which match is returned.
PLATFORMS
    Windows, OSX, and Linux (though note the limitations with case-insensitive
    filesystems).
LIMITATIONS
  - Paths starting with './' are acceptable, but paths starting with '../'
    are not - when in doubt, resolve with fs.realPathSync() first.
    An initial '.' and *interior* '..' instances are normalized, but a relative
    input path still results in a relative output path. If you want to ensure
    an absolute output path, apply fs.realPathSync() to the result.
  - On Windows, no attempt is made to case-correct the drive letter or UNC-share
    component of the path.
  - Unicode support:
    - Be sure to use UTF8 source-code files (with a BOM on Windows)
    - On OSX, the input path is automatically converted to NFD Unicode form
      to match how the filesystem stores names, but note that the result will
      invariably be NFD too (which makes no difference for ASCII-characters-only
      names).
PREREQUISITES
  npm install glob    # see https://www.npmjs.com/search?q=glob
EXAMPLES
  trueCasePathSync('/users/guest') // OSX: -> '/Users/Guest'
  trueCasePathSync('c:\\users\\all users') // Windows: -> 'c:\Users\All Users'
*/
function trueCasePathSync(fsPath) {

  var glob = require('glob')
  var path = require('path')

  // Normalize the path so as to resolve . and .. components.
  // !! As of Node v4.1.1, a path starting with ../ is NOT resolved relative
  // !! to the current dir, and glob.sync() below then fails.
  // !! When in doubt, resolve with fs.realPathSync() *beforehand*.
  var fsPathNormalized = path.normalize(fsPath)

  // OSX: HFS+ stores filenames in NFD (decomposed normal form) Unicode format,
  // so we must ensure that the input path is in that format first.
  if (process.platform === 'darwin') fsPathNormalized = fsPathNormalized.normalize('NFD')

  // !! Windows: Curiously, the drive component mustn't be part of a glob,
  // !! otherwise glob.sync() will invariably match nothing.
  // !! Thus, we remove the drive component and instead pass it in as the 'cwd' 
  // !! (working dir.) property below.
  var pathRoot = path.parse(fsPathNormalized).root
  var noDrivePath = fsPathNormalized.slice(Math.max(pathRoot.length - 1, 0))

  // Perform case-insensitive globbing (on Windows, relative to the drive / 
  // network share) and return the 1st match, if any.
  // Fortunately, glob() with nocase case-corrects the input even if it is 
  // a *literal* path.
  return glob.sync(noDrivePath, { nocase: true, cwd: pathRoot })[0]
}
查看更多
登录 后发表回答