piattaforme con case-insensitive file system (Windows, MacOS) rendono sorprendentemente difficile per ottenere il caso esatto forma di un dato, possibilmente percorso caso-variante - non ci sembrano essere nessun sistema API per esso, quindi non sono da biasimare ambienti come Node.js (o Python, Perl, ...).
Aggiornamento: @barsh stato bello abbastanza per package il codice qui sotto per l'utilizzo con npm
, in modo da è possibile installarlo facilmente con
npm install true-case-path
.
La glob
npm package con l'opzione nocase
viene in soccorso qui (anche se aveva bisogno di qualche ritocco su Windows); in pratica, trattando il percorso di ingresso come glob - anche se si tratta di un letterale percorso - marche glob()
restituiscono il vero e proprio caso come memorizzata nel file system:
installare il pacchetto glob
nella cartella del progetto: npm install glob
(aggiungere --save
o --save-dev
secondo necessità).
Utilizzare la funzione trueCasePathSync()
di seguito; vedere i commenti per l'utilizzo e le limitazioni; in particolare, mentre il percorso di input è anche normalizzato, i percorsi a partire da..
sono non supportati, perché path.normalize()
non li risolve relativamente alla directory corrente.
- NOTA:
trueCasePathSync()
fa non restituire un percorso canonico: se si passa in un percorso relativo, si otterrà un percorso di uscita relativo pure, e nessun link simbolici sono risolti. Se si desidera il percorso canonico, applicare fs.realPathSync()
al risultato.
dovrebbe funzionare su Windows, MacOS e Linux (anche se con scarsa utilità su file system case-sensitive), controllato con Node.js v4.1.1
- NOTA: Su Windows, non tentativo di caso-correggere la lettera di unità o il componente UNC-quota di percorso (nome del server, nome di condivisione).
/*
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]
}
https://github.com/nodejs/node/issues/3352 –
domanda simile http://stackoverflow.com/questions/4763117/how-can-i-obtain -the-case-sensitive-path-on-windows per .NET –
Un'altra soluzione: https://github.com/Microsoft/TypeScript/issues/3626#issuecomment-148966821 di duanyao (https://github.com/duanyao) –