@naturalcycles/nodejs-lib 15.135.0 → 15.136.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/dist/fs/fs2.d.ts CHANGED
@@ -45,8 +45,18 @@ declare class FS2 {
45
45
  ensureFileAsync(filePath: string): Promise<void>;
46
46
  removePath(fileOrDirPath: string, opt?: RmOptions): void;
47
47
  removePathAsync(fileOrDirPath: string, opt?: RmOptions): Promise<void>;
48
- emptyDir(dirPath: string): void;
49
- emptyDirAsync(dirPath: string): Promise<void>;
48
+ /**
49
+ * Ensures that the directory exists and is empty.
50
+ * Creates it (recursively) if it doesn't exist, otherwise deletes all its contents.
51
+ * The directory itself is not deleted.
52
+ */
53
+ ensureEmptyDir(dirPath: string): void;
54
+ /**
55
+ * Ensures that the directory exists and is empty.
56
+ * Creates it (recursively) if it doesn't exist, otherwise deletes all its contents.
57
+ * The directory itself is not deleted.
58
+ */
59
+ ensureEmptyDirAsync(dirPath: string): Promise<void>;
50
60
  /**
51
61
  * Cautious, underlying Node function is currently Experimental.
52
62
  */
package/dist/fs/fs2.js CHANGED
@@ -196,12 +196,19 @@ class FS2 {
196
196
  async removePathAsync(fileOrDirPath, opt) {
197
197
  await fsp.rm(fileOrDirPath, { recursive: true, force: true, ...opt });
198
198
  }
199
- emptyDir(dirPath) {
199
+ /**
200
+ * Ensures that the directory exists and is empty.
201
+ * Creates it (recursively) if it doesn't exist, otherwise deletes all its contents.
202
+ * The directory itself is not deleted.
203
+ */
204
+ ensureEmptyDir(dirPath) {
200
205
  let items;
201
206
  try {
202
207
  items = fs.readdirSync(dirPath);
203
208
  }
204
- catch {
209
+ catch (err) {
210
+ if (err?.code !== 'ENOENT')
211
+ throw err;
205
212
  this.ensureDir(dirPath);
206
213
  return;
207
214
  }
@@ -209,12 +216,19 @@ class FS2 {
209
216
  this.removePath(path.join(dirPath, item));
210
217
  }
211
218
  }
212
- async emptyDirAsync(dirPath) {
219
+ /**
220
+ * Ensures that the directory exists and is empty.
221
+ * Creates it (recursively) if it doesn't exist, otherwise deletes all its contents.
222
+ * The directory itself is not deleted.
223
+ */
224
+ async ensureEmptyDirAsync(dirPath) {
213
225
  let items;
214
226
  try {
215
227
  items = await fsp.readdir(dirPath);
216
228
  }
217
- catch {
229
+ catch (err) {
230
+ if (err?.code !== 'ENOENT')
231
+ throw err;
218
232
  return await this.ensureDirAsync(dirPath);
219
233
  }
220
234
  await Promise.all(items.map(item => this.removePathAsync(path.join(dirPath, item))));
@@ -36,7 +36,7 @@ export function writableChunk(splitPredicate, fn, opt = {}) {
36
36
  logger.debug(`WritableChunk(${splitIndex}): pause`);
37
37
  }
38
38
  if (splitPredicate(chunk, ++indexWritten)) {
39
- logger.log(`WritableChunk(${splitIndex}): splitting to ${splitIndex + 1}`);
39
+ logger.debug(`WritableChunk(${splitIndex}): splitting to ${splitIndex + 1}`);
40
40
  splitIndex++;
41
41
  fork.push(null);
42
42
  lock?.resolve();
@@ -47,7 +47,7 @@ export function writableChunk(splitPredicate, fn, opt = {}) {
47
47
  cb();
48
48
  },
49
49
  async final(cb) {
50
- logger.log(`WritableChunk: final`);
50
+ logger.debug(`WritableChunk: final`);
51
51
  // Pushing null "closes"/ends the secondary pipeline correctly
52
52
  fork.push(null);
53
53
  // Acknowledge that we've received `null` and passed it through to the fork
@@ -67,7 +67,7 @@ export function writableChunk(splitPredicate, fn, opt = {}) {
67
67
  lockCopy.resolve();
68
68
  });
69
69
  void fn(Pipeline.from(readable), currentSplitIndex).then(() => {
70
- logger.log(`WritableChunk(${currentSplitIndex}): done`);
70
+ logger.debug(`WritableChunk(${currentSplitIndex}): done`);
71
71
  });
72
72
  return readable;
73
73
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.135.0",
4
+ "version": "15.136.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -14,7 +14,7 @@
14
14
  "yaml": "^2"
15
15
  },
16
16
  "devDependencies": {
17
- "@naturalcycles/dev-lib": "0.0.0",
17
+ "@naturalcycles/dev-lib": "20.69.0",
18
18
  "typescript": "^7"
19
19
  },
20
20
  "exports": {
package/src/fs/fs2.ts CHANGED
@@ -230,11 +230,17 @@ class FS2 {
230
230
  await fsp.rm(fileOrDirPath, { recursive: true, force: true, ...opt })
231
231
  }
232
232
 
233
- emptyDir(dirPath: string): void {
233
+ /**
234
+ * Ensures that the directory exists and is empty.
235
+ * Creates it (recursively) if it doesn't exist, otherwise deletes all its contents.
236
+ * The directory itself is not deleted.
237
+ */
238
+ ensureEmptyDir(dirPath: string): void {
234
239
  let items: string[]
235
240
  try {
236
241
  items = fs.readdirSync(dirPath)
237
- } catch {
242
+ } catch (err) {
243
+ if ((err as any)?.code !== 'ENOENT') throw err
238
244
  this.ensureDir(dirPath)
239
245
  return
240
246
  }
@@ -244,11 +250,17 @@ class FS2 {
244
250
  }
245
251
  }
246
252
 
247
- async emptyDirAsync(dirPath: string): Promise<void> {
253
+ /**
254
+ * Ensures that the directory exists and is empty.
255
+ * Creates it (recursively) if it doesn't exist, otherwise deletes all its contents.
256
+ * The directory itself is not deleted.
257
+ */
258
+ async ensureEmptyDirAsync(dirPath: string): Promise<void> {
248
259
  let items: string[]
249
260
  try {
250
261
  items = await fsp.readdir(dirPath)
251
- } catch {
262
+ } catch (err) {
263
+ if ((err as any)?.code !== 'ENOENT') throw err
252
264
  return await this.ensureDirAsync(dirPath)
253
265
  }
254
266
 
@@ -48,7 +48,7 @@ export function writableChunk<T>(
48
48
  }
49
49
 
50
50
  if (splitPredicate(chunk, ++indexWritten)) {
51
- logger.log(`WritableChunk(${splitIndex}): splitting to ${splitIndex + 1}`)
51
+ logger.debug(`WritableChunk(${splitIndex}): splitting to ${splitIndex + 1}`)
52
52
  splitIndex++
53
53
  fork.push(null)
54
54
  lock?.resolve()
@@ -60,7 +60,7 @@ export function writableChunk<T>(
60
60
  cb()
61
61
  },
62
62
  async final(cb) {
63
- logger.log(`WritableChunk: final`)
63
+ logger.debug(`WritableChunk: final`)
64
64
 
65
65
  // Pushing null "closes"/ends the secondary pipeline correctly
66
66
  fork.push(null)
@@ -84,7 +84,7 @@ export function writableChunk<T>(
84
84
  })
85
85
 
86
86
  void fn(Pipeline.from<T>(readable), currentSplitIndex).then(() => {
87
- logger.log(`WritableChunk(${currentSplitIndex}): done`)
87
+ logger.debug(`WritableChunk(${currentSplitIndex}): done`)
88
88
  })
89
89
 
90
90
  return readable