@augment-vir/node 31.2.1 → 31.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.
@@ -1,6 +1,31 @@
1
1
  import { type MaybePromise } from '@augment-vir/common';
2
- export declare function findAncestor(currentPath: string, callback: (path: string) => Promise<boolean>): Promise<string | undefined>;
3
- export declare function findAncestor(currentPath: string, callback: (path: string) => boolean): string | undefined;
2
+ /**
3
+ * Find an ancestor file path that matches the given `callback`. If no matches are found all the way
4
+ * up until the system root, this returns `undefined`.
5
+ *
6
+ * @category Path : Node
7
+ * @category Package : @augment-vir/node
8
+ * @returns `undefined` if no matches are found.
9
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
10
+ */
11
+ export declare function findAncestor(currentPath: string, callback: (path: string) => Promise<boolean>): Promise<string | undefined>; /**
12
+ * Find an ancestor file path that matches the given `callback`. If no matches are found all the
13
+ * way up until the system root, this returns `undefined`.
14
+ *
15
+ * @category Path : Node
16
+ * @category Package : @augment-vir/node
17
+ * @returns `undefined` if no matches are found.
18
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
19
+ */
20
+ export declare function findAncestor(currentPath: string, callback: (path: string) => boolean): string | undefined; /**
21
+ * Find an ancestor file path that matches the given `callback`. If no matches are found all the
22
+ * way up until the system root, this returns `undefined`.
23
+ *
24
+ * @category Path : Node
25
+ * @category Package : @augment-vir/node
26
+ * @returns `undefined` if no matches are found.
27
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
28
+ */
4
29
  export declare function findAncestor(currentPath: string, callback: (path: string) => MaybePromise<boolean>): MaybePromise<string | undefined>;
