path-arg.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const path_1 = require("path");
  7. const util_1 = require("util");
  8. const platform_js_1 = __importDefault(require("./platform.js"));
  9. const pathArg = (path, opt = {}) => {
  10. const type = typeof path;
  11. if (type !== 'string') {
  12. const ctor = path && type === 'object' && path.constructor;
  13. const received = ctor && ctor.name ? `an instance of ${ctor.name}`
  14. : type === 'object' ? (0, util_1.inspect)(path)
  15. : `type ${type} ${path}`;
  16. const msg = 'The "path" argument must be of type string. ' + `Received ${received}`;
  17. throw Object.assign(new TypeError(msg), {
  18. path,
  19. code: 'ERR_INVALID_ARG_TYPE',
  20. });
  21. }
  22. if (/\0/.test(path)) {
  23. // simulate same failure that node raises
  24. const msg = 'path must be a string without null bytes';
  25. throw Object.assign(new TypeError(msg), {
  26. path,
  27. code: 'ERR_INVALID_ARG_VALUE',
  28. });
  29. }
  30. path = (0, path_1.resolve)(path);
  31. const { root } = (0, path_1.parse)(path);
  32. if (path === root && opt.preserveRoot !== false) {
  33. const msg = 'refusing to remove root directory without preserveRoot:false';
  34. throw Object.assign(new Error(msg), {
  35. path,
  36. code: 'ERR_PRESERVE_ROOT',
  37. });
  38. }
  39. if (platform_js_1.default === 'win32') {
  40. const badWinChars = /[*|"<>?:]/;
  41. const { root } = (0, path_1.parse)(path);
  42. if (badWinChars.test(path.substring(root.length))) {
  43. throw Object.assign(new Error('Illegal characters in path.'), {
  44. path,
  45. code: 'EINVAL',
  46. });
  47. }
  48. }
  49. return path;
  50. };
  51. exports.default = pathArg;
  52. //# sourceMappingURL=path-arg.js.map