gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:19:53 42 4
gpt4 key购买 nike

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

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

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

最佳答案

具有不区分大小写的文件系统(Windows、macOS)的平台使得获取给定路径的 case-exact 形式变得异常困难,可能是大小写不同的路径 - 似乎没有 系统 它的 API,因此 Node.js(或 Python、Perl 等)等环境不应受到责备。

<支持>更新:@barsh很好到package将下面的代码与 npm 一起使用,所以 您可以使用
轻松安装它npm install tr​​ue-case-path

glob npm package它的 nocase 选项在这里拯救了(尽管它需要在 Windows 上进行一些调整);基本上,将输入路径视为 glob - 即使它是 literal 路径 - 也会使 glob() 返回文件系统中存储的真实情况:

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

  • 使用下面的trueCasePathSync()函数;请参阅使用和限制的评论;值得注意的是,虽然输入路径也被规范化,但 .. 开头的路径 受支持,因为 path.normalize() 不会相对于当前目录解析它们。

    • 注意:trueCasePathSync() 确实返回规范路径:如果你传入相对路径,你会得到一个相对路径输出路径也是如此,并且没有解析符号链接(symbolic link)。如果您想要规范路径,请将 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]
}

关于node.js - 如何在 Windows 上的 Node.js 中获取文件的大小写精确路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086985/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com