5
30
  /**
6
31
  * Join a list of paths to the given `parentDirPath`. This is particularly useful for getting full
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Determines the path that will be imported into the given `startingPoint` from the given
3
+ * `importPath`. Resolves symlinks, tries to map `.js` extensions to `.ts` extensions when needed,
4
+ * and tries to find the best `node_modules` import for package imports.
5
+ *
6
+ * @category Path : Node
7
+ * @category Package : @augment-vir/node
8
+ * @returns `undefined` if no matches are found.
9
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
10
+ */
11
+ export declare function resolveImportPath(importerFilePath: string, importPath: string): string | undefined;
@@ -0,0 +1,78 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { filterMap, getObjectTypedEntries } from '@augment-vir/common';
3
+ import { existsSync, realpathSync } from 'node:fs';
4
+ import { dirname, join, resolve, sep } from 'node:path';
5
+ import { readTsconfig } from '../typescript/read-tsconfig.js';
6
+ import { findAncestor } from './ancestor.js';
7
+ import { replaceWithWindowsPathIfNeeded } from './os-path.js';
8
+ /**
9
+ * Determines the path that will be imported into the given `startingPoint` from the given
10
+ * `importPath`. Resolves symlinks, tries to map `.js` extensions to `.ts` extensions when needed,
11
+ * and tries to find the best `node_modules` import for package imports.
12
+ *
13
+ * @category Path : Node
14
+ * @category Package : @augment-vir/node
15
+ * @returns `undefined` if no matches are found.
16
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
17
+ */
18
+ export function resolveImportPath(importerFilePath, importPath) {
19
+ const foundTsconfig = readTsconfig(importerFilePath);
20
+ const mappedImportPath = foundTsconfig
21
+ ? mapImportPath(importPath, foundTsconfig.tsconfig, foundTsconfig.path)
22
+ : importPath;
23
+ if (mappedImportPath.startsWith(sep) || mappedImportPath.startsWith('/')) {
24
+ /** Absolute import */
25
+ return mapFilePath(replaceWithWindowsPathIfNeeded(mappedImportPath));
26
+ }
27
+ else if (mappedImportPath.startsWith('.')) {
28
+ /** Relative import */
29
+ return mapFilePath(resolve(dirname(importerFilePath), replaceWithWindowsPathIfNeeded(mappedImportPath)));
30
+ }
31
+ else {
32
+ /** Package import */
33
+ const isOrgPackage = mappedImportPath.startsWith('@');
34
+ const importPathParts = mappedImportPath.split('/');
35
+ const packagePath = isOrgPackage
36
+ ? join(...importPathParts.slice(0, 2))
37
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
38
+ importPathParts[0];
39
+ const relevantNodeModulesParent = findAncestor(importerFilePath, (ancestorPath) => {
40
+ const isMatch = existsSync(join(ancestorPath, 'node_modules', packagePath));
41
+ return isMatch;
42
+ });
43
+ if (!relevantNodeModulesParent) {
44
+ return undefined;
45
+ }
46
+ return mapFilePath(join(relevantNodeModulesParent, 'node_modules', replaceWithWindowsPathIfNeeded(mappedImportPath)));
47
+ }
48
+ }
49
+ function mapFilePath(path) {
50
+ if (existsSync(path)) {
51
+ return realpathSync(path);
52
+ }
53
+ const tsPath = path.replace(/\.js$/, '.ts');
54
+ if (path.endsWith('.js') && existsSync(tsPath)) {
55
+ return realpathSync(tsPath);
56
+ }
57
+ return path;
58
+ }
59
+ function mapImportPath(importPath, tsconfig, tsconfigPath) {
60
+ const paths = tsconfig.options.paths;
61
+ if (!paths) {
62
+ return importPath;
63
+ }
64
+ const mappedPaths = filterMap(getObjectTypedEntries(paths), ([alias, paths]) => {
65
+ const aliasRegex = new RegExp('^' + String(alias).replace(/\*/g, '(.*)') + '$');
66
+ const match = importPath.match(aliasRegex);
67
+ if (!match) {
68
+ return undefined;
69
+ }
70
+ /** An empty paths is invalid anyway. */
71
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72
+ return paths[0].replace(/\*/g, match[1]);
73
+ }, check.isTruthy);
74
+ if (!mappedPaths[0]) {
75
+ return importPath;
76
+ }
77
+ return resolve(tsconfig.options.baseUrl || dirname(tsconfigPath), mappedPaths[0]);
78
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Finds the closest `tsconfig.json` file and parses it.
3
+ *
4
+ * @category Path : Node
5
+ * @category Package : @augment-vir/node
6
+ * @returns `undefined` if no tsconfig was found or if a found tsconfig fails to parse.
7
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
8
+ */
9
+ export declare function readTsconfig(startingPath: string): {
10
+ tsconfig: import("typescript").ParsedCommandLine;
11
+ path: string;
12
+ } | undefined;
@@ -0,0 +1,32 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { parseJsonConfigFileContent, readConfigFile, sys } from 'typescript';
4
+ import { findAncestor } from '../path/ancestor.js';
5
+ /**
6
+ * Finds the closest `tsconfig.json` file and parses it.
7
+ *
8
+ * @category Path : Node
9
+ * @category Package : @augment-vir/node
10
+ * @returns `undefined` if no tsconfig was found or if a found tsconfig fails to parse.
11
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
12
+ */
13
+ export function readTsconfig(startingPath) {
14
+ const tsconfigDirPath = findAncestor(startingPath, (ancestorPath) => existsSync(join(ancestorPath, 'tsconfig.json')));
15
+ const tsconfigPath = tsconfigDirPath ? join(tsconfigDirPath, 'tsconfig.json') : undefined;
16
+ if (!tsconfigPath) {
17
+ return undefined;
18
+ }
19
+ // eslint-disable-next-line @typescript-eslint/unbound-method
20
+ const { config, error } = readConfigFile(tsconfigPath, sys.readFile);
21
+ if (error) {
22
+ return undefined;
23
+ }
24
+ const parsedConfig = parseJsonConfigFileContent(config, sys, dirname(tsconfigPath));
25
+ if (parsedConfig.errors.length) {
26
+ return undefined;
27
+ }
28
+ return {
29
+ tsconfig: parsedConfig,
30
+ path: tsconfigPath,
31
+ };
32
+ }
package/dist/index.d.ts CHANGED
@@ -11,9 +11,11 @@ export * from './augments/npm/read-package-json.js';
11
11
  export * from './augments/os/operating-system.js';
12
12
  export * from './augments/path/ancestor.js';
13
13
  export * from './augments/path/os-path.js';
14
+ export * from './augments/path/resolve-import.js';
14
15
  export * from './augments/path/root.js';
15
16
  export * from './augments/prisma.js';
16
17
  export * from './augments/terminal/question.js';
17
18
  export * from './augments/terminal/relevant-args.js';
18
19
  export * from './augments/terminal/run-cli-script.js';
19
20
  export * from './augments/terminal/shell.js';
21
+ export * from './augments/typescript/read-tsconfig.js';
package/dist/index.js CHANGED
@@ -11,9 +11,11 @@ export * from './augments/npm/read-package-json.js';
11
11
  export * from './augments/os/operating-system.js';
12
12
  export * from './augments/path/ancestor.js';
13
13
  export * from './augments/path/os-path.js';
14
+ export * from './augments/path/resolve-import.js';
14
15
  export * from './augments/path/root.js';
15
16
  export * from './augments/prisma.js';
16
17
  export * from './augments/terminal/question.js';
17
18
  export * from './augments/terminal/relevant-args.js';
18
19
  export * from './augments/terminal/run-cli-script.js';
19
20
  export * from './augments/terminal/shell.js';
21
+ export * from './augments/typescript/read-tsconfig.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/node",
3
- "version": "31.2.1",
3
+ "version": "31.3.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,27 +37,31 @@
37
37
  "test:update": "npm test"
38
38
  },
