pwd-fs 3.2.3 → 3.3.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/src/recurse-io.ts CHANGED
@@ -1,17 +1,11 @@
1
1
  import fs, { NoParamCallback } from 'node:fs';
2
2
  import path from 'node:path';
3
3
 
4
- type Files = Array<string>;
5
-
6
- const { sep } = path;
7
-
8
4
  export function chmod(src: string, mode: number, callback: NoParamCallback) {
9
5
  let reduce = 0;
10
6
 
11
7
  fs.stat(src, (err, stats) => {
12
- if (err) {
13
- return callback(err);
14
- }
8
+ if (err) return callback(err);
15
9
 
16
10
  if (stats.isDirectory()) {
17
11
  fs.readdir(src, (err, list) => {
@@ -23,10 +17,10 @@ export function chmod(src: string, mode: number, callback: NoParamCallback) {
23
17
  return fs.chmod(src, mode, callback);
24
18
  }
25
19
 
26
- reduce += list.length;
20
+ reduce = list.length;
27
21
 
28
22
  for (const loc of list) {
29
- chmod(`${src}${sep}${loc}`, mode, (err) => {
23
+ chmod(path.join(src, loc), mode, (err) => {
30
24
  if (err) {
31
25
  return callback(err);
32
26
  }
@@ -65,15 +59,15 @@ export function chown(src: string, uid: number, gid: number, callback: NoParamCa
65
59
  if (err) {
66
60
  return callback(err);
67
61
  }
68
-
62
+
69
63
  if (list.length === 0) {
70
64
  return fs.chown(src, uid, gid, callback);
71
65
  }
72
66
 
73
- reduce += list.length;
67
+ reduce = list.length;
74
68
 
75
69
  for (const loc of list) {
76
- chown(`${src}${sep}${loc}`, uid, gid, (err) => {
70
+ chown(path.join(src, loc), uid, gid, (err) => {
77
71
  if (err) {
78
72
  return callback(err);
79
73
  }
@@ -92,8 +86,6 @@ export function chown(src: string, uid: number, gid: number, callback: NoParamCa
92
86
  }
93
87
 
94
88
  export function copy(src: string, dir: string, umask: number, callback: NoParamCallback) {
95
- let reduce = 0;
96
-
97
89
  fs.stat(src, (err, stat) => {
98
90
  if (err) {
99
91
  return callback(err);
@@ -105,25 +97,27 @@ export function copy(src: string, dir: string, umask: number, callback: NoParamC
105
97
  return callback(err);
106
98
  }
107
99
 
108
- reduce += list.length;
109
-
110
- const paths = src.split(sep);
111
- const loc = paths[paths.length - 1];
100
+ const loc = path.basename(src);
101
+ const destDir = path.join(dir, loc);
112
102
  const mode = 0o777 - umask;
113
103
 
114
- dir = `${dir}${sep}${loc}`;
115
-
116
- fs.mkdir(dir, { mode }, (err) => {
104
+ fs.mkdir(destDir, { mode }, (err) => {
117
105
  if (err) {
106
+ if (err.code === 'EEXIST') {
107
+ err = new Error(`Target already exists: ${destDir}`);
108
+ }
109
+
118
110
  return callback(err);
119
111
  }
120
112
 
121
- if (reduce === 0) {
113
+ if (list.length === 0) {
122
114
  return callback(null);
123
115
  }
124
116
 
125
- for (const loc of list) {
126
- copy(`${src}${sep}${loc}`, dir, umask, (err) => {
117
+ let reduce = list.length;
118
+
119
+ for (const item of list) {
120
+ copy(path.join(src, item), destDir, umask, (err) => {
127
121
  if (err) {
128
122
  return callback(err);
129
123
  }
@@ -137,20 +131,16 @@ export function copy(src: string, dir: string, umask: number, callback: NoParamC
137
131
  });
138
132
  }
139
133
  else {
140
- const mode = 0o666 - umask;
141
134
  const loc = path.basename(src);
135
+ const dest = path.join(dir, loc);
136
+ const mode = 0o666 - umask;
142
137
 
143
138
  const readStream = fs.createReadStream(src);
144
- const writeStream = fs.createWriteStream(`${dir}${sep}${loc}`, {
145
- mode
146
- });
139
+ const writeStream = fs.createWriteStream(dest, { mode });
147
140
 
148
141
  readStream.on('error', callback);
149
142
  writeStream.on('error', callback);
150
-
151
- writeStream.on('close', () => {
152
- callback(null);
153
- });
143
+ writeStream.on('close', () => callback(null));
154
144
 
155
145
  readStream.pipe(writeStream);
156
146
  }
@@ -168,15 +158,15 @@ export function remove(src: string, callback: NoParamCallback) {
168
158
  if (err) {
169
159
  return callback(err);
170
160
  }
171
-
172
- let reduce = list.length;
173
161
 
174
- if (reduce === 0) {
162
+ if (list.length === 0) {
175
163
  return fs.rmdir(src, callback);
176
164
  }
177
165
 
166
+ let reduce = list.length;
167
+
178
168
  for (const loc of list) {
179
- remove(`${src}${sep}${loc}`, (err) => {
169
+ remove(path.join(src, loc), (err) => {
180
170
  if (err) {
181
171
  return callback(err);
182
172
  }
@@ -201,38 +191,31 @@ export function mkdir(dir: string, umask: number, callback: NoParamCallback) {
201
191
  return callback(null);
202
192
  }
203
193
 
204
- const sequence = function* (dir: string, files: Files, mode: number): Generator<void, void, Generator> {
205
- const iter = yield;
206
-
207
- for (const item of files) {
208
- dir += `${sep}${item}`;
194
+ let base = '';
195
+ const mode = 0o777 - umask;
209
196
 
210
- fs.mkdir(dir, { mode }, (err) => {
211
- if (err && err.errno !== -17) {
212
- return callback(err);
213
- }
197
+ if (dir.startsWith(cwd)) {
198
+ base = cwd;
199
+ dir = dir.slice(cwd.length);
200
+ }
214
201
 
215
- iter.next();
216
- });
202
+ const parts = dir.split(path.sep).filter(Boolean);
217
203
 
218
- yield;
204
+ function next(index: number) {
205
+ if (index >= parts.length) {
206
+ return callback(null);
219
207
  }
220
208
 
221
- callback(null);
222
- };
209
+ base = path.join(base, parts[index]);
223
210
 
224
- let use = '';
211
+ fs.mkdir(base, { mode }, (err) => {
212
+ if (err && err.code !== 'EEXIST') {
213
+ return callback(err);
214
+ }
225
215
 
226
- if (dir.indexOf(cwd) === 0) {
227
- use = cwd;
228
- dir = dir.substring(cwd.length);
216
+ next(index + 1);
217
+ });
229
218
  }
230
219
 
231
- const files = dir.split(sep);
232
- const mode = 0o777 - umask;
233
-
234
- const iter = sequence(use, files, mode);
235
-
236
- iter.next();
237
- iter.next(iter);
220
+ next(0);
238
221
  }
@@ -30,20 +30,6 @@ describe('append(src, data [, options])', () => {
30
30
 
31
31
  assert(payload.length + 6 === size);
32
32
  });
33
-
34
-
35
- it(`Negative: Unexpected option 'flag' returns Error`, async () => {
36
- const payload = chance.paragraph();
37
-
38
- await expect(async () => {
39
- await pfs.append('./tmpdir/tings.txt', payload, {
40
- flag: 'r'
41
- });
42
- })
43
- .rejects
44
- .toThrow();
45
- });
46
-
47
33
 
48
34
  it(`[sync] Positive: Must append content to file`, () => {
49
35
  const payload = chance.paragraph();
@@ -56,16 +42,4 @@ describe('append(src, data [, options])', () => {
56
42
 
57
43
  assert(payload.length + 6 === size);
58
44
  });
59
-
60
-
61
- it(`[sync] Negative: Unexpected option 'flag' returns Error`, () => {
62
- const payload = chance.paragraph();
63
-
64
- assert.throws(() => {
65
- pfs.append('./tmpdir/tings.txt', payload, {
66
- sync: true,
67
- flag: 'r'
68
- });
69
- });
70
- });
71
45
  });
package/tsconfig.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "lib": [
4
- "es2022"
5
- ],
6
- "module": "commonjs",
7
- "target": "es2022",
8
- "moduleResolution": "node",
9
- "resolveJsonModule": true,
4
+ "ES2022", "DOM"
5
+ ],
6
+ "target": "es2022",
7
+ "module": "commonjs",
8
+ "moduleResolution": "node",
10
9
  "esModuleInterop": true,
10
+ "emitDecoratorMetadata": true,
11
+ "experimentalDecorators": true,
12
+ "resolveJsonModule": true,
11
13
  "declaration": true,
14
+ "sourceMap": true,
12
15
  "outDir": "./dist"
13
16
  }
14
- }
17
+ }