@augment-vir/node 30.6.2 → 30.7.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.
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Nested contents read from a file system directory.
3
+ *
4
+ * @category Node : File
5
+ * @category Package : @augment-vir/node
6
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
7
+ */
8
+ export type DirContents = {
9
+ [Path in string]: string | DirContents;
10
+ };
11
+ /**
12
+ * Read all contents within a directory and store them in an object. Optionally recursive.
13
+ *
14
+ * @category Node : File
15
+ * @category Package : @augment-vir/node
16
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
17
+ */
18
+ export declare function readAllDirContents(dir: string, { recursive, excludeList, }: {
19
+ recursive?: boolean;
20
+ excludeList?: ReadonlyArray<string | RegExp> | undefined;
21
+ }): Promise<DirContents>;
22
+ /**
23
+ * Deletes and entire directory and resets it to the given contents.
24
+ *
25
+ * @category Node : File
26
+ * @category Package : @augment-vir/node
27
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
28
+ */
29
+ export declare function resetDirContents(rootDir: string, contents: Readonly<DirContents>): Promise<void>;
30
+ /**
31
+ * Write {@link DirContents} to a directory.
32
+ *
33
+ * @category Node : File
34
+ * @category Package : @augment-vir/node
35
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
36
+ */
37
+ export declare function writeDirContents(rootDir: string, contents: Readonly<DirContents>): Promise<void>;
@@ -0,0 +1,77 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { getObjectTypedEntries } from '@augment-vir/common';
3
+ import { readdir, readFile, rm, stat } from 'node:fs/promises';
4
+ import { join } from 'node:path';
5
+ import { writeFileAndDir } from './write.js';
6
+ /**
7
+ * Read all contents within a directory and store them in an object. Optionally recursive.
8
+ *
9
+ * @category Node : File
10
+ * @category Package : @augment-vir/node
11
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
12
+ */
13
+ export async function readAllDirContents(dir, { recursive = false, excludeList, }) {
14
+ const fileNames = (await readdir(dir)).sort();
15
+ const allFileContents = await Promise.all(fileNames.map(async (fileName) => {
16
+ const filePath = join(dir, fileName);
17
+ if (excludeList?.some((excludeItem) => {
18
+ if (check.isString(excludeItem)) {
19
+ return filePath.includes(excludeItem);
20
+ }
21
+ else {
22
+ return filePath.match(excludeItem);
23
+ }
24
+ })) {
25
+ return undefined;
26
+ }
27
+ const isFile = (await stat(filePath)).isFile();
28
+ const contents = isFile
29
+ ? (await readFile(filePath)).toString()
30
+ : recursive
31
+ ? await readAllDirContents(filePath, { recursive, excludeList })
32
+ : undefined;
33
+ if (check.isObject(contents) && !Object.keys(contents).length) {
34
+ return undefined;
35
+ }
36
+ return contents;
37
+ }));
38
+ return fileNames.reduce((accum, fileName, index) => {
39
+ if (allFileContents[index] != undefined) {
40
+ const fileContents = allFileContents[index];
41
+ accum[fileName] = fileContents;
42
+ }
43
+ return accum;
44
+ }, {});
45
+ }
46
+ /**
47
+ * Deletes and entire directory and resets it to the given contents.
48
+ *
49
+ * @category Node : File
50
+ * @category Package : @augment-vir/node
51
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
52
+ */
53
+ export async function resetDirContents(rootDir, contents) {
54
+ await rm(rootDir, {
55
+ force: true,
56
+ recursive: true,
57
+ });
58
+ await writeDirContents(rootDir, contents);
59
+ }
60
+ /**
61
+ * Write {@link DirContents} to a directory.
62
+ *
63
+ * @category Node : File
64
+ * @category Package : @augment-vir/node
65
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
66
+ */
67
+ export async function writeDirContents(rootDir, contents) {
68
+ await Promise.all(getObjectTypedEntries(contents).map(async ([relativePath, content,]) => {
69
+ const fullPath = join(rootDir, relativePath);
70
+ if (check.isString(content)) {
71
+ await writeFileAndDir(fullPath, content);
72
+ }
73
+ else {
74
+ await writeDirContents(fullPath, content);
75
+ }
76
+ }));
77
+ }
@@ -2,6 +2,7 @@ export declare const monoRepoDirPath: string;
2
2
  export declare const notCommittedDirPath: string;
3
3
  export declare const nodePackageDir: string;
4
4
  export declare const testFilesDir: string;
5
+ export declare const dirContentsTestDir: string;
5
6
  export declare const longRunningFilePath: string;
6
7
  export declare const longRunningFileWithStderr: string;
7
8
  export declare const workspaceQueryDir: string;
@@ -4,6 +4,7 @@ export const notCommittedDirPath = join(monoRepoDirPath, '.not-committed');
4
4
  export const nodePackageDir = dirname(import.meta.dirname);
5
5
  export const testFilesDir = join(nodePackageDir, 'test-files');
6
6
  const longRunningFileDir = join(testFilesDir, 'long-running-test-file');
7
+ export const dirContentsTestDir = join(testFilesDir, 'dir-contents-test');
7
8
  export const longRunningFilePath = join(longRunningFileDir, 'long-running-file.ts');
8
9
  export const longRunningFileWithStderr = join(longRunningFileDir, 'long-running-file-with-stderr.ts');
9
10
  export const workspaceQueryDir = join(testFilesDir, 'workspace-query');
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './augments/docker.js';
2
+ export * from './augments/fs/dir-contents.js';
2
3
  export * from './augments/fs/download.js';
3
4
  export * from './augments/fs/json.js';
4
5
  export * from './augments/fs/read-dir.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './augments/docker.js';
2
+ export * from './augments/fs/dir-contents.js';
2
3
  export * from './augments/fs/download.js';
3
4
  export * from './augments/fs/json.js';
4
5
  export * from './augments/fs/read-dir.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/node",
3
- "version": "30.6.2",
3
+ "version": "30.7.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",
@@ -37,16 +37,16 @@
37
37
  "test:update": "npm test"
38
38
  },
39
39
  "dependencies": {
40
- "@augment-vir/assert": "^30.6.2",
41
- "@augment-vir/common": "^30.6.2",
42
- "@date-vir/duration": "^6.0.1",
40
+ "@augment-vir/assert": "^30.7.0",
41
+ "@augment-vir/common": "^30.7.0",
42
+ "@date-vir/duration": "^7.0.1",
43
43
  "ansi-styles": "^6.2.1",
44
44
  "terminate": "^2.8.0",
45
45
  "type-fest": "^4.26.1",
46
46
  "typed-event-target": "^4.0.2"
47
47
  },
48
48
  "devDependencies": {
49
- "@augment-vir/test": "^30.6.2",
49
+ "@augment-vir/test": "^30.7.0",
50
50
  "@prisma/client": "^5.22.0",
51
51
  "@types/node": "^22.9.0",
52
52
  "@web/dev-server-esbuild": "^1.0.3",