39
39
  "dependencies": {
40
- "@augment-vir/assert": "^31.2.1",
41
- "@augment-vir/common": "^31.2.1",
42
- "@date-vir/duration": "^7.0.1",
40
+ "@augment-vir/assert": "^31.3.0",
41
+ "@augment-vir/common": "^31.3.0",
42
+ "@date-vir/duration": "^7.1.1",
43
43
  "ansi-styles": "^6.2.1",
44
44
  "terminate": "^2.8.0",
45
- "type-fest": "^4.29.0",
45
+ "type-fest": "^4.31.0",
46
46
  "typed-event-target": "^4.0.2"
47
47
  },
48
48
  "devDependencies": {
49
- "@augment-vir/test": "^31.2.1",
50
- "@prisma/client": "^5.22.0",
51
- "@types/node": "^22.10.0",
49
+ "@augment-vir/test": "^31.3.0",
50
+ "@prisma/client": "^6.1.0",
51
+ "@types/node": "^22.10.2",
52
52
  "@web/dev-server-esbuild": "^1.0.3",
53
53
  "@web/test-runner": "^0.19.0",
54
54
  "@web/test-runner-commands": "^0.9.0",
55
55
  "@web/test-runner-playwright": "^0.11.0",
56
56
  "@web/test-runner-visual-regression": "^0.10.0",
57
- "c8": "^10.1.2",
58
- "concurrently": "^9.1.0",
57
+ "c8": "^10.1.3",
58
+ "concurrently": "^9.1.2",
59
59
  "istanbul-smart-text-reporter": "^1.1.5",
60
- "prisma": "^5.22.0"
60
+ "prisma": "^6.1.0",
61
+ "typescript": "^5.7.2"
62
+ },
63
+ "peerDependencies": {
64
+ "typescript": "*"
61
65
  },
