2015-10-12 11 views
9

私はパスがあります。C:\temp\something.jsとし、Windows上のパスの大文字と小文字の正確なバージョンを取得したいと思います。もしC:\Temp\someThing.jsがディスクに保存されていれば、この値(パス)を取得したいと思います。Windows上でNode.js内のファイルのcase-exactパスを取得する方法は?

Node.jsの後のパスから元のパスを取得するにはどうすればよいですか?

私はすでにFSのAPI(https://nodejs.org/api/fs.html)を介して行っていると私は有益な何かを発見していない(つまりfs.realpathSyncfs.statSyncfs.accessSyncは私が必要なものを返しませんでした)。大文字と小文字を区別しないファイルシステム(Windowsの、MacOSの)と

+0

https://github.com/nodejs/node/issues/3352 –

+0

同様の質問http://stackoverflow.com/questions/4763117/how-can-i-obtain - 大文字と小文字を区別するWindows上のパスのパス –

+0

別の解決策:httany://github.com/Microsoft/TypeScript/issues/3626#issuecomment-148966821 by duanyao(https://github.com/duanyao) ) –

答えて

9

プラットフォーム、それは意外に難しい与え、おそらくケースバリアントパスの場合、正確なフォームを取得するために作る - 、それにはシステムのAPIは存在しないと思われますNode.js(またはPython、Perl、...)などの環境は責任を負いません。

更新@barshnpmで使用するために以下のコードまでpackageに十分素敵だったのであなたは
npm install true-case-path
で簡単にインストールすることができます。

(それはWindows上でいくつかの調整が必要だが)そのnocaseオプション付きglob npm packageがここに救助に来ます。

  • は、プロジェクトフォルダ内のパッケージglobをインストールします:npm install glob基本的には、グロブとして入力パスを処理する - - それはリテラルパスであっても、ファイルシステムに保存されているようになり、真のケースを返す​​3210 (必要に応じて--saveまたは--save-devを追加してください)。

  • trueCasePathSync()下記の機能を使用してください。使用法と制限のコメントを参照してください。特に、入力パスも正規化されていますが、..で始まるパスpath.normalize()が現在のディレクトリに対して相対パスを解決しないため、ではなくがサポートされています。

    • trueCasePathSync()は正規のパスを返ししません:あなたは、相対パスを渡した場合、あなたにも、相対出力パスを取得します、そして何のシンボリックリンクが解決されていません。正規パスを使用する場合は、結果にfs.realPathSync()を適用します。
  • は、Node.jsのv4.1のでテストのWindows、MacOSの、およびLinux(大文字と小文字を区別したファイルシステム上の限られた有用性を持つが)、上で動作するはずです。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] 
} 
+2

ありがとう!これをnpmに 'true-case-path'として追加しました。 https://www.npmjs.com/package/true-case-path – barsh

+0

少なくとも悲しいことに、少なくとも2017年12月29日現在、これは多くの場合には機能しません。ドライブ文字は扱いません。また、UNCパスを処理しません – gman

+0

@gman:ソースコードのコメントに記載されているように、実際にはドライブレターとパスのUNC-share _part(サーバー名、共有名)では機能しません。しかし、私は、これをもっと明白にするために、上記の箇条書きにこの情報を追加しました。あなたが他の制限を知っているかどうか教えてください。 – mklement0

関連する問題