39803cb3c423c79ff53c967c5e1510d82c7ef23c17c96501e569bf71031cd1ed413a460758429aa24ec31a3c09014bbb58d744279271341f11917f119248dd 1.0 KB

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