62
66
  "engines": {
63
67
  "node": ">=22"
@@ -3,14 +3,39 @@ import {type MaybePromise} from '@augment-vir/common';
3
3
  import {dirname, join} from 'node:path';
4
4
  import {systemRootPath} from './root.js';
5
5
 
6
+ /**
7
+ * Find an ancestor file path that matches the given `callback`. If no matches are found all the way
8
+ * up until the system root, this returns `undefined`.
9
+ *
10
+ * @category Path : Node
11
+ * @category Package : @augment-vir/node
12
+ * @returns `undefined` if no matches are found.
13
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
14
+ */
6
15
  export function findAncestor(
7
16
  currentPath: string,
8
17
  callback: (path: string) => Promise<boolean>,
9
- ): Promise<string | undefined>;
18
+ ): Promise<string | undefined>; /**
19
+ * Find an ancestor file path that matches the given `callback`. If no matches are found all the
20
+ * way up until the system root, this returns `undefined`.
21
+ *
22
+ * @category Path : Node
23
+ * @category Package : @augment-vir/node
24
+ * @returns `undefined` if no matches are found.
25
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
26
+ */
10
27
  export function findAncestor(
11
28
  currentPath: string,
12
29
  callback: (path: string) => boolean,
13
- ): string | undefined;
30
+ ): string | undefined; /**
31
+ * Find an ancestor file path that matches the given `callback`. If no matches are found all the
32
+ * way up until the system root, this returns `undefined`.
33
+ *
34
+ * @category Path : Node
35
+ * @category Package : @augment-vir/node
36
+ * @returns `undefined` if no matches are found.
37
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
38
+ */
14
39
  export function findAncestor(
15
40
  currentPath: string,
16
41
  callback: (path: string) => MaybePromise<boolean>,
@@ -0,0 +1,116 @@
1
+ import {check} from '@augment-vir/assert';
2
+ import {filterMap, getObjectTypedEntries} from '@augment-vir/common';
3
+ import {existsSync, realpathSync} from 'node:fs';
4
+ import {dirname, join, resolve, sep} from 'node:path';
5
+ import {ParsedCommandLine} from 'typescript';
6
+ import {readTsconfig} from '../typescript/read-tsconfig.js';
7
+ import {findAncestor} from './ancestor.js';
8
+ import {replaceWithWindowsPathIfNeeded} from './os-path.js';
9
+
10
+ /**
11
+ * Determines the path that will be imported into the given `startingPoint` from the given
12
+ * `importPath`. Resolves symlinks, tries to map `.js` extensions to `.ts` extensions when needed,
13
+ * and tries to find the best `node_modules` import for package imports.
14
+ *
15
+ * @category Path : Node
16
+ * @category Package : @augment-vir/node
17
+ * @returns `undefined` if no matches are found.
18
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
19
+ */
20
+ export function resolveImportPath(
21
+ importerFilePath: string,
22
+ importPath: string,
23
+ ): string | undefined {
24
+ const foundTsconfig = readTsconfig(importerFilePath);
25
+
26
+ const mappedImportPath = foundTsconfig
27
+ ? mapImportPath(importPath, foundTsconfig.tsconfig, foundTsconfig.path)
28
+ : importPath;
29
+
30
+ if (mappedImportPath.startsWith(sep) || mappedImportPath.startsWith('/')) {
31
+ /** Absolute import */
32
+
33
+ return mapFilePath(replaceWithWindowsPathIfNeeded(mappedImportPath));
34
+ } else if (mappedImportPath.startsWith('.')) {
35
+ /** Relative import */
36
+ return mapFilePath(
37
+ resolve(dirname(importerFilePath), replaceWithWindowsPathIfNeeded(mappedImportPath)),
38
+ );
39
+ } else {
40
+ /** Package import */
41
+
42
+ const isOrgPackage = mappedImportPath.startsWith('@');
43
+ const importPathParts = mappedImportPath.split('/');
44
+
45
+ const packagePath: string = isOrgPackage
46
+ ? join(...importPathParts.slice(0, 2))
47
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
48
+ importPathParts[0]!;
49
+
50
+ const relevantNodeModulesParent = findAncestor(importerFilePath, (ancestorPath) => {
51
+ const isMatch = existsSync(join(ancestorPath, 'node_modules', packagePath));
52
+
53
+ return isMatch;
54
+ });
55
+
56
+ if (!relevantNodeModulesParent) {
57
+ return undefined;
58
+ }
59
+
60
+ return mapFilePath(
61
+ join(
62
+ relevantNodeModulesParent,
63
+ 'node_modules',
64
+ replaceWithWindowsPathIfNeeded(mappedImportPath),
65
+ ),
66
+ );
67
+ }
68
+ }
69
+
70
+ function mapFilePath(path: string): string {
71
+ if (existsSync(path)) {
72
+ return realpathSync(path);
73
+ }
74
+
75
+ const tsPath = path.replace(/\.js$/, '.ts');
76
+
77
+ if (path.endsWith('.js') && existsSync(tsPath)) {
78
+ return realpathSync(tsPath);
79
+ }
80
+
81
+ return path;
82
+ }
83
+
84
+ function mapImportPath(
85
+ importPath: string,
86
+ tsconfig: ParsedCommandLine,
87
+ tsconfigPath: string,
88
+ ): string {
89
+ const paths = tsconfig.options.paths;
90
+ if (!paths) {
91
+ return importPath;
92
+ }
93
+
94
+ const mappedPaths = filterMap(
95
+ getObjectTypedEntries(paths),
96
+ ([alias, paths]) => {
97
+ const aliasRegex = new RegExp('^' + String(alias).replace(/\*/g, '(.*)') + '$');
98
+ const match = importPath.match(aliasRegex);
99
+
100
+ if (!match) {
101
+ return undefined;
102
+ }
103
+
104
+ /** An empty paths is invalid anyway. */
105
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
106
+ return paths[0]!.replace(/\*/g, match[1]!);
107
+ },
108
+ check.isTruthy,
109
+ );
110
+
111
+ if (!mappedPaths[0]) {
112
+ return importPath;
113
+ }
114
+
115
+ return resolve(tsconfig.options.baseUrl || dirname(tsconfigPath), mappedPaths[0]);
116
+ }
@@ -0,0 +1,41 @@
1
+ import {existsSync} from 'node:fs';
2
+ import {dirname, join} from 'node:path';
3
+ import {parseJsonConfigFileContent, readConfigFile, sys} from 'typescript';
4
+ import {findAncestor} from '../path/ancestor.js';
5
+
6
+ /**
7
+ * Finds the closest `tsconfig.json` file and parses it.
8
+ *
9
+ * @category Path : Node
10
+ * @category Package : @augment-vir/node
11
+ * @returns `undefined` if no tsconfig was found or if a found tsconfig fails to parse.
12
+ * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
13
+ */
14
+ export function readTsconfig(startingPath: string) {
15
+ const tsconfigDirPath = findAncestor(startingPath, (ancestorPath) =>
16
+ existsSync(join(ancestorPath, 'tsconfig.json')),
17
+ );
18
+ const tsconfigPath = tsconfigDirPath ? join(tsconfigDirPath, 'tsconfig.json') : undefined;
19
+
20
+ if (!tsconfigPath) {
21
+ return undefined;
22
+ }
23
+
24
+ // eslint-disable-next-line @typescript-eslint/unbound-method
25
+ const {config, error} = readConfigFile(tsconfigPath, sys.readFile);
26
+
27
+ if (error) {
28
+ return undefined;
29
+ }
30
+
31
+ const parsedConfig = parseJsonConfigFileContent(config, sys, dirname(tsconfigPath));
32
+
33
+ if (parsedConfig.errors.length) {
34
+ return undefined;
35
+ }
36
+
37
+ return {
38
+ tsconfig: parsedConfig,
39
+ path: tsconfigPath,
40
+ };
41
+ }
package/src/index.ts CHANGED
@@ -11,9 +11,11 @@ export * from './augments/npm/read-package-json.js';
11
11
  export * from './augments/os/operating-system.js';
12
12
  export * from './augments/path/ancestor.js';
13
13
  export * from './augments/path/os-path.js';
14
+ export * from './augments/path/resolve-import.js';
14
15
  export * from './augments/path/root.js';
15
16
  export * from './augments/prisma.js';
16
17
  export * from './augments/terminal/question.js';
17
18
  export * from './augments/terminal/relevant-args.js';
18
19
  export * from './augments/terminal/run-cli-script.js';
19
20
  export * from './augments/terminal/shell.js';
21
+ export * from './augments/typescript/read-tsconfig.js';