a3988746b988c854a1cfe0e5619645c370f830019a0ec5f9f060132a2c872355e6a07401c41773c685ed913a0d1d814601217c50dbf129e9173e2c9da032fb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.chownrSync = exports.chownr = void 0;
  7. const node_fs_1 = __importDefault(require("node:fs"));
  8. const node_path_1 = __importDefault(require("node:path"));
  9. const lchownSync = (path, uid, gid) => {
  10. try {
  11. return node_fs_1.default.lchownSync(path, uid, gid);
  12. }
  13. catch (er) {
  14. if (er?.code !== 'ENOENT')
  15. throw er;
  16. }
  17. };
  18. const chown = (cpath, uid, gid, cb) => {
  19. node_fs_1.default.lchown(cpath, uid, gid, er => {
  20. // Skip ENOENT error
  21. cb(er && er?.code !== 'ENOENT' ? er : null);
  22. });
  23. };
  24. const chownrKid = (p, child, uid, gid, cb) => {
  25. if (child.isDirectory()) {
  26. (0, exports.chownr)(node_path_1.default.resolve(p, child.name), uid, gid, (er) => {
  27. if (er)
  28. return cb(er);
  29. const cpath = node_path_1.default.resolve(p, child.name);
  30. chown(cpath, uid, gid, cb);
  31. });
  32. }
  33. else {
  34. const cpath = node_path_1.default.resolve(p, child.name);
  35. chown(cpath, uid, gid, cb);
  36. }
  37. };
  38. const chownr = (p, uid, gid, cb) => {
  39. node_fs_1.default.readdir(p, { withFileTypes: true }, (er, children) => {
  40. // any error other than ENOTDIR or ENOTSUP means it's not readable,
  41. // or doesn't exist. give up.
  42. if (er) {
  43. if (er.code === 'ENOENT')
  44. return cb();
  45. else if (er.code !== 'ENOTDIR' && er.code !== 'ENOTSUP')
  46. return cb(er);
  47. }
  48. if (er || !children.length)
  49. return chown(p, uid, gid, cb);
  50. let len = children.length;
  51. let errState = null;
  52. const then = (er) => {
  53. /* c8 ignore start */
  54. if (errState)
  55. return;
  56. /* c8 ignore stop */
  57. if (er)
  58. return cb((errState = er));
  59. if (--len === 0)
  60. return chown(p, uid, gid, cb);
  61. };
  62. for (const child of children) {
  63. chownrKid(p, child, uid, gid, then);
  64. }
  65. });
  66. };
  67. exports.chownr = chownr;
  68. const chownrKidSync = (p, child, uid, gid) => {
  69. if (child.isDirectory())
  70. (0, exports.chownrSync)(node_path_1.default.resolve(p, child.name), uid, gid);
  71. lchownSync(node_path_1.default.resolve(p, child.name), uid, gid);
  72. };
  73. const chownrSync = (p, uid, gid) => {
  74. let children;
  75. try {
  76. children = node_fs_1.default.readdirSync(p, { withFileTypes: true });
  77. }
  78. catch (er) {
  79. const e = er;
  80. if (e?.code === 'ENOENT')
  81. return;
  82. else if (e?.code === 'ENOTDIR' || e?.code === 'ENOTSUP')
  83. return lchownSync(p, uid, gid);
  84. else
  85. throw e;
  86. }
  87. for (const child of children) {
  88. chownrKidSync(p, child, uid, gid);
  89. }
  90. return lchownSync(p, uid, gid);
  91. };
  92. exports.chownrSync = chownrSync;
  93. //# sourceMappingURL=index.js.map