strip-absolute-path.js 918 B

123456789101112131415161718192021222324
  1. // unix absolute paths are also absolute on win32, so we use this for both
  2. const { isAbsolute, parse } = require('path').win32
  3. // returns [root, stripped]
  4. // Note that windows will think that //x/y/z/a has a "root" of //x/y, and in
  5. // those cases, we want to sanitize it to x/y/z/a, not z/a, so we strip /
  6. // explicitly if it's the first character.
  7. // drive-specific relative paths on Windows get their root stripped off even
  8. // though they are not absolute, so `c:../foo` becomes ['c:', '../foo']
  9. module.exports = path => {
  10. let r = ''
  11. let parsed = parse(path)
  12. while (isAbsolute(path) || parsed.root) {
  13. // windows will think that //x/y/z has a "root" of //x/y/
  14. // but strip the //?/C:/ off of //?/C:/path
  15. const root = path.charAt(0) === '/' && path.slice(0, 4) !== '//?/' ? '/'
  16. : parsed.root
  17. path = path.substr(root.length)
  18. r += root
  19. parsed = parse(path)
  20. }
  21. return [r, path]
  22. }