@augment-vir/node 31.51.0 → 31.52.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/augments/fs/walk-files.d.ts +50 -0
- package/dist/augments/fs/walk-files.js +40 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +5 -5
- package/src/augments/fs/walk-files.ts +88 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type MaybePromise, type PartialWithUndefined } from '@augment-vir/common';
|
|
2
|
+
import { type Dirent, type PathLike } from 'node:fs';
|
|
3
|
+
import { type FileHandle, type readFile } from 'node:fs/promises';
|
|
4
|
+
/**
|
|
5
|
+
* Output from `readfile`, used in {@link WalkFilesParams}.
|
|
6
|
+
*
|
|
7
|
+
* @category Package : @augment-vir/node
|
|
8
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
9
|
+
*/
|
|
10
|
+
export type ReadFileOutput = Awaited<ReturnType<typeof readFile>>;
|
|
11
|
+
/**
|
|
12
|
+
* Params for {@link walkFiles}.
|
|
13
|
+
*
|
|
14
|
+
* @category Package : @augment-vir/node
|
|
15
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
16
|
+
*/
|
|
17
|
+
export type WalkFilesParams = {
|
|
18
|
+
/** The directory to start walking files at. */
|
|
19
|
+
startDirPath: string;
|
|
20
|
+
/** Do something with a read file's contents. */
|
|
21
|
+
handleFileContents: (params: {
|
|
22
|
+
path: string;
|
|
23
|
+
contents: ReadFileOutput;
|
|
24
|
+
}) => MaybePromise<void>;
|
|
25
|
+
} & PartialWithUndefined<{
|
|
26
|
+
/**
|
|
27
|
+
* Check if a file should be read or a directory should be traversed.
|
|
28
|
+
*
|
|
29
|
+
* If this is not provided, every file will be read and every directory will be traversed.
|
|
30
|
+
*/
|
|
31
|
+
shouldRead: (params: {
|
|
32
|
+
path: string;
|
|
33
|
+
isDir: boolean;
|
|
34
|
+
}) => MaybePromise<boolean>;
|
|
35
|
+
/** Optional overrides for the internally used `node:fs/promises' imports. */
|
|
36
|
+
fs: {
|
|
37
|
+
readFile: (path: PathLike | FileHandle) => MaybePromise<ReadFileOutput>;
|
|
38
|
+
readdir: (path: PathLike, options: {
|
|
39
|
+
withFileTypes: true;
|
|
40
|
+
}) => MaybePromise<Pick<Dirent, 'name' | 'isDirectory'>[]>;
|
|
41
|
+
};
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* Walks files within a directory.
|
|
45
|
+
*
|
|
46
|
+
* @category Node : File
|
|
47
|
+
* @category Package : @augment-vir/node
|
|
48
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
49
|
+
*/
|
|
50
|
+
export declare function walkFiles({ handleFileContents, shouldRead, startDirPath, fs, }: Readonly<WalkFilesParams>): Promise<void>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { awaitedForEach } from '@augment-vir/common';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Walks files within a directory.
|
|
5
|
+
*
|
|
6
|
+
* @category Node : File
|
|
7
|
+
* @category Package : @augment-vir/node
|
|
8
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
9
|
+
*/
|
|
10
|
+
export async function walkFiles({ handleFileContents, shouldRead, startDirPath, fs, }) {
|
|
11
|
+
const { readFile, readdir } = {
|
|
12
|
+
/* node:coverage ignore next 2: dynamic imports are not branches. */
|
|
13
|
+
readFile: fs?.readFile || (await import('node:fs/promises')).readFile,
|
|
14
|
+
readdir: fs?.readdir || (await import('node:fs/promises')).readdir,
|
|
15
|
+
};
|
|
16
|
+
const children = await readdir(startDirPath, {
|
|
17
|
+
withFileTypes: true,
|
|
18
|
+
});
|
|
19
|
+
await awaitedForEach(children, async (file) => {
|
|
20
|
+
const childPath = join(startDirPath, file.name);
|
|
21
|
+
const isDir = file.isDirectory();
|
|
22
|
+
const willRead = shouldRead ? await shouldRead({ path: childPath, isDir }) : true;
|
|
23
|
+
if (!willRead) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (isDir) {
|
|
27
|
+
await walkFiles({
|
|
28
|
+
startDirPath: childPath,
|
|
29
|
+
shouldRead,
|
|
30
|
+
handleFileContents,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
await handleFileContents({
|
|
35
|
+
path: childPath,
|
|
36
|
+
contents: await readFile(childPath),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './augments/fs/json.js';
|
|
|
5
5
|
export * from './augments/fs/read-dir.js';
|
|
6
6
|
export * from './augments/fs/read-file.js';
|
|
7
7
|
export * from './augments/fs/symlink.js';
|
|
8
|
+
export * from './augments/fs/walk-files.js';
|
|
8
9
|
export * from './augments/fs/write.js';
|
|
9
10
|
export * from './augments/npm/find-bin-path.js';
|
|
10
11
|
export * from './augments/npm/npm-deps.js';
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from './augments/fs/json.js';
|
|
|
5
5
|
export * from './augments/fs/read-dir.js';
|
|
6
6
|
export * from './augments/fs/read-file.js';
|
|
7
7
|
export * from './augments/fs/symlink.js';
|
|
8
|
+
export * from './augments/fs/walk-files.js';
|
|
8
9
|
export * from './augments/fs/write.js';
|
|
9
10
|
export * from './augments/npm/find-bin-path.js';
|
|
10
11
|
export * from './augments/npm/npm-deps.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/node",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.52.0",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"test:update": "npm test"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@augment-vir/assert": "^31.
|
|
42
|
-
"@augment-vir/common": "^31.
|
|
41
|
+
"@augment-vir/assert": "^31.52.0",
|
|
42
|
+
"@augment-vir/common": "^31.52.0",
|
|
43
43
|
"@date-vir/duration": "^8.0.0",
|
|
44
44
|
"ansi-styles": "^6.2.3",
|
|
45
45
|
"sanitize-filename": "^1.6.3",
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"typed-event-target": "^4.1.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@augment-vir/test": "^31.
|
|
53
|
-
"@types/node": "^24.10.
|
|
52
|
+
"@augment-vir/test": "^31.52.0",
|
|
53
|
+
"@types/node": "^24.10.1",
|
|
54
54
|
"@web/dev-server-esbuild": "^1.0.4",
|
|
55
55
|
"@web/test-runner": "^0.20.2",
|
|
56
56
|
"@web/test-runner-commands": "^0.9.0",
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {awaitedForEach, type MaybePromise, type PartialWithUndefined} from '@augment-vir/common';
|
|
2
|
+
import {type Dirent, type PathLike} from 'node:fs';
|
|
3
|
+
import {type FileHandle, type readFile} from 'node:fs/promises';
|
|
4
|
+
import {join} from 'node:path';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Output from `readfile`, used in {@link WalkFilesParams}.
|
|
8
|
+
*
|
|
9
|
+
* @category Package : @augment-vir/node
|
|
10
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
11
|
+
*/
|
|
12
|
+
export type ReadFileOutput = Awaited<ReturnType<typeof readFile>>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Params for {@link walkFiles}.
|
|
16
|
+
*
|
|
17
|
+
* @category Package : @augment-vir/node
|
|
18
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
19
|
+
*/
|
|
20
|
+
export type WalkFilesParams = {
|
|
21
|
+
/** The directory to start walking files at. */
|
|
22
|
+
startDirPath: string;
|
|
23
|
+
/** Do something with a read file's contents. */
|
|
24
|
+
handleFileContents: (params: {path: string; contents: ReadFileOutput}) => MaybePromise<void>;
|
|
25
|
+
} & PartialWithUndefined<{
|
|
26
|
+
/**
|
|
27
|
+
* Check if a file should be read or a directory should be traversed.
|
|
28
|
+
*
|
|
29
|
+
* If this is not provided, every file will be read and every directory will be traversed.
|
|
30
|
+
*/
|
|
31
|
+
shouldRead: (params: {path: string; isDir: boolean}) => MaybePromise<boolean>;
|
|
32
|
+
/** Optional overrides for the internally used `node:fs/promises' imports. */
|
|
33
|
+
fs: {
|
|
34
|
+
readFile: (path: PathLike | FileHandle) => MaybePromise<ReadFileOutput>;
|
|
35
|
+
readdir: (
|
|
36
|
+
path: PathLike,
|
|
37
|
+
options: {withFileTypes: true},
|
|
38
|
+
) => MaybePromise<Pick<Dirent, 'name' | 'isDirectory'>[]>;
|
|
39
|
+
};
|
|
40
|
+
}>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Walks files within a directory.
|
|
44
|
+
*
|
|
45
|
+
* @category Node : File
|
|
46
|
+
* @category Package : @augment-vir/node
|
|
47
|
+
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
|
|
48
|
+
*/
|
|
49
|
+
export async function walkFiles({
|
|
50
|
+
handleFileContents,
|
|
51
|
+
shouldRead,
|
|
52
|
+
startDirPath,
|
|
53
|
+
fs,
|
|
54
|
+
}: Readonly<WalkFilesParams>): Promise<void> {
|
|
55
|
+
const {readFile, readdir} = {
|
|
56
|
+
/* node:coverage ignore next 2: dynamic imports are not branches. */
|
|
57
|
+
readFile: fs?.readFile || (await import('node:fs/promises')).readFile,
|
|
58
|
+
readdir: fs?.readdir || (await import('node:fs/promises')).readdir,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const children = await readdir(startDirPath, {
|
|
62
|
+
withFileTypes: true,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
await awaitedForEach(children, async (file) => {
|
|
66
|
+
const childPath = join(startDirPath, file.name);
|
|
67
|
+
const isDir = file.isDirectory();
|
|
68
|
+
|
|
69
|
+
const willRead = shouldRead ? await shouldRead({path: childPath, isDir}) : true;
|
|
70
|
+
|
|
71
|
+
if (!willRead) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (isDir) {
|
|
76
|
+
await walkFiles({
|
|
77
|
+
startDirPath: childPath,
|
|
78
|
+
shouldRead,
|
|
79
|
+
handleFileContents,
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
await handleFileContents({
|
|
83
|
+
path: childPath,
|
|
84
|
+
contents: await readFile(childPath),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './augments/fs/json.js';
|
|
|
5
5
|
export * from './augments/fs/read-dir.js';
|
|
6
6
|
export * from './augments/fs/read-file.js';
|
|
7
7
|
export * from './augments/fs/symlink.js';
|
|
8
|
+
export * from './augments/fs/walk-files.js';
|
|
8
9
|
export * from './augments/fs/write.js';
|
|
9
10
|
export * from './augments/npm/find-bin-path.js';
|
|
10
11
|
export * from './augments/npm/npm-deps.js';
|