index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict'
  2. var glob = require('glob')
  3. var path = require('path')
  4. function trueCasePathSync(fsPath) {
  5. // Normalize the path so as to resolve . and .. components.
  6. // !! As of Node v4.1.1, a path starting with ../ is NOT resolved relative
  7. // !! to the current dir, and glob.sync() below then fails.
  8. // !! When in doubt, resolve with fs.realPathSync() *beforehand*.
  9. var fsPathNormalized = path.normalize(fsPath)
  10. // OSX: HFS+ stores filenames in NFD (decomposed normal form) Unicode format,
  11. // so we must ensure that the input path is in that format first.
  12. if (process.platform === 'darwin') fsPathNormalized = fsPathNormalized.normalize('NFD')
  13. // !! Windows: Curiously, the drive component mustn't be part of a glob,
  14. // !! otherwise glob.sync() will invariably match nothing.
  15. // !! Thus, we remove the drive component and instead pass it in as the 'cwd'
  16. // !! (working dir.) property below.
  17. var pathRoot = path.parse(fsPathNormalized).root
  18. var noDrivePath = fsPathNormalized.slice(Math.max(pathRoot.length - 1, 0))
  19. // Perform case-insensitive globbing (on Windows, relative to the drive /
  20. // network share) and return the 1st match, if any.
  21. // Fortunately, glob() with nocase case-corrects the input even if it is
  22. // a *literal* path.
  23. return glob.sync(noDrivePath, { nocase: true, cwd: pathRoot })[0]
  24. }
  25. module.exports = trueCasePathSync