2015-10-12 19 views
9

Tôi có một đường dẫn, giả sử C:\temp\something.js và tôi muốn có phiên bản chính xác của đường dẫn trên Windows - vì vậy nếu có C:\Temp\someThing.js được lưu trữ trên đĩa, tôi muốn lấy giá trị này (đường dẫn).Cách lấy đường dẫn trường hợp chính xác của tệp trong Node.js trên Windows?

Làm thế nào tôi có thể nhận được từ con đường cũ sau này trong Node.js?

Tôi đã trải qua API FS (https://nodejs.org/api/fs.html) và tôi không thấy bất kỳ điều gì hữu ích (cụ thể là fs.realpathSync, fs.statSync, fs.accessSync không trả lại những gì tôi cần).

+0

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

+0

câu hỏi tương tự http://stackoverflow.com/questions/4763117/how-can-i-obtain -e-case-sensitive-path-on-windows cho .NET –

+0

Một giải pháp khác: https://github.com/Microsoft/TypeScript/issues/3626#issuecomment-148966821 bởi duanyao (https://github.com/duanyao) –

Trả lời

9

Platforms với case-insensitive hệ thống tập tin (Windows, MacOS) làm cho nó đáng ngạc nhiên khó có thể lấy mẫu đơn trường chính xác của một, có thể là con đường trường biến thể nhất định - có vẻ như là không có hệ thống API cho nó, vì vậy các môi trường như Node.js (hoặc Python, Perl, ...) không được đổ lỗi.

Cập nhật: @barsh đã được tốt đẹp đủ để package lên đoạn code dưới đây để sử dụng với npm, vì vậy bạn có thể cài đặt nó một cách dễ dàng với
npm install true-case-path
.

Các glob npm package với tùy chọn nocase của nó đến để giải thoát đây (mặc dù nó cần một số tinh chỉnh trên Windows); về cơ bản, xử lý con đường đầu vào như một glob - ngay cả khi nó là một đen đường - làm cho glob() trở lại trường hợp đúng như lưu trữ trong hệ thống tập tin:

  • Cài đặt gói glob trong thư mục dự án của bạn: npm install glob (thêm --save hoặc --save-dev nếu cần).

  • Sử dụng chức năng trueCasePathSync() bên dưới; xem nhận xét về mức sử dụng và giới hạn; đáng chú ý, trong khi đường dẫn đầu vào cũng được chuẩn hóa, đường dẫn bắt đầu bằng..không phải là được hỗ trợ, bởi vì path.normalize() không giải quyết chúng liên quan đến thư mục hiện tại.

    • LƯU Ý: trueCasePathSync() không không trả về một con đường kinh điển: nếu bạn vượt qua trong một đường dẫn tương đối, bạn sẽ nhận được một đường dẫn đầu ra tương đối là tốt, và không có liên kết tượng trưng được giải quyết. Nếu bạn muốn đường dẫn chuẩn, áp dụng fs.realPathSync() cho kết quả.
  • nên làm việc trên Windows, MacOS và Linux (mặc dù với tính hữu dụng hạn chế trên hệ thống tập tin case-sensitive), thử nghiệm với Node.js v4.1.1

    • LƯU Ý: Trên Windows, không nỗ lực được thực hiện để trường đúng các ký tự ổ đĩa hoặc một thành phần UNC-phần của con đường (tên máy chủ, tên cổ phiếu).
/* 
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

cảm ơn! Tôi đã thêm điều này vào npm là 'true-case-path'. https://www.npmjs.com/package/true-case-path – barsh

+0

thật đáng buồn ít nhất là từ 2017-12-29, điều này không hiệu quả đối với nhiều trường hợp. Nó không xử lý các ký tự ổ đĩa. Nó cũng không xử lý đường dẫn UNC – gman

+0

@gman: Nó thực sự không hoạt động đối với các ký tự ổ đĩa và UNC-chia sẻ _part_ của một đường dẫn (tên máy chủ, tên chia sẻ), như đã nêu trong các chú thích mã nguồn; tuy nhiên, tôi vừa thêm thông tin này vào các điểm bullet ở trên để làm điều này rõ ràng hơn. Hãy cho chúng tôi biết nếu bạn biết các hạn chế khác. – mklement0

Các vấn đề liên quan