default-tmp.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. exports.defaultTmpSync = exports.defaultTmp = void 0;
  7. // The default temporary folder location for use in the windows algorithm.
  8. // It's TEMPting to use dirname(path), since that's guaranteed to be on the
  9. // same device. However, this means that:
  10. // rimraf(path).then(() => rimraf(dirname(path)))
  11. // will often fail with EBUSY, because the parent dir contains
  12. // marked-for-deletion directory entries (which do not show up in readdir).
  13. // The approach here is to use os.tmpdir() if it's on the same drive letter,
  14. // or resolve(path, '\\temp') if it exists, or the root of the drive if not.
  15. // On Posix (not that you'd be likely to use the windows algorithm there),
  16. // it uses os.tmpdir() always.
  17. const os_1 = require("os");
  18. const path_1 = require("path");
  19. const fs_js_1 = require("./fs.js");
  20. const platform_js_1 = __importDefault(require("./platform.js"));
  21. const { stat } = fs_js_1.promises;
  22. const isDirSync = (path) => {
  23. try {
  24. return (0, fs_js_1.statSync)(path).isDirectory();
  25. }
  26. catch (er) {
  27. return false;
  28. }
  29. };
  30. const isDir = (path) => stat(path).then(st => st.isDirectory(), () => false);
  31. const win32DefaultTmp = async (path) => {
  32. const { root } = (0, path_1.parse)(path);
  33. const tmp = (0, os_1.tmpdir)();
  34. const { root: tmpRoot } = (0, path_1.parse)(tmp);
  35. if (root.toLowerCase() === tmpRoot.toLowerCase()) {
  36. return tmp;
  37. }
  38. const driveTmp = (0, path_1.resolve)(root, '/temp');
  39. if (await isDir(driveTmp)) {
  40. return driveTmp;
  41. }
  42. return root;
  43. };
  44. const win32DefaultTmpSync = (path) => {
  45. const { root } = (0, path_1.parse)(path);
  46. const tmp = (0, os_1.tmpdir)();
  47. const { root: tmpRoot } = (0, path_1.parse)(tmp);
  48. if (root.toLowerCase() === tmpRoot.toLowerCase()) {
  49. return tmp;
  50. }
  51. const driveTmp = (0, path_1.resolve)(root, '/temp');
  52. if (isDirSync(driveTmp)) {
  53. return driveTmp;
  54. }
  55. return root;
  56. };
  57. const posixDefaultTmp = async () => (0, os_1.tmpdir)();
  58. const posixDefaultTmpSync = () => (0, os_1.tmpdir)();
  59. exports.defaultTmp = platform_js_1.default === 'win32' ? win32DefaultTmp : posixDefaultTmp;
  60. exports.defaultTmpSync = platform_js_1.default === 'win32' ? win32DefaultTmpSync : posixDefaultTmpSync;
  61. //# sourceMappingURL=default-tmp.js.map