pwd-fs 3.1.3 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/src/bitmask.d.ts +1 -0
- package/lib/src/bitmask.js +18 -0
- package/lib/src/index.d.ts +3 -5
- package/lib/src/index.js +21 -0
- package/lib/src/powered-file-system.d.ts +21 -3
- package/lib/{powered-file-system.js → src/powered-file-system.js} +69 -67
- package/lib/src/recurse-io-sync.d.ts +5 -13
- package/lib/src/recurse-io-sync.js +93 -0
- package/lib/src/recurse-io.d.ts +6 -16
- package/lib/{recurse-io.js → src/recurse-io.js} +44 -41
- package/lib/test/__fmock.js +40 -0
- package/lib/test/append.spec.js +58 -0
- package/lib/test/bitmask.spec.js +26 -0
- package/lib/test/chmod.spec.js +62 -0
- package/lib/test/chown.spec.js +74 -0
- package/lib/test/constructor.spec.js +17 -0
- package/lib/test/copy.spec.js +80 -0
- package/lib/test/mkdir.spec.js +90 -0
- package/lib/test/read.spec.js +73 -0
- package/lib/test/readdir.spec.js +70 -0
- package/lib/test/remove.spec.js +63 -0
- package/lib/test/rename.spec.js +66 -0
- package/lib/test/stat.spec.js +76 -0
- package/lib/test/symlink.spec.js +74 -0
- package/lib/test/test.spec.js +60 -0
- package/lib/test/write.spec.js +82 -0
- package/package.json +8 -15
- package/readme.md +14 -12
- package/src/bitmask.ts +20 -0
- package/src/index.ts +4 -26
- package/src/powered-file-system.ts +50 -52
- package/src/recurse-io-sync.ts +24 -24
- package/src/recurse-io.ts +18 -19
- package/{__tests__ → test}/append.spec.ts +2 -3
- package/{__tests__ → test}/chmod.spec.ts +2 -3
- package/{__tests__ → test}/chown.spec.ts +18 -14
- package/{__tests__ → test}/constructor.spec.ts +1 -1
- package/{__tests__ → test}/copy.spec.ts +2 -3
- package/{__tests__ → test}/mkdir.spec.ts +2 -1
- package/{__tests__ → test}/read.spec.ts +2 -3
- package/{__tests__ → test}/readdir.spec.ts +3 -4
- package/{__tests__ → test}/remove.spec.ts +3 -3
- package/{__tests__ → test}/rename.spec.ts +2 -2
- package/{__tests__ → test}/stat.spec.ts +2 -3
- package/{__tests__ → test}/symlink.spec.ts +3 -3
- package/{__tests__ → test}/test.spec.ts +1 -3
- package/{__tests__ → test}/write.spec.ts +2 -2
- package/tsconfig.json +5 -5
- package/jest.config.ts +0 -14
- package/lib/index.js +0 -24
- package/lib/recurse-io-sync.js +0 -90
- package/rollup.config.js +0 -16
- /package/lib/{__tests__ → test}/__fmock.d.ts +0 -0
- /package/lib/{__tests__ → test}/append.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/bitmask.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/chmod.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/chown.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/constructor.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/copy.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/mkdir.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/read.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/readdir.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/remove.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/rename.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/stat.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/symlink.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/test.spec.d.ts +0 -0
- /package/lib/{__tests__ → test}/write.spec.d.ts +0 -0
- /package/{__tests__ → test}/__fmock.ts +0 -0
- /package/{__tests__ → test}/bitmask.spec.ts +0 -0
package/readme.md
CHANGED
|
@@ -19,7 +19,7 @@ To improve reliability and maintainability the code is migrated to [TypeScript](
|
|
|
19
19
|
To use `Powered File System` in your project, run:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
npm
|
|
22
|
+
npm install pwd-fs
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
#### Table of Contents
|
|
@@ -47,7 +47,7 @@ The scope `URI` of the class methods are divided into groups.
|
|
|
47
47
|
| URI | Methods |
|
|
48
48
|
|-----------------------------|------------------------------------------------------------------|
|
|
49
49
|
| Common (file and directory) | `chmod` `chown` `copy` `remove` `rename` `symlink` `stat` `test` |
|
|
50
|
-
| File only | `read` `write`
|
|
50
|
+
| File only | `read` `write` |
|
|
51
51
|
| Directory only | `mkdir` `readdir` |
|
|
52
52
|
|
|
53
53
|
|
|
@@ -63,19 +63,17 @@ This class implemented by following the [ECMAScript® 2018 Language Specificatio
|
|
|
63
63
|
String form paths are interpreted as UTF-8 character sequences identifying the absolute or relative filename.
|
|
64
64
|
|
|
65
65
|
```ts
|
|
66
|
-
import
|
|
66
|
+
import { pfs } from 'pwd-fs';
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* pfs.pwd === process.cwd()
|
|
70
70
|
*/
|
|
71
|
-
|
|
72
|
-
const pfs = new PoweredFileSystem();
|
|
73
71
|
```
|
|
74
72
|
|
|
75
73
|
Relative paths will be resolved relative to the current working directory as specified by `process.cwd()`:
|
|
76
74
|
|
|
77
75
|
```ts
|
|
78
|
-
import PoweredFileSystem from 'pwd-fs';
|
|
76
|
+
import { PoweredFileSystem } from 'pwd-fs';
|
|
79
77
|
|
|
80
78
|
/**
|
|
81
79
|
* pfs.pwd === `${process.cwd()}/foo/bar`
|
|
@@ -87,7 +85,7 @@ const pfs = new PoweredFileSystem('./foo/bar');
|
|
|
87
85
|
Absolute paths:
|
|
88
86
|
|
|
89
87
|
```ts
|
|
90
|
-
import PoweredFileSystem from 'pwd-fs';
|
|
88
|
+
import { PoweredFileSystem } from 'pwd-fs';
|
|
91
89
|
|
|
92
90
|
/**
|
|
93
91
|
* pfs.pwd === __dirname
|
|
@@ -141,23 +139,25 @@ These functions return information about a resource in the file system.
|
|
|
141
139
|
Asynchronously changes the permissions of a file.
|
|
142
140
|
|
|
143
141
|
```ts
|
|
142
|
+
import { bitmask } from 'pwd-fs';
|
|
143
|
+
|
|
144
144
|
await pfs.chmod('./path', 0o750);
|
|
145
145
|
const { mode } = await pfs.stat('./path');
|
|
146
146
|
|
|
147
|
-
console.log(
|
|
147
|
+
console.log(bitmask(mode) === 0o750); // true
|
|
148
148
|
```
|
|
149
149
|
|
|
150
150
|
> **Caveats:** on Windows only the write permission can be changed, and the distinction among the permissions of group, owner or others is not implemented.
|
|
151
151
|
|
|
152
152
|
See manuals [chmod(2)](http://man7.org/linux/man-pages/man2/chmod.2.html)
|
|
153
153
|
|
|
154
|
-
#### pfs.chown(src,
|
|
154
|
+
#### pfs.chown(src, [, options])
|
|
155
155
|
|
|
156
156
|
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths will be resolved relative to the present working directory as specified by `pfs.pwd`.
|
|
157
|
-
- `uid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>
|
|
158
|
-
- `gid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>
|
|
159
157
|
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
160
158
|
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
159
|
+
- `uid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new owner's user id.
|
|
160
|
+
- `gid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new group's group id.
|
|
161
161
|
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful change, the `Promise` is resolved with an value with a `undefined`.
|
|
162
162
|
|
|
163
163
|
Asynchronously changes owner and group of a file.
|
|
@@ -194,10 +194,12 @@ See manuals [symlink(2)](http://man7.org/linux/man-pages/man2/symlink.2.html).
|
|
|
194
194
|
Asynchronously recursively copy a file or directory.
|
|
195
195
|
|
|
196
196
|
```ts
|
|
197
|
+
import { bitmask } from 'pwd-fs';
|
|
198
|
+
|
|
197
199
|
await pfs.copy('./path/file.txt', './dist');
|
|
198
200
|
const { mode } = await pfs.stat('./dist/path/file.txt');
|
|
199
201
|
|
|
200
|
-
console.log(
|
|
202
|
+
console.log(bitmask(mode) === 0o666); // true
|
|
201
203
|
```
|
|
202
204
|
|
|
203
205
|
#### pfs.rename(src, use[, options])
|
package/src/bitmask.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function bitmask(mode: number) {
|
|
2
|
+
const type = typeof mode;
|
|
3
|
+
|
|
4
|
+
if (type !== 'number') {
|
|
5
|
+
throw new Error(
|
|
6
|
+
`Argument of type '${type}' is not assignable to parameter of type 'number'.`
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const permissions: number[] = [ 0o400, 0o200, 0o100, 0o040, 0o020, 0o010, 0o004, 0o002, 0o001 ];
|
|
11
|
+
let umask = 0o000;
|
|
12
|
+
|
|
13
|
+
for (const flag of permissions) {
|
|
14
|
+
if (mode & flag) {
|
|
15
|
+
umask += flag;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return umask;
|
|
20
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,29 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Mode as TMode,
|
|
3
|
-
type Flag as TFlag,
|
|
4
|
-
type Stats as TStats,
|
|
5
|
-
PoweredFileSystem
|
|
6
|
-
} from './powered-file-system';
|
|
1
|
+
import { PoweredFileSystem } from './powered-file-system';
|
|
7
2
|
|
|
8
|
-
export
|
|
9
|
-
export type Flag = TFlag;
|
|
10
|
-
export type Stats = TStats;
|
|
3
|
+
export const pfs = new PoweredFileSystem();
|
|
11
4
|
|
|
12
|
-
|
|
5
|
+
export default PoweredFileSystem;
|
|
13
6
|
|
|
14
|
-
|
|
15
|
-
// export const stat = pfs.stat;
|
|
16
|
-
// export const chmod = pfs.chmod;
|
|
17
|
-
// export const chown = pfs.chown;
|
|
18
|
-
// export const symlink = pfs.symlink;
|
|
19
|
-
// export const copy = pfs.copy;
|
|
20
|
-
// export const rename = pfs.rename;
|
|
21
|
-
// export const remove = pfs.remove;
|
|
22
|
-
// export const read = pfs.read;
|
|
23
|
-
// export const write = pfs.write;
|
|
24
|
-
// export const append = pfs.append;
|
|
25
|
-
// export const readdir = pfs.readdir;
|
|
26
|
-
// export const mkdir = pfs.mkdir;
|
|
27
|
-
|
|
28
|
-
export const bitmask = PoweredFileSystem.bitmask;
|
|
29
|
-
export default PoweredFileSystem;
|
|
7
|
+
export * from './powered-file-system';
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
1
|
+
import fs, { NoParamCallback } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { chmod, chown, copy, remove, mkdir } from './recurse-io';
|
|
4
|
+
import { chmodSync, chownSync, copySync, removeSync, mkdirSync } from './recurse-io-sync';
|
|
5
|
+
import { bitmask } from './bitmask';
|
|
5
6
|
|
|
6
7
|
export type Mode = keyof IConstants;
|
|
7
8
|
export type Flag = Mode | 'a';
|
|
8
9
|
export type Stats = fs.Stats;
|
|
9
10
|
|
|
11
|
+
export * from './bitmask';
|
|
12
|
+
|
|
10
13
|
export interface IConstants {
|
|
11
14
|
e: number,
|
|
12
15
|
r: number,
|
|
@@ -14,18 +17,6 @@ export interface IConstants {
|
|
|
14
17
|
x: number
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
const permissions: number[] = [
|
|
18
|
-
0o400, // OWNER_READ
|
|
19
|
-
0o200, // OWNER_WRITE
|
|
20
|
-
0o100, // OWNER_EXECUTE
|
|
21
|
-
0o040, // GROUP_READ
|
|
22
|
-
0o020, // GROUP_WRITE
|
|
23
|
-
0o010, // GROUP_EXECUTE
|
|
24
|
-
0o004, // OTHERS_READ
|
|
25
|
-
0o002, // OTHERS_WRITE
|
|
26
|
-
0o001 // OTHERS_EXECUTE
|
|
27
|
-
];
|
|
28
|
-
|
|
29
20
|
export class PoweredFileSystem {
|
|
30
21
|
readonly pwd: string;
|
|
31
22
|
|
|
@@ -36,6 +27,8 @@ export class PoweredFileSystem {
|
|
|
36
27
|
x: fs.constants.X_OK
|
|
37
28
|
};
|
|
38
29
|
|
|
30
|
+
static bitmask = bitmask;
|
|
31
|
+
|
|
39
32
|
constructor(pwd?: string) {
|
|
40
33
|
this.pwd = pwd ? path.resolve(pwd) : process.cwd();
|
|
41
34
|
}
|
|
@@ -43,26 +36,6 @@ export class PoweredFileSystem {
|
|
|
43
36
|
private resolve(src: string) {
|
|
44
37
|
return path.resolve(this.pwd, src);
|
|
45
38
|
}
|
|
46
|
-
|
|
47
|
-
static bitmask(mode: number) {
|
|
48
|
-
const type = typeof mode;
|
|
49
|
-
|
|
50
|
-
if (type !== 'number') {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`Argument of type '${type}' is not assignable to parameter of type 'number'.`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let umask = 0o000;
|
|
57
|
-
|
|
58
|
-
for (const flag of permissions) {
|
|
59
|
-
if (mode & flag) {
|
|
60
|
-
umask += flag;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return umask;
|
|
65
|
-
}
|
|
66
39
|
|
|
67
40
|
test(src: string, options: {
|
|
68
41
|
sync: true,
|
|
@@ -131,11 +104,11 @@ export class PoweredFileSystem {
|
|
|
131
104
|
src = this.resolve(src);
|
|
132
105
|
|
|
133
106
|
if (sync) {
|
|
134
|
-
return
|
|
107
|
+
return chmodSync(src, mode);
|
|
135
108
|
}
|
|
136
109
|
|
|
137
110
|
return new Promise<void>((resolve, reject) => {
|
|
138
|
-
|
|
111
|
+
chmod(src, mode, (err) => {
|
|
139
112
|
if (err) {
|
|
140
113
|
return reject(err);
|
|
141
114
|
}
|
|
@@ -145,23 +118,27 @@ export class PoweredFileSystem {
|
|
|
145
118
|
});
|
|
146
119
|
}
|
|
147
120
|
|
|
148
|
-
chown(src: string,
|
|
149
|
-
sync: true
|
|
121
|
+
chown(src: string, options: {
|
|
122
|
+
sync: true,
|
|
123
|
+
uid?: number,
|
|
124
|
+
gid?: number
|
|
150
125
|
}): void;
|
|
151
126
|
|
|
152
|
-
chown(src: string,
|
|
153
|
-
sync?: false
|
|
127
|
+
chown(src: string, options?: {
|
|
128
|
+
sync?: false,
|
|
129
|
+
uid?: number,
|
|
130
|
+
gid?: number
|
|
154
131
|
}): Promise<void>;
|
|
155
132
|
|
|
156
|
-
chown(src: string,
|
|
133
|
+
chown(src: string, { sync = false, uid = 0, gid = 0 }: { sync?: boolean, uid?: number, gid?: number } = {}) {
|
|
157
134
|
src = this.resolve(src);
|
|
158
135
|
|
|
159
136
|
if (sync) {
|
|
160
|
-
return
|
|
137
|
+
return chownSync(src, uid, gid);
|
|
161
138
|
}
|
|
162
139
|
|
|
163
140
|
return new Promise<void>((resolve, reject) => {
|
|
164
|
-
|
|
141
|
+
chown(src, uid, gid, (err) => {
|
|
165
142
|
if (err) {
|
|
166
143
|
return reject(err);
|
|
167
144
|
}
|
|
@@ -216,11 +193,11 @@ export class PoweredFileSystem {
|
|
|
216
193
|
dir = this.resolve(dir);
|
|
217
194
|
|
|
218
195
|
if (sync) {
|
|
219
|
-
return
|
|
196
|
+
return copySync(src, dir, umask);
|
|
220
197
|
}
|
|
221
198
|
|
|
222
199
|
return new Promise<void>((resolve, reject) => {
|
|
223
|
-
|
|
200
|
+
copy(src, dir, umask, (err) => {
|
|
224
201
|
if (err) {
|
|
225
202
|
return reject(err);
|
|
226
203
|
}
|
|
@@ -269,17 +246,23 @@ export class PoweredFileSystem {
|
|
|
269
246
|
src = this.resolve(src);
|
|
270
247
|
|
|
271
248
|
if (sync) {
|
|
272
|
-
|
|
249
|
+
removeSync(src);
|
|
273
250
|
}
|
|
274
251
|
|
|
275
252
|
return new Promise<void>((resolve, reject) => {
|
|
276
|
-
|
|
253
|
+
const callback: NoParamCallback = (err) => {
|
|
277
254
|
if (err) {
|
|
278
255
|
return reject(err);
|
|
279
256
|
}
|
|
280
257
|
|
|
281
258
|
resolve();
|
|
282
|
-
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
if ('rm' in fs) {
|
|
262
|
+
return fs.rm(src, { recursive: true }, callback);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
remove(src, callback);
|
|
283
266
|
});
|
|
284
267
|
}
|
|
285
268
|
|
|
@@ -398,6 +381,9 @@ export class PoweredFileSystem {
|
|
|
398
381
|
});
|
|
399
382
|
}
|
|
400
383
|
|
|
384
|
+
/**
|
|
385
|
+
* @deprecated The method should not be used
|
|
386
|
+
*/
|
|
401
387
|
append(src: string, data: Buffer, options: {
|
|
402
388
|
sync: true,
|
|
403
389
|
encoding: null,
|
|
@@ -405,6 +391,9 @@ export class PoweredFileSystem {
|
|
|
405
391
|
flag?: Flag
|
|
406
392
|
}): void;
|
|
407
393
|
|
|
394
|
+
/**
|
|
395
|
+
* @deprecated The method should not be used
|
|
396
|
+
*/
|
|
408
397
|
append(src: string, data: string, options: {
|
|
409
398
|
sync: true,
|
|
410
399
|
encoding?: BufferEncoding,
|
|
@@ -412,6 +401,9 @@ export class PoweredFileSystem {
|
|
|
412
401
|
flag?: Flag
|
|
413
402
|
}): void;
|
|
414
403
|
|
|
404
|
+
/**
|
|
405
|
+
* @deprecated The method should not be used
|
|
406
|
+
*/
|
|
415
407
|
append(src: string, data: Buffer, options: {
|
|
416
408
|
sync?: false,
|
|
417
409
|
encoding: null,
|
|
@@ -419,6 +411,9 @@ export class PoweredFileSystem {
|
|
|
419
411
|
flag?: Flag
|
|
420
412
|
}): Promise<void>;
|
|
421
413
|
|
|
414
|
+
/**
|
|
415
|
+
* @deprecated The method should not be used
|
|
416
|
+
*/
|
|
422
417
|
append(src: string, data: string, options?: {
|
|
423
418
|
sync?: false,
|
|
424
419
|
encoding?: BufferEncoding,
|
|
@@ -426,6 +421,9 @@ export class PoweredFileSystem {
|
|
|
426
421
|
flag?: Flag
|
|
427
422
|
}): Promise<void>;
|
|
428
423
|
|
|
424
|
+
/**
|
|
425
|
+
* @deprecated The method should not be used
|
|
426
|
+
*/
|
|
429
427
|
append(src: string, data: Buffer | string, { sync = false, encoding = 'utf8', umask = 0o000, flag = 'a' }: {
|
|
430
428
|
sync?: boolean,
|
|
431
429
|
encoding?: BufferEncoding | null,
|
|
@@ -509,11 +507,11 @@ export class PoweredFileSystem {
|
|
|
509
507
|
dir = this.resolve(dir);
|
|
510
508
|
|
|
511
509
|
if (sync) {
|
|
512
|
-
return
|
|
510
|
+
return mkdirSync(dir, umask);
|
|
513
511
|
}
|
|
514
512
|
|
|
515
513
|
return new Promise<void>((resolve, reject) => {
|
|
516
|
-
|
|
514
|
+
mkdir(dir, umask, (err) => {
|
|
517
515
|
if (err) {
|
|
518
516
|
return reject(err);
|
|
519
517
|
}
|
|
@@ -522,4 +520,4 @@ export class PoweredFileSystem {
|
|
|
522
520
|
});
|
|
523
521
|
});
|
|
524
522
|
}
|
|
525
|
-
}
|
|
523
|
+
}
|
package/src/recurse-io-sync.ts
CHANGED
|
@@ -3,35 +3,43 @@ import path from 'node:path';
|
|
|
3
3
|
|
|
4
4
|
const { sep } = path;
|
|
5
5
|
|
|
6
|
-
function
|
|
7
|
-
const
|
|
6
|
+
export function chmodSync(src: string, mode: number) {
|
|
7
|
+
const stats = fs.statSync(src);
|
|
8
8
|
|
|
9
|
-
if (
|
|
9
|
+
if (stats.isDirectory()) {
|
|
10
10
|
const list = fs.readdirSync(src);
|
|
11
11
|
|
|
12
12
|
for (const loc of list) {
|
|
13
|
-
|
|
13
|
+
chmodSync(`${src}${sep}${loc}`, mode);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
fs.chmodSync(src, mode);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function
|
|
21
|
-
const
|
|
20
|
+
export function chownSync(src: string, uid: number, gid: number) {
|
|
21
|
+
const stats = fs.statSync(src);
|
|
22
22
|
|
|
23
|
-
if (
|
|
23
|
+
if (uid === 0) {
|
|
24
|
+
uid = stats.uid;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (gid === 0) {
|
|
28
|
+
gid = stats.gid;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (stats.isDirectory()) {
|
|
24
32
|
const list = fs.readdirSync(src);
|
|
25
33
|
|
|
26
34
|
for (const loc of list) {
|
|
27
|
-
|
|
35
|
+
chownSync(`${src}${sep}${loc}`, uid, gid);
|
|
28
36
|
}
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
fs.chownSync(src, uid, gid);
|
|
32
40
|
}
|
|
33
41
|
|
|
34
|
-
function
|
|
42
|
+
export function copySync(src: string, dir: string, umask: number) {
|
|
35
43
|
const stat = fs.statSync(src);
|
|
36
44
|
|
|
37
45
|
if (stat.isDirectory()) {
|
|
@@ -45,7 +53,7 @@ function copy(src: string, dir: string, umask: number) {
|
|
|
45
53
|
fs.mkdirSync(dir, mode);
|
|
46
54
|
|
|
47
55
|
for (const loc of list) {
|
|
48
|
-
|
|
56
|
+
copySync(`${src}${sep}${loc}`, dir, umask);
|
|
49
57
|
}
|
|
50
58
|
}
|
|
51
59
|
else {
|
|
@@ -56,14 +64,14 @@ function copy(src: string, dir: string, umask: number) {
|
|
|
56
64
|
}
|
|
57
65
|
}
|
|
58
66
|
|
|
59
|
-
function
|
|
60
|
-
const
|
|
67
|
+
export function removeSync(src: string) {
|
|
68
|
+
const stats = fs.statSync(src);
|
|
61
69
|
|
|
62
|
-
if (
|
|
70
|
+
if (stats.isDirectory()) {
|
|
63
71
|
const list = fs.readdirSync(src);
|
|
64
72
|
|
|
65
73
|
for (const loc of list) {
|
|
66
|
-
|
|
74
|
+
removeSync(`${src}${sep}${loc}`);
|
|
67
75
|
}
|
|
68
76
|
|
|
69
77
|
fs.rmdirSync(src);
|
|
@@ -73,14 +81,14 @@ function remove(src: string) {
|
|
|
73
81
|
}
|
|
74
82
|
}
|
|
75
83
|
|
|
76
|
-
function
|
|
84
|
+
export function mkdirSync(dir: string, umask: number) {
|
|
77
85
|
const mode = 0o777 - umask;
|
|
78
86
|
const cwd = process.cwd();
|
|
79
87
|
let use = '';
|
|
80
88
|
|
|
81
89
|
if (dir.indexOf(cwd) === 0) {
|
|
82
90
|
use = cwd;
|
|
83
|
-
dir = dir.
|
|
91
|
+
dir = dir.substring(cwd.length);
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
const ways = dir.split(sep).slice(1);
|
|
@@ -98,11 +106,3 @@ function mkdir(dir: string, umask: number) {
|
|
|
98
106
|
}
|
|
99
107
|
}
|
|
100
108
|
}
|
|
101
|
-
|
|
102
|
-
export default {
|
|
103
|
-
chmod,
|
|
104
|
-
chown,
|
|
105
|
-
copy,
|
|
106
|
-
remove,
|
|
107
|
-
mkdir
|
|
108
|
-
}
|
package/src/recurse-io.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
1
|
+
import fs, { NoParamCallback } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
export type NoParamCallback = fs.NoParamCallback;
|
|
4
|
+
type Files = Array<string>;
|
|
6
5
|
|
|
7
6
|
const { sep } = path;
|
|
8
7
|
|
|
9
|
-
function chmod(src: string, mode: number, callback: NoParamCallback) {
|
|
8
|
+
export function chmod(src: string, mode: number, callback: NoParamCallback) {
|
|
10
9
|
let reduce = 0;
|
|
11
10
|
|
|
12
|
-
fs.stat(src, (err,
|
|
11
|
+
fs.stat(src, (err, stats) => {
|
|
13
12
|
if (err) {
|
|
14
13
|
return callback(err);
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
if (
|
|
16
|
+
if (stats.isDirectory()) {
|
|
18
17
|
fs.readdir(src, (err, list) => {
|
|
19
18
|
if (err) {
|
|
20
19
|
return callback(err);
|
|
@@ -45,7 +44,7 @@ function chmod(src: string, mode: number, callback: NoParamCallback) {
|
|
|
45
44
|
});
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
function chown(src: string, uid: number, gid: number, callback: NoParamCallback) {
|
|
47
|
+
export function chown(src: string, uid: number, gid: number, callback: NoParamCallback) {
|
|
49
48
|
let reduce = 0;
|
|
50
49
|
|
|
51
50
|
fs.stat(src, (err, stats) => {
|
|
@@ -53,6 +52,14 @@ function chown(src: string, uid: number, gid: number, callback: NoParamCallback)
|
|
|
53
52
|
return callback(err);
|
|
54
53
|
}
|
|
55
54
|
|
|
55
|
+
if (uid === 0) {
|
|
56
|
+
uid = stats.uid;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (gid === 0) {
|
|
60
|
+
gid = stats.gid;
|
|
61
|
+
}
|
|
62
|
+
|
|
56
63
|
if (stats.isDirectory()) {
|
|
57
64
|
fs.readdir(src, (err, list) => {
|
|
58
65
|
if (err) {
|
|
@@ -84,7 +91,7 @@ function chown(src: string, uid: number, gid: number, callback: NoParamCallback)
|
|
|
84
91
|
});
|
|
85
92
|
}
|
|
86
93
|
|
|
87
|
-
function copy(src: string, dir: string, umask: number, callback: NoParamCallback) {
|
|
94
|
+
export function copy(src: string, dir: string, umask: number, callback: NoParamCallback) {
|
|
88
95
|
let reduce = 0;
|
|
89
96
|
|
|
90
97
|
fs.stat(src, (err, stat) => {
|
|
@@ -150,7 +157,7 @@ function copy(src: string, dir: string, umask: number, callback: NoParamCallback
|
|
|
150
157
|
});
|
|
151
158
|
}
|
|
152
159
|
|
|
153
|
-
function remove(src: string, callback: NoParamCallback) {
|
|
160
|
+
export function remove(src: string, callback: NoParamCallback) {
|
|
154
161
|
fs.stat(src, (err, stat) => {
|
|
155
162
|
if (err) {
|
|
156
163
|
return callback(err);
|
|
@@ -187,7 +194,7 @@ function remove(src: string, callback: NoParamCallback) {
|
|
|
187
194
|
});
|
|
188
195
|
}
|
|
189
196
|
|
|
190
|
-
function mkdir(dir: string, umask: number, callback: NoParamCallback) {
|
|
197
|
+
export function mkdir(dir: string, umask: number, callback: NoParamCallback) {
|
|
191
198
|
const cwd = process.cwd();
|
|
192
199
|
|
|
193
200
|
if (dir === cwd) {
|
|
@@ -218,7 +225,7 @@ function mkdir(dir: string, umask: number, callback: NoParamCallback) {
|
|
|
218
225
|
|
|
219
226
|
if (dir.indexOf(cwd) === 0) {
|
|
220
227
|
use = cwd;
|
|
221
|
-
dir = dir.
|
|
228
|
+
dir = dir.substring(cwd.length);
|
|
222
229
|
}
|
|
223
230
|
|
|
224
231
|
const files = dir.split(sep);
|
|
@@ -229,11 +236,3 @@ function mkdir(dir: string, umask: number, callback: NoParamCallback) {
|
|
|
229
236
|
iter.next();
|
|
230
237
|
iter.next(iter);
|
|
231
238
|
}
|
|
232
|
-
|
|
233
|
-
export default {
|
|
234
|
-
chmod,
|
|
235
|
-
chown,
|
|
236
|
-
copy,
|
|
237
|
-
remove,
|
|
238
|
-
mkdir
|
|
239
|
-
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
|
-
import { sep } from 'node:path';
|
|
3
2
|
import fs from 'node:fs';
|
|
4
3
|
import Chance from 'chance';
|
|
4
|
+
import { expect } from 'expect';
|
|
5
5
|
import { fmock, restore } from './__fmock';
|
|
6
|
-
import
|
|
6
|
+
import { pfs } from '../src';
|
|
7
7
|
|
|
8
8
|
describe('append(src, data [, options])', () => {
|
|
9
|
-
const pfs = new PoweredFileSystem();
|
|
10
9
|
const chance = new Chance();
|
|
11
10
|
|
|
12
11
|
beforeEach(() => {
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
|
-
import { sep } from 'node:path';
|
|
3
2
|
import fs from 'node:fs';
|
|
4
3
|
import Chance from 'chance';
|
|
4
|
+
import { expect } from 'expect';
|
|
5
5
|
import { fmock, restore } from './__fmock';
|
|
6
|
-
import
|
|
6
|
+
import { pfs, bitmask } from '../src';
|
|
7
7
|
|
|
8
8
|
describe('chmod(src, mode [, options])', () => {
|
|
9
|
-
const pfs = new PoweredFileSystem();
|
|
10
9
|
const chance = new Chance();
|
|
11
10
|
|
|
12
11
|
beforeEach(() => {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import Chance from 'chance';
|
|
4
|
+
import { expect } from 'expect';
|
|
4
5
|
import { fmock, restore } from './__fmock';
|
|
5
|
-
import
|
|
6
|
+
import { pfs } from '../src';
|
|
6
7
|
|
|
7
|
-
describe('chown(src,
|
|
8
|
-
const pfs = new PoweredFileSystem();
|
|
8
|
+
describe('chown(src, [, options])', () => {
|
|
9
9
|
const chance = new Chance();
|
|
10
10
|
|
|
11
11
|
beforeEach(() => {
|
|
@@ -25,8 +25,7 @@ describe('chown(src, uid, gid [, options])', () => {
|
|
|
25
25
|
|
|
26
26
|
it('Positive: Changes the permissions of a file', async () => {
|
|
27
27
|
const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
|
|
28
|
-
|
|
29
|
-
await pfs.chown('./tmpdir/tings.txt', uid, gid);
|
|
28
|
+
await pfs.chown('./tmpdir/tings.txt', { uid, gid });
|
|
30
29
|
|
|
31
30
|
assert(uid && gid);
|
|
32
31
|
});
|
|
@@ -34,8 +33,7 @@ describe('chown(src, uid, gid [, options])', () => {
|
|
|
34
33
|
|
|
35
34
|
it('Positive: Changes the permissions of a directory', async () => {
|
|
36
35
|
const { uid, gid } = fs.statSync('./tmpdir/digest');
|
|
37
|
-
|
|
38
|
-
await pfs.chown('./tmpdir/digest', uid, gid);
|
|
36
|
+
await pfs.chown('./tmpdir/digest', { uid, gid });
|
|
39
37
|
|
|
40
38
|
assert(uid && gid);
|
|
41
39
|
});
|
|
@@ -46,7 +44,7 @@ describe('chown(src, uid, gid [, options])', () => {
|
|
|
46
44
|
const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
|
|
47
45
|
|
|
48
46
|
await expect(async () => {
|
|
49
|
-
await pfs.chown(`./tmpdir/${guid}`, uid, gid);
|
|
47
|
+
await pfs.chown(`./tmpdir/${guid}`, { uid, gid });
|
|
50
48
|
})
|
|
51
49
|
.rejects
|
|
52
50
|
.toThrow();
|
|
@@ -56,8 +54,10 @@ describe('chown(src, uid, gid [, options])', () => {
|
|
|
56
54
|
it('[sync] Positive: Changes the permissions of a file', () => {
|
|
57
55
|
const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
|
|
58
56
|
|
|
59
|
-
pfs.chown('./tmpdir/tings.txt',
|
|
60
|
-
sync: true
|
|
57
|
+
pfs.chown('./tmpdir/tings.txt', {
|
|
58
|
+
sync: true,
|
|
59
|
+
uid,
|
|
60
|
+
gid
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
assert(uid && gid);
|
|
@@ -67,8 +67,10 @@ describe('chown(src, uid, gid [, options])', () => {
|
|
|
67
67
|
it('[sync] Positive: Changes the permissions of a directory', () => {
|
|
68
68
|
const { uid, gid } = fs.statSync('./tmpdir/digest');
|
|
69
69
|
|
|
70
|
-
pfs.chown('./tmpdir/digest',
|
|
71
|
-
sync: true
|
|
70
|
+
pfs.chown('./tmpdir/digest', {
|
|
71
|
+
sync: true,
|
|
72
|
+
uid,
|
|
73
|
+
gid
|
|
72
74
|
});
|
|
73
75
|
|
|
74
76
|
assert(uid && gid);
|
|
@@ -80,8 +82,10 @@ describe('chown(src, uid, gid [, options])', () => {
|
|
|
80
82
|
const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
|
|
81
83
|
|
|
82
84
|
assert.throws(() => {
|
|
83
|
-
pfs.chown(`./tmpdir/${guid}`,
|
|
84
|
-
sync: true
|
|
85
|
+
pfs.chown(`./tmpdir/${guid}`, {
|
|
86
|
+
sync: true,
|
|
87
|
+
uid,
|
|
88
|
+
gid
|
|
85
89
|
});
|
|
86
90
|
});
|
|
87
91
|
});
|