如何在Windows上的Node.js中获取文件的大小写精确路径? [英] How to obtain case-exact path of a file in Node.js on Windows?

查看:227
本文介绍了如何在Windows上的Node.js中获取文件的大小写精确路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个路径,比如说C:\temp\something.js,我想在Windows上获取路径的大小写精确版本-因此,如果磁盘上存储了C:\Temp\someThing.js,我想获取此值(路径).

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).

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

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

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

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).

推荐答案

具有不区分大小写的文件系统的平台(Windows,macOS)令人惊讶地难以获得给定的 case-exact 形式,可能是因情况而异的路径-似乎没有 system API,因此不应该抱怨诸如Node.js(或Python,Perl等)的环境.

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.

更新: @barsh 足以使

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
.

带有nocase选项的 glob npm软件包此处进行救援(尽管需要在Windows上进行一些调整);基本上,将输入路径视为glob-即使它是 literal 路径-也会使glob()返回存储在文件系统中的真实大小写:

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:

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

使用下面的 trueCasePathSync()功能;有关用法和限制,请参见注释;值得注意的是,虽然输入路径也已规范化,但 ..开头的路径不被支持,因为path.normalize()不能相对于当前目录解析它们.

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.

  • 注意:trueCasePathSync()不会返回规范路径:如果您通过相对路径,也会获得相对输出路径,并且没有符号链接已解决.如果需要规范路径,请对结果应用fs.realPathSync().
  • 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.

应在 Windows,macOS和Linux 上工作(尽管在区分大小写的文件系统上作用有限),并已通过Node.js v4.1.1进行了测试

Should work on Windows, macOS, and Linux (though with limited usefulness on case-sensitive filesystems), tested with Node.js v4.1.1

  • 注意:在Windows上,尝试区分大小写纠正路径(服务器名称,共享名称)的驱动器号或UNC共享组件. /li>
  • 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]
}

这篇关于如何在Windows上的Node.js中获取文件的大小写精确路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