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