如何在Windows上的Node.js中获取文件的大小写确切路径?

Mar*_*cka 10 windows macos path case-sensitive node.js

我有一条路径,让我们说C:\temp\something.js,我想在Windows上获得路径的大小写确切版本 - 所以如果C:\Temp\someThing.js存储在磁盘上,我想得到这个值(路径).

如何从Node.js中的后一个路径获取前一个路径?

我已经通过FS API(去https://nodejs.org/api/fs.html),我还没有发现任何有用的东西(即fs.realpathSync,fs.statSync,fs.accessSync没有回我的需要).

mkl*_*nt0 12

不区分大小写的文件系统(在Windows,MacOS的)平台,使其出人意料地难以得到的情况下,准确给定的,可能的情况下变道的形式-有似乎是没有系统为它的API,这样的环境,如Node.js的(或Python,Perl,...)不应该受到责备.

更新:@barsh非常适合打包下面的代码以供使用npm,因此您可以轻松安装它
npm install true-case-path
.

带有选项globnpm包nocase在这里得到了解决(尽管它需要在Windows上进行一些调整); 基本上,将输入路径视为glob - 即使它是文字路径 - 使得glob()返回存储在文件系统中的真实案例:

  • glob在项目文件夹中安装软件包:( npm install glob添加--save--save-dev根据需要).

  • 使用以下trueCasePathSync()功能 ; 查看有关使用和限制的评论; 值得注意的是,虽然输入路径也是标准化的,但支持 路径开头的路径,因为相对于当前目录不能解析它们...path.normalize()

    • :trueCasePathSync()没有返回规范路径:如果你在一个相对路径通过,你会得到一个相对的输出路径,以及,没有符号链接得到解决.如果您想要规范路径,请应用于fs.realPathSync()结果.
  • 应该在Windows,macOS和Linux上工作(虽然对区分大小写的文件系统的用处有限),使用Node.js v4.1.1进行测试

    • 注意:在Windows上,不会尝试大小写更正路径的驱动器号或UNC共享组件(服务器名称,共享名称).
/*
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]
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!我把它添加到npm作为`true-case-path`.https://www.npmjs.com/package/true-case-path (2认同)

Pio*_*otr 5

从节点 9 开始,fs.realpathSync.native似乎可以解决问题。