@rushstack/trace-import 0.0.0 → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ @rushstack/trace-import
2
+
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # @microsoft/trace-import
2
+
3
+ > 🚨 *EARLY PREVIEW RELEASE* 🚨
4
+ >
5
+ > Not all features are implemented yet. To provide suggestions, please
6
+ > [create a GitHub issue](https://github.com/microsoft/rushstack/issues/new/choose).
7
+ > If you have questions, see the [Rush Stack Help page](https://rushstack.io/pages/help/support/)
8
+ > for support resources.
9
+
10
+ The `trace-import` command line tool helps you:
11
+
12
+ - Analyze `import`/`require()` statements to understand why they aren't resolving correctly
13
+ - Understand the relationships between package folders in your `node_modules` tree
14
+ - Ensure that `package.json` files correctly export their .js and .d.ts entry points
15
+
16
+ ## Usage
17
+
18
+ It's recommended to install this package globally:
19
+
20
+ ```
21
+ # Install the NPM package
22
+ npm install -g @rushstack/trace-import
23
+
24
+ # View the command-line help
25
+ trace-import --help
26
+ ```
27
+
28
+ ## Command line
29
+
30
+ ```
31
+ usage: trace-import [-h] [-d] -p IMPORT_PATH [-b FOLDER_PATH] [-t {cjs,es,ts}]
32
+
33
+ This tool analyzes import module paths, to determine the resolved target
34
+ folder. For example, if the "semver" NPM package is installed, "trace-import
35
+ --path semver/index" will print output equivalent to the Node.js require.
36
+ resolve() API. If "@types/semver" is installed, then "trace-import
37
+ --resolution-type ts --path semver/index" will print the .d.ts file path that
38
+ would be resolved by a TypeScript import statement.
39
+
40
+ Optional arguments:
41
+ -h, --help Show this help message and exit.
42
+ -d, --debug Show the full call stack if an error occurs while
43
+ executing the tool
44
+ -p IMPORT_PATH, --path IMPORT_PATH
45
+ The import module path to be analyzed. For example,
46
+ "example" in expressions such as: require("example");
47
+ require.resolve("example"); import { Thing } from
48
+ "example";
49
+ -b FOLDER_PATH, --base-folder FOLDER_PATH
50
+ The "--path" string will be resolved as if the import
51
+ statement appeared in a script located in this folder.
52
+ If omitted, the current working directory is used.
53
+ -t {cjs,es,ts}, --resolution-type {cjs,es,ts}
54
+ The type of module resolution to perform: "cjs" for
55
+ CommonJS, "es" for ES modules, or "ts" for TypeScript
56
+ typings. The default value is "cjs".
57
+ ```
58
+
59
+ ## Sample outputs
60
+
61
+ These commands were invoked in the `C:\Git\rushstack\apps\trace-import` folder
62
+ where trace-import is developed.
63
+
64
+ ### Resolving a CommonJS main index
65
+ ```
66
+ trace-import --path semver
67
+ ```
68
+
69
+ Sample output:
70
+ ```
71
+ Base folder: C:\Git\rushstack\apps\trace-import
72
+ Package name: semver
73
+ Package subpath: (not specified)
74
+
75
+ Resolving...
76
+
77
+ Package folder: C:\Git\rushstack\common\temp\node_modules\.pnpm\semver@7.3.8\node_modules\semver
78
+ package.json: semver (7.3.8)
79
+ Main index: "main": "index.js"
80
+
81
+ Target path: C:\Git\rushstack\common\temp\node_modules\.pnpm\semver@7.3.8\node_modules\semver\index.js
82
+ ```
83
+
84
+ ### Resolving a CommonJS package subpath
85
+ ```
86
+ trace-import --path typescript/bin/tsc
87
+ ```
88
+
89
+ Sample output:
90
+ ```
91
+ Base folder: C:\Git\rushstack\apps\trace-import
92
+ Package name: typescript
93
+ Package subpath: bin/tsc
94
+
95
+ Resolving...
96
+
97
+ Package folder: C:\Git\rushstack\common\temp\node_modules\.pnpm\typescript@4.8.4\node_modules\typescript
98
+ package.json: typescript (4.8.4)
99
+
100
+ Target path: C:\Git\rushstack\common\temp\node_modules\.pnpm\typescript@4.8.4\node_modules\typescript\bin\tsc
101
+ ```
102
+
103
+ ### Resolving a TypeScript declaration
104
+ ```
105
+ trace-import --resolution-type ts --path semver
106
+ ```
107
+
108
+ Sample output:
109
+ ```
110
+ Base folder: C:\Git\rushstack\apps\trace-import
111
+ Package name: semver
112
+ Package subpath: (not specified)
113
+
114
+ Resolving...
115
+
116
+ Package folder: C:\Git\rushstack\common\temp\node_modules\.pnpm\semver@7.3.8\node_modules\semver
117
+ package.json: semver (7.3.8)
118
+ @types folder: C:\Git\rushstack\common\temp\node_modules\.pnpm\@types+semver@7.3.5\node_modules\@types\semver
119
+ @types package.json: @types/semver (7.3.5)
120
+ @types main index: "types": "index.d.ts"
121
+
122
+ Target path: C:\Git\rushstack\common\temp\node_modules\.pnpm\@types+semver@7.3.5\node_modules\@types\semver\index.d.ts
123
+ ```
124
+
125
+ ### Resolving a relative path
126
+ ```
127
+ trace-import --path ./config/rig.json
128
+ ```
129
+
130
+ Sample output:
131
+ ```
132
+ Base folder: C:\Git\rushstack\apps\trace-import
133
+ Import path: ./config/rig.json
134
+
135
+ The import path does not appear to reference an NPM package.
136
+ Resolving...
137
+
138
+ Target path: C:\Git\rushstack\apps\trace-import\config\rig.json
139
+ ```
140
+
141
+ ## Links
142
+
143
+ - [CHANGELOG.md](
144
+ https://github.com/microsoft/rushstack/blob/main/apps/trace-import/CHANGELOG.md) - Find
145
+ out what's new in the latest version
146
+ - [Rush Lockfile Explorer](https://lfx.rushstack.io) - The desktop app for troubleshooting PNPM lockfiles
147
+
148
+ The `trace-import` tool is part of the [Rush Stack](https://rushstack.io/) family of projects.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require('../lib/start.js');
@@ -0,0 +1,10 @@
1
+ import { CommandLineParser } from '@rushstack/ts-command-line';
2
+ export declare class TraceImportCommandLineParser extends CommandLineParser {
3
+ private readonly _debugParameter;
4
+ private readonly _pathParameter;
5
+ private readonly _baseFolderParameter;
6
+ private readonly _resolutionTypeParameter;
7
+ constructor();
8
+ protected onExecute(): Promise<void>;
9
+ }
10
+ //# sourceMappingURL=TraceImportCommandLineParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TraceImportCommandLineParser.d.ts","sourceRoot":"","sources":["../src/TraceImportCommandLineParser.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,iBAAiB,EAIlB,MAAM,4BAA4B,CAAC;AAKpC,qBAAa,4BAA6B,SAAQ,iBAAiB;IACjE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA2B;IAC3D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6B;IAC5D,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA6B;IAClE,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAA6B;;cAiDtD,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;CAmB3C"}
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TraceImportCommandLineParser = void 0;
9
+ const safe_1 = __importDefault(require("colors/safe"));
10
+ const ts_command_line_1 = require("@rushstack/ts-command-line");
11
+ const node_core_library_1 = require("@rushstack/node-core-library");
12
+ const traceImport_1 = require("./traceImport");
13
+ class TraceImportCommandLineParser extends ts_command_line_1.CommandLineParser {
14
+ constructor() {
15
+ super({
16
+ toolFilename: 'trace-import',
17
+ toolDescription: 'This tool analyzes import module paths, to determine the resolved target folder. ' +
18
+ 'For example, if the "semver" NPM package is installed, "trace-import --path semver/index" will ' +
19
+ 'print output equivalent to the Node.js require.resolve() API. ' +
20
+ 'If "@types/semver" is installed, then "trace-import --resolution-type ts --path semver/index" will ' +
21
+ 'print the .d.ts file path that would be resolved by a TypeScript import statement.'
22
+ });
23
+ this._debugParameter = this.defineFlagParameter({
24
+ parameterLongName: '--debug',
25
+ parameterShortName: '-d',
26
+ description: 'Show the full call stack if an error occurs while executing the tool'
27
+ });
28
+ this._pathParameter = this.defineStringParameter({
29
+ parameterLongName: '--path',
30
+ parameterShortName: '-p',
31
+ description: 'The import module path to be analyzed. For example, ' +
32
+ '"example" in expressions such as: require("example"); require.resolve("example"); import { Thing } from "example";',
33
+ argumentName: 'IMPORT_PATH',
34
+ required: true
35
+ });
36
+ this._baseFolderParameter = this.defineStringParameter({
37
+ parameterLongName: '--base-folder',
38
+ parameterShortName: '-b',
39
+ description: 'The "--path" string will be resolved as if the import statement appeared in a script located in this folder. ' +
40
+ 'If omitted, the current working directory is used.',
41
+ argumentName: 'FOLDER_PATH'
42
+ });
43
+ this._resolutionTypeParameter = this.defineChoiceParameter({
44
+ parameterLongName: '--resolution-type',
45
+ parameterShortName: '-t',
46
+ description: 'The type of module resolution to perform: ' +
47
+ '"cjs" for CommonJS, "es" for ES modules, or "ts" for TypeScript typings',
48
+ alternatives: ['cjs', 'es', 'ts'],
49
+ defaultValue: 'cjs'
50
+ });
51
+ }
52
+ async onExecute() {
53
+ var _a;
54
+ // override
55
+ if (this._debugParameter.value) {
56
+ node_core_library_1.InternalError.breakInDebugger = true;
57
+ }
58
+ try {
59
+ (0, traceImport_1.traceImport)({
60
+ importPath: this._pathParameter.value,
61
+ baseFolder: this._baseFolderParameter.value,
62
+ resolutionType: ((_a = this._resolutionTypeParameter.value) !== null && _a !== void 0 ? _a : 'cjs')
63
+ });
64
+ }
65
+ catch (error) {
66
+ if (this._debugParameter.value) {
67
+ console.error('\n' + error.stack);
68
+ }
69
+ else {
70
+ console.error('\n' + safe_1.default.red('ERROR: ' + error.message.trim()));
71
+ }
72
+ }
73
+ }
74
+ }
75
+ exports.TraceImportCommandLineParser = TraceImportCommandLineParser;
76
+ //# sourceMappingURL=TraceImportCommandLineParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TraceImportCommandLineParser.js","sourceRoot":"","sources":["../src/TraceImportCommandLineParser.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAE3D,uDAAiC;AACjC,gEAKoC;AACpC,oEAA6D;AAE7D,+CAA4D;AAE5D,MAAa,4BAA6B,SAAQ,mCAAiB;IAMjE;QACE,KAAK,CAAC;YACJ,YAAY,EAAE,cAAc;YAC5B,eAAe,EACb,oFAAoF;gBACpF,iGAAiG;gBACjG,iEAAiE;gBACjE,qGAAqG;gBACrG,oFAAoF;SACvF,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,mBAAmB,CAAC;YAC9C,iBAAiB,EAAE,SAAS;YAC5B,kBAAkB,EAAE,IAAI;YACxB,WAAW,EAAE,sEAAsE;SACpF,CAAC,CAAC;QAEH,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,qBAAqB,CAAC;YAC/C,iBAAiB,EAAE,QAAQ;YAC3B,kBAAkB,EAAE,IAAI;YACxB,WAAW,EACT,sDAAsD;gBACtD,oHAAoH;YACtH,YAAY,EAAE,aAAa;YAC3B,QAAQ,EAAE,IAAI;SACf,CAAC,CAAC;QAEH,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,qBAAqB,CAAC;YACrD,iBAAiB,EAAE,eAAe;YAClC,kBAAkB,EAAE,IAAI;YACxB,WAAW,EACT,gHAAgH;gBAChH,oDAAoD;YACtD,YAAY,EAAE,aAAa;SAC5B,CAAC,CAAC;QAEH,IAAI,CAAC,wBAAwB,GAAG,IAAI,CAAC,qBAAqB,CAAC;YACzD,iBAAiB,EAAE,mBAAmB;YACtC,kBAAkB,EAAE,IAAI;YACxB,WAAW,EACT,6CAA6C;gBAC7C,yEAAyE;YAC3E,YAAY,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC;YACjC,YAAY,EAAE,KAAK;SACpB,CAAC,CAAC;IACL,CAAC;IAES,KAAK,CAAC,SAAS;;QACvB,WAAW;QACX,IAAI,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE;YAC9B,iCAAa,CAAC,eAAe,GAAG,IAAI,CAAC;SACtC;QACD,IAAI;YACF,IAAA,yBAAW,EAAC;gBACV,UAAU,EAAE,IAAI,CAAC,cAAc,CAAC,KAAM;gBACtC,UAAU,EAAE,IAAI,CAAC,oBAAoB,CAAC,KAAK;gBAC3C,cAAc,EAAE,CAAC,MAAA,IAAI,CAAC,wBAAwB,CAAC,KAAK,mCAAI,KAAK,CAAmB;aACjF,CAAC,CAAC;SACJ;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE;gBAC9B,OAAO,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;aACnC;iBAAM;gBACL,OAAO,CAAC,KAAK,CAAC,IAAI,GAAG,cAAM,CAAC,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;aACpE;SACF;IACH,CAAC;CACF;AAxED,oEAwEC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport colors from 'colors/safe';\nimport {\n CommandLineParser,\n CommandLineFlagParameter,\n CommandLineStringParameter,\n CommandLineChoiceParameter\n} from '@rushstack/ts-command-line';\nimport { InternalError } from '@rushstack/node-core-library';\n\nimport { ResolutionType, traceImport } from './traceImport';\n\nexport class TraceImportCommandLineParser extends CommandLineParser {\n private readonly _debugParameter: CommandLineFlagParameter;\n private readonly _pathParameter: CommandLineStringParameter;\n private readonly _baseFolderParameter: CommandLineStringParameter;\n private readonly _resolutionTypeParameter: CommandLineChoiceParameter;\n\n public constructor() {\n super({\n toolFilename: 'trace-import',\n toolDescription:\n 'This tool analyzes import module paths, to determine the resolved target folder. ' +\n 'For example, if the \"semver\" NPM package is installed, \"trace-import --path semver/index\" will ' +\n 'print output equivalent to the Node.js require.resolve() API. ' +\n 'If \"@types/semver\" is installed, then \"trace-import --resolution-type ts --path semver/index\" will ' +\n 'print the .d.ts file path that would be resolved by a TypeScript import statement.'\n });\n\n this._debugParameter = this.defineFlagParameter({\n parameterLongName: '--debug',\n parameterShortName: '-d',\n description: 'Show the full call stack if an error occurs while executing the tool'\n });\n\n this._pathParameter = this.defineStringParameter({\n parameterLongName: '--path',\n parameterShortName: '-p',\n description:\n 'The import module path to be analyzed. For example, ' +\n '\"example\" in expressions such as: require(\"example\"); require.resolve(\"example\"); import { Thing } from \"example\";',\n argumentName: 'IMPORT_PATH',\n required: true\n });\n\n this._baseFolderParameter = this.defineStringParameter({\n parameterLongName: '--base-folder',\n parameterShortName: '-b',\n description:\n 'The \"--path\" string will be resolved as if the import statement appeared in a script located in this folder. ' +\n 'If omitted, the current working directory is used.',\n argumentName: 'FOLDER_PATH'\n });\n\n this._resolutionTypeParameter = this.defineChoiceParameter({\n parameterLongName: '--resolution-type',\n parameterShortName: '-t',\n description:\n 'The type of module resolution to perform: ' +\n '\"cjs\" for CommonJS, \"es\" for ES modules, or \"ts\" for TypeScript typings',\n alternatives: ['cjs', 'es', 'ts'],\n defaultValue: 'cjs'\n });\n }\n\n protected async onExecute(): Promise<void> {\n // override\n if (this._debugParameter.value) {\n InternalError.breakInDebugger = true;\n }\n try {\n traceImport({\n importPath: this._pathParameter.value!,\n baseFolder: this._baseFolderParameter.value,\n resolutionType: (this._resolutionTypeParameter.value ?? 'cjs') as ResolutionType\n });\n } catch (error) {\n if (this._debugParameter.value) {\n console.error('\\n' + error.stack);\n } else {\n console.error('\\n' + colors.red('ERROR: ' + error.message.trim()));\n }\n }\n }\n}\n"]}
package/lib/start.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=start.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start.d.ts","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":""}
package/lib/start.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ const safe_1 = __importDefault(require("colors/safe"));
9
+ const node_core_library_1 = require("@rushstack/node-core-library");
10
+ const TraceImportCommandLineParser_1 = require("./TraceImportCommandLineParser");
11
+ const toolVersion = node_core_library_1.PackageJsonLookup.loadOwnPackageJson(__dirname).version;
12
+ console.log();
13
+ console.log(safe_1.default.bold(`trace-import ${toolVersion}`) + ' - ' + safe_1.default.cyan('https://rushstack.io'));
14
+ console.log();
15
+ const commandLine = new TraceImportCommandLineParser_1.TraceImportCommandLineParser();
16
+ commandLine.execute().catch((error) => {
17
+ console.error(error);
18
+ });
19
+ //# sourceMappingURL=start.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start.js","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;AAE3D,uDAAiC;AAEjC,oEAAiE;AACjE,iFAA8E;AAE9E,MAAM,WAAW,GAAW,qCAAiB,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC;AAEpF,OAAO,CAAC,GAAG,EAAE,CAAC;AACd,OAAO,CAAC,GAAG,CAAC,cAAM,CAAC,IAAI,CAAC,gBAAgB,WAAW,EAAE,CAAC,GAAG,KAAK,GAAG,cAAM,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC,CAAC;AACtG,OAAO,CAAC,GAAG,EAAE,CAAC;AAEd,MAAM,WAAW,GAAiC,IAAI,2DAA4B,EAAE,CAAC;AACrF,WAAW,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACpC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport colors from 'colors/safe';\n\nimport { PackageJsonLookup } from '@rushstack/node-core-library';\nimport { TraceImportCommandLineParser } from './TraceImportCommandLineParser';\n\nconst toolVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;\n\nconsole.log();\nconsole.log(colors.bold(`trace-import ${toolVersion}`) + ' - ' + colors.cyan('https://rushstack.io'));\nconsole.log();\n\nconst commandLine: TraceImportCommandLineParser = new TraceImportCommandLineParser();\ncommandLine.execute().catch((error) => {\n console.error(error);\n});\n"]}
@@ -0,0 +1,9 @@
1
+ export declare type ResolutionType = 'cjs' | 'es' | 'ts';
2
+ interface IExecuteOptions {
3
+ importPath: string;
4
+ baseFolder: string | undefined;
5
+ resolutionType: ResolutionType;
6
+ }
7
+ export declare function traceImport(options: IExecuteOptions): void;
8
+ export {};
9
+ //# sourceMappingURL=traceImport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traceImport.d.ts","sourceRoot":"","sources":["../src/traceImport.ts"],"names":[],"mappings":"AAkBA,oBAAY,cAAc,GAAG,KAAK,GAAG,IAAI,GAAG,IAAI,CAAC;AAEjD,UAAU,eAAe;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,cAAc,EAAE,cAAc,CAAC;CAChC;AAqSD,wBAAgB,WAAW,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,CAY1D"}
@@ -0,0 +1,302 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
17
+ }) : function(o, v) {
18
+ o["default"] = v;
19
+ });
20
+ var __importStar = (this && this.__importStar) || function (mod) {
21
+ if (mod && mod.__esModule) return mod;
22
+ var result = {};
23
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
+ __setModuleDefault(result, mod);
25
+ return result;
26
+ };
27
+ var __importDefault = (this && this.__importDefault) || function (mod) {
28
+ return (mod && mod.__esModule) ? mod : { "default": mod };
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.traceImport = void 0;
32
+ const node_core_library_1 = require("@rushstack/node-core-library");
33
+ const safe_1 = __importDefault(require("colors/safe"));
34
+ const path = __importStar(require("path"));
35
+ const process = __importStar(require("process"));
36
+ const Resolve = __importStar(require("resolve"));
37
+ const jsExtensions = ['.js', '.cjs', '.jsx', '.json'];
38
+ const tsExtensions = ['.d.ts', '.ts', '.tsx', '.json'];
39
+ // Somewhat loosely matches inputs such as:
40
+ // my-package/path/to/file.js
41
+ // [group 1 ][group 2 ]
42
+ //
43
+ // @scope/my-package/path/to/file.js
44
+ // [group 1 ][group 2 ]
45
+ //
46
+ // @scope/my-package
47
+ // [group 1 ]
48
+ const packageImportPathRegExp = /^((?:@[a-z0-9_][a-z0-9\-_\.]*\/)?[a-z0-9_][a-z0-9\-_\.]*)(\/.*)?$/i;
49
+ function logInputField(title, value) {
50
+ console.log(safe_1.default.cyan(title.padEnd(25)) + value);
51
+ }
52
+ function logOutputField(title, value) {
53
+ console.log(safe_1.default.green(title.padEnd(25)) + value);
54
+ }
55
+ function traceTypeScriptPackage(options) {
56
+ const { packageSubpath, packageFolder, packageJson, atTypes, warnings } = options;
57
+ // For example, if we started with importFullPath="semver/index",
58
+ // here we may get normalizedImportFullPath="@types/semver/index"
59
+ const normalizedImportFullPath = packageJson.name + packageSubpath;
60
+ // First try to resolve the .js main index
61
+ let cjsTargetPath = undefined;
62
+ try {
63
+ cjsTargetPath = Resolve.sync(normalizedImportFullPath, {
64
+ basedir: packageFolder,
65
+ preserveSymlinks: false,
66
+ extensions: jsExtensions
67
+ });
68
+ }
69
+ catch (error) {
70
+ // not found
71
+ }
72
+ const mainIndexTitle = atTypes ? '@types main index:' : 'Main index:';
73
+ if (cjsTargetPath) {
74
+ const parsedPath = path.parse(cjsTargetPath);
75
+ // Is the resolved .js extension okay?
76
+ if (tsExtensions.indexOf(parsedPath.ext.toLocaleLowerCase()) >= 0) {
77
+ logOutputField(mainIndexTitle, '(inferred from .js main index)');
78
+ console.log();
79
+ logOutputField('Target path:', cjsTargetPath);
80
+ return true;
81
+ }
82
+ // Try to replace the file extension
83
+ const dtsTargetPath = path.join(parsedPath.dir, parsedPath.name + '.d.ts');
84
+ if (node_core_library_1.FileSystem.exists(dtsTargetPath)) {
85
+ logOutputField(mainIndexTitle, '(inferred from .js entry point)');
86
+ console.log();
87
+ logOutputField('Target path:', dtsTargetPath);
88
+ return true;
89
+ }
90
+ }
91
+ if (!packageSubpath) {
92
+ // Try importing the "types"/"typings" main index:
93
+ if (packageJson.types) {
94
+ logOutputField(mainIndexTitle, `"types": ${JSON.stringify(packageJson.types)}`);
95
+ console.log();
96
+ logOutputField('Target path:', path.join(packageFolder, packageJson.types));
97
+ return true;
98
+ }
99
+ if (packageJson.typings) {
100
+ logOutputField(mainIndexTitle, `"typings": ${JSON.stringify(packageJson.typings)}`);
101
+ console.log();
102
+ logOutputField('Target path:', path.join(packageFolder, packageJson.typings));
103
+ return true;
104
+ }
105
+ if (atTypes) {
106
+ warnings.push('The @types package does not define "types" or "typings" field.');
107
+ }
108
+ }
109
+ else {
110
+ // Try importing the .d.ts file directly
111
+ let dtsTargetPath = undefined;
112
+ try {
113
+ dtsTargetPath = Resolve.sync(normalizedImportFullPath, {
114
+ basedir: packageFolder,
115
+ preserveSymlinks: false,
116
+ extensions: tsExtensions
117
+ });
118
+ }
119
+ catch (error) {
120
+ // not found
121
+ }
122
+ if (dtsTargetPath) {
123
+ console.log();
124
+ logOutputField('Target path:', dtsTargetPath);
125
+ return true;
126
+ }
127
+ }
128
+ return false;
129
+ }
130
+ function traceImportInner(options, warnings) {
131
+ let baseFolder;
132
+ if (options.baseFolder) {
133
+ baseFolder = path.resolve(options.baseFolder);
134
+ }
135
+ else {
136
+ baseFolder = process.cwd();
137
+ }
138
+ const importFullPath = options.importPath.trim();
139
+ if (!importFullPath) {
140
+ throw new Error(`Invalid import path syntax: ${JSON.stringify(importFullPath)}`);
141
+ }
142
+ const match = packageImportPathRegExp.exec(importFullPath);
143
+ logInputField('Base folder:', baseFolder);
144
+ if (match) {
145
+ const importPackageName = match[1];
146
+ const packageSubpath = match[2];
147
+ const packageSubpathWithoutSlash = packageSubpath
148
+ ? packageSubpath.substring(1)
149
+ : undefined;
150
+ logInputField('Package name:', importPackageName);
151
+ logInputField('Package subpath:', packageSubpathWithoutSlash || '(not specified)');
152
+ console.log('\nResolving...\n');
153
+ // Resolve the NPM package first
154
+ let packageFolder;
155
+ let packageJson = undefined;
156
+ {
157
+ let resolvedPackageJsonPath;
158
+ try {
159
+ resolvedPackageJsonPath = Resolve.sync(`${importPackageName}/package.json`, {
160
+ basedir: baseFolder,
161
+ preserveSymlinks: false
162
+ });
163
+ }
164
+ catch (e) {
165
+ // Could not find NPM package
166
+ }
167
+ if (resolvedPackageJsonPath) {
168
+ packageFolder = path.dirname(resolvedPackageJsonPath);
169
+ logOutputField('Package folder:', packageFolder);
170
+ packageJson = node_core_library_1.JsonFile.load(resolvedPackageJsonPath);
171
+ logOutputField('package.json:', `${packageJson.name || '(missing name)'} (${packageJson.version || 'missing version'})`);
172
+ }
173
+ }
174
+ // Also try to resolve the @types package
175
+ let atTypesPackageFolder = undefined;
176
+ let atTypesPackageJson = undefined;
177
+ if (options.resolutionType === 'ts') {
178
+ if (!importPackageName.startsWith('@types/')) {
179
+ const parsedPackageName = node_core_library_1.PackageName.parse(importPackageName);
180
+ let atTypesPackageName;
181
+ if (parsedPackageName.scope) {
182
+ atTypesPackageName = `@types/${parsedPackageName.scope}__${parsedPackageName.unscopedName}`;
183
+ }
184
+ else {
185
+ atTypesPackageName = `@types/${parsedPackageName.unscopedName}`;
186
+ }
187
+ let atTypesPackageJsonPath;
188
+ try {
189
+ atTypesPackageJsonPath = Resolve.sync(`${atTypesPackageName}/package.json`, {
190
+ basedir: baseFolder,
191
+ preserveSymlinks: false
192
+ });
193
+ }
194
+ catch (e) {
195
+ // Unable to resolve @types package
196
+ }
197
+ if (atTypesPackageJsonPath) {
198
+ atTypesPackageFolder = path.dirname(atTypesPackageJsonPath);
199
+ logOutputField('@types folder:', atTypesPackageFolder);
200
+ atTypesPackageJson = node_core_library_1.JsonFile.load(atTypesPackageJsonPath);
201
+ logOutputField('@types package.json:', `${atTypesPackageJson.name || '(missing name)'} (${atTypesPackageJson.version || 'missing version'})`);
202
+ }
203
+ }
204
+ }
205
+ switch (options.resolutionType) {
206
+ case 'cjs':
207
+ {
208
+ if (!packageFolder || !packageJson) {
209
+ throw new Error(`Cannot find package "${importPackageName}" from "${baseFolder}".`);
210
+ }
211
+ if (!packageSubpath) {
212
+ if (packageJson.main) {
213
+ logOutputField('Main index:', `"main": ${JSON.stringify(packageJson.main)}`);
214
+ }
215
+ else {
216
+ logOutputField('Main index:', '(none)');
217
+ }
218
+ }
219
+ let targetPath;
220
+ try {
221
+ targetPath = Resolve.sync(importFullPath, {
222
+ basedir: packageFolder,
223
+ preserveSymlinks: false,
224
+ extensions: jsExtensions
225
+ });
226
+ }
227
+ catch (error) {
228
+ // Are we importing the main index?
229
+ if (packageSubpath) {
230
+ throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);
231
+ }
232
+ else {
233
+ console.log('\nThis package does not define a main index.');
234
+ return;
235
+ }
236
+ }
237
+ console.log();
238
+ logOutputField('Target path:', targetPath);
239
+ }
240
+ break;
241
+ case 'ts':
242
+ if (!packageFolder || (!packageJson && !atTypesPackageFolder && !atTypesPackageJson)) {
243
+ throw new Error(`Cannot find package "${importPackageName}" from "${baseFolder}".`);
244
+ }
245
+ if (packageFolder && packageJson) {
246
+ if (traceTypeScriptPackage({ packageSubpath, packageFolder, packageJson, warnings })) {
247
+ if (atTypesPackageFolder) {
248
+ warnings.push('An @types package was found but not used.');
249
+ }
250
+ return;
251
+ }
252
+ }
253
+ if (atTypesPackageFolder && atTypesPackageJson) {
254
+ if (traceTypeScriptPackage({
255
+ packageSubpath,
256
+ packageFolder: atTypesPackageFolder,
257
+ packageJson: atTypesPackageJson,
258
+ warnings,
259
+ atTypes: true
260
+ })) {
261
+ return;
262
+ }
263
+ }
264
+ throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);
265
+ default:
266
+ throw new Error(`The "${options.resolutionType}" resolution type is not implemented yet`);
267
+ }
268
+ }
269
+ else {
270
+ logInputField('Import path:', importFullPath);
271
+ console.log(`\nThe import path does not appear to reference an NPM package.`);
272
+ console.log('Resolving...\n');
273
+ let targetPath;
274
+ try {
275
+ targetPath = Resolve.sync(importFullPath, {
276
+ basedir: baseFolder,
277
+ preserveSymlinks: false,
278
+ extensions: options.resolutionType === 'ts' ? tsExtensions : jsExtensions
279
+ });
280
+ }
281
+ catch (error) {
282
+ throw new Error(`Unable to resolve the import path: ${importFullPath}`);
283
+ }
284
+ logOutputField('Target path:', targetPath);
285
+ }
286
+ }
287
+ function traceImport(options) {
288
+ const warnings = [];
289
+ try {
290
+ traceImportInner(options, warnings);
291
+ }
292
+ finally {
293
+ if (warnings.length) {
294
+ console.log();
295
+ for (const warning of warnings) {
296
+ console.log(safe_1.default.yellow('Warning: ' + warning));
297
+ }
298
+ }
299
+ }
300
+ }
301
+ exports.traceImport = traceImport;
302
+ //# sourceMappingURL=traceImport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traceImport.js","sourceRoot":"","sources":["../src/traceImport.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,oEAMsC;AACtC,uDAAiC;AACjC,2CAA6B;AAC7B,iDAAmC;AACnC,iDAAmC;AAEnC,MAAM,YAAY,GAAa,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAChE,MAAM,YAAY,GAAa,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAUjE,2CAA2C;AAC3C,+BAA+B;AAC/B,+BAA+B;AAC/B,EAAE;AACF,sCAAsC;AACtC,sCAAsC;AACtC,EAAE;AACF,sBAAsB;AACtB,sBAAsB;AACtB,MAAM,uBAAuB,GAAW,oEAAoE,CAAC;AAE7G,SAAS,aAAa,CAAC,KAAa,EAAE,KAAa;IACjD,OAAO,CAAC,GAAG,CAAC,cAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,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9B,IAAI,UAAkB,CAAC;QACvB,IAAI;YACF,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;gBACxC,OAAO,EAAE,UAAU;gBACnB,gBAAgB,EAAE,KAAK;gBACvB,UAAU,EAAE,OAAO,CAAC,cAAc,KAAK,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY;aAC1E,CAAC,CAAC;SACJ;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,sCAAsC,cAAc,EAAE,CAAC,CAAC;SACzE;QAED,cAAc,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;KAC5C;AACH,CAAC;AAED,SAAgB,WAAW,CAAC,OAAwB;IAClD,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI;QACF,gBAAgB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;KACrC;YAAS;QACR,IAAI,QAAQ,CAAC,MAAM,EAAE;YACnB,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;gBAC9B,OAAO,CAAC,GAAG,CAAC,cAAM,CAAC,MAAM,CAAC,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;aACnD;SACF;KACF;AACH,CAAC;AAZD,kCAYC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport {\n FileSystem,\n IPackageJson,\n IParsedPackageName,\n JsonFile,\n PackageName\n} from '@rushstack/node-core-library';\nimport colors from 'colors/safe';\nimport * as path from 'path';\nimport * as process from 'process';\nimport * as Resolve from 'resolve';\n\nconst jsExtensions: string[] = ['.js', '.cjs', '.jsx', '.json'];\nconst tsExtensions: string[] = ['.d.ts', '.ts', '.tsx', '.json'];\n\nexport type ResolutionType = 'cjs' | 'es' | 'ts';\n\ninterface IExecuteOptions {\n importPath: string;\n baseFolder: string | undefined;\n resolutionType: ResolutionType;\n}\n\n// Somewhat loosely matches inputs such as:\n// my-package/path/to/file.js\n// [group 1 ][group 2 ]\n//\n// @scope/my-package/path/to/file.js\n// [group 1 ][group 2 ]\n//\n// @scope/my-package\n// [group 1 ]\nconst packageImportPathRegExp: RegExp = /^((?:@[a-z0-9_][a-z0-9\\-_\\.]*\\/)?[a-z0-9_][a-z0-9\\-_\\.]*)(\\/.*)?$/i;\n\nfunction logInputField(title: string, value: string): void {\n console.log(colors.cyan(title.padEnd(25)) + value);\n}\n\nfunction logOutputField(title: string, value: string): void {\n console.log(colors.green(title.padEnd(25)) + value);\n}\n\nfunction traceTypeScriptPackage(options: {\n packageSubpath: string;\n packageFolder: string;\n packageJson: IPackageJson;\n warnings: string[];\n atTypes?: boolean;\n}): boolean {\n const { packageSubpath, packageFolder, packageJson, atTypes, warnings } = options;\n\n // For example, if we started with importFullPath=\"semver/index\",\n // here we may get normalizedImportFullPath=\"@types/semver/index\"\n const normalizedImportFullPath: string = packageJson.name + packageSubpath;\n\n // First try to resolve the .js main index\n let cjsTargetPath: string | undefined = undefined;\n try {\n cjsTargetPath = Resolve.sync(normalizedImportFullPath, {\n basedir: packageFolder,\n preserveSymlinks: false,\n extensions: jsExtensions\n });\n } catch (error) {\n // not found\n }\n\n const mainIndexTitle: string = atTypes ? '@types main index:' : 'Main index:';\n\n if (cjsTargetPath) {\n const parsedPath: path.ParsedPath = path.parse(cjsTargetPath);\n\n // Is the resolved .js extension okay?\n if (tsExtensions.indexOf(parsedPath.ext.toLocaleLowerCase()) >= 0) {\n logOutputField(mainIndexTitle, '(inferred from .js main index)');\n console.log();\n logOutputField('Target path:', cjsTargetPath);\n return true;\n }\n\n // Try to replace the file extension\n const dtsTargetPath: string = path.join(parsedPath.dir, parsedPath.name + '.d.ts');\n if (FileSystem.exists(dtsTargetPath)) {\n logOutputField(mainIndexTitle, '(inferred from .js entry point)');\n console.log();\n logOutputField('Target path:', dtsTargetPath);\n return true;\n }\n }\n\n if (!packageSubpath) {\n // Try importing the \"types\"/\"typings\" main index:\n\n if (packageJson.types) {\n logOutputField(mainIndexTitle, `\"types\": ${JSON.stringify(packageJson.types)}`);\n console.log();\n logOutputField('Target path:', path.join(packageFolder, packageJson.types));\n return true;\n }\n\n if (packageJson.typings) {\n logOutputField(mainIndexTitle, `\"typings\": ${JSON.stringify(packageJson.typings)}`);\n console.log();\n logOutputField('Target path:', path.join(packageFolder, packageJson.typings));\n return true;\n }\n\n if (atTypes) {\n warnings.push('The @types package does not define \"types\" or \"typings\" field.');\n }\n } else {\n // Try importing the .d.ts file directly\n let dtsTargetPath: string | undefined = undefined;\n try {\n dtsTargetPath = Resolve.sync(normalizedImportFullPath, {\n basedir: packageFolder,\n preserveSymlinks: false,\n extensions: tsExtensions\n });\n } catch (error) {\n // not found\n }\n\n if (dtsTargetPath) {\n console.log();\n logOutputField('Target path:', dtsTargetPath);\n return true;\n }\n }\n\n return false;\n}\n\nfunction traceImportInner(options: IExecuteOptions, warnings: string[]): void {\n let baseFolder: string;\n if (options.baseFolder) {\n baseFolder = path.resolve(options.baseFolder);\n } else {\n baseFolder = process.cwd();\n }\n\n const importFullPath: string = options.importPath.trim();\n if (!importFullPath) {\n throw new Error(`Invalid import path syntax: ${JSON.stringify(importFullPath)}`);\n }\n const match: RegExpExecArray | null = packageImportPathRegExp.exec(importFullPath);\n\n logInputField('Base folder:', baseFolder);\n\n if (match) {\n const importPackageName: string = match[1];\n const packageSubpath: string | undefined = match[2];\n const packageSubpathWithoutSlash: string | undefined = packageSubpath\n ? packageSubpath.substring(1)\n : undefined;\n\n logInputField('Package name:', importPackageName);\n logInputField('Package subpath:', packageSubpathWithoutSlash || '(not specified)');\n\n console.log('\\nResolving...\\n');\n\n // Resolve the NPM package first\n let packageFolder: string | undefined;\n let packageJson: IPackageJson | undefined = undefined;\n {\n let resolvedPackageJsonPath: string | undefined;\n try {\n resolvedPackageJsonPath = Resolve.sync(`${importPackageName}/package.json`, {\n basedir: baseFolder,\n preserveSymlinks: false\n });\n } catch (e) {\n // Could not find NPM package\n }\n\n if (resolvedPackageJsonPath) {\n packageFolder = path.dirname(resolvedPackageJsonPath);\n logOutputField('Package folder:', packageFolder);\n\n packageJson = JsonFile.load(resolvedPackageJsonPath) as IPackageJson;\n logOutputField(\n 'package.json:',\n `${packageJson.name || '(missing name)'} (${packageJson.version || 'missing version'})`\n );\n }\n }\n\n // Also try to resolve the @types package\n let atTypesPackageFolder: string | undefined = undefined;\n let atTypesPackageJson: IPackageJson | undefined = undefined;\n\n if (options.resolutionType === 'ts') {\n if (!importPackageName.startsWith('@types/')) {\n const parsedPackageName: IParsedPackageName = PackageName.parse(importPackageName);\n let atTypesPackageName: string;\n if (parsedPackageName.scope) {\n atTypesPackageName = `@types/${parsedPackageName.scope}__${parsedPackageName.unscopedName}`;\n } else {\n atTypesPackageName = `@types/${parsedPackageName.unscopedName}`;\n }\n\n let atTypesPackageJsonPath: string | undefined;\n try {\n atTypesPackageJsonPath = Resolve.sync(`${atTypesPackageName}/package.json`, {\n basedir: baseFolder,\n preserveSymlinks: false\n });\n } catch (e) {\n // Unable to resolve @types package\n }\n\n if (atTypesPackageJsonPath) {\n atTypesPackageFolder = path.dirname(atTypesPackageJsonPath);\n logOutputField('@types folder:', atTypesPackageFolder);\n\n atTypesPackageJson = JsonFile.load(atTypesPackageJsonPath) as IPackageJson;\n logOutputField(\n '@types package.json:',\n `${atTypesPackageJson.name || '(missing name)'} (${\n atTypesPackageJson.version || 'missing version'\n })`\n );\n }\n }\n }\n\n switch (options.resolutionType) {\n case 'cjs':\n {\n if (!packageFolder || !packageJson) {\n throw new Error(`Cannot find package \"${importPackageName}\" from \"${baseFolder}\".`);\n }\n\n if (!packageSubpath) {\n if (packageJson.main) {\n logOutputField('Main index:', `\"main\": ${JSON.stringify(packageJson.main)}`);\n } else {\n logOutputField('Main index:', '(none)');\n }\n }\n\n let targetPath: string;\n try {\n targetPath = Resolve.sync(importFullPath, {\n basedir: packageFolder,\n preserveSymlinks: false,\n extensions: jsExtensions\n });\n } catch (error) {\n // Are we importing the main index?\n if (packageSubpath) {\n throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);\n } else {\n console.log('\\nThis package does not define a main index.');\n return;\n }\n }\n console.log();\n logOutputField('Target path:', targetPath);\n }\n break;\n case 'ts':\n if (!packageFolder || (!packageJson && !atTypesPackageFolder && !atTypesPackageJson)) {\n throw new Error(`Cannot find package \"${importPackageName}\" from \"${baseFolder}\".`);\n }\n\n if (packageFolder && packageJson) {\n if (traceTypeScriptPackage({ packageSubpath, packageFolder, packageJson, warnings })) {\n if (atTypesPackageFolder) {\n warnings.push('An @types package was found but not used.');\n }\n\n return;\n }\n }\n\n if (atTypesPackageFolder && atTypesPackageJson) {\n if (\n traceTypeScriptPackage({\n packageSubpath,\n packageFolder: atTypesPackageFolder,\n packageJson: atTypesPackageJson,\n warnings,\n atTypes: true\n })\n ) {\n return;\n }\n }\n\n throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);\n default:\n throw new Error(`The \"${options.resolutionType}\" resolution type is not implemented yet`);\n }\n } else {\n logInputField('Import path:', importFullPath);\n console.log(`\\nThe import path does not appear to reference an NPM package.`);\n console.log('Resolving...\\n');\n\n let targetPath: string;\n try {\n targetPath = Resolve.sync(importFullPath, {\n basedir: baseFolder,\n preserveSymlinks: false,\n extensions: options.resolutionType === 'ts' ? tsExtensions : jsExtensions\n });\n } catch (error) {\n throw new Error(`Unable to resolve the import path: ${importFullPath}`);\n }\n\n logOutputField('Target path:', targetPath);\n }\n}\n\nexport function traceImport(options: IExecuteOptions): void {\n const warnings: string[] = [];\n try {\n traceImportInner(options, warnings);\n } finally {\n if (warnings.length) {\n console.log();\n for (const warning of warnings) {\n console.log(colors.yellow('Warning: ' + warning));\n }\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,5 +1,37 @@
1
1
  {
2
- "license": "MIT",
3
2
  "name": "@rushstack/trace-import",
4
- "version": "0.0.0"
5
- }
3
+ "version": "0.1.1",
4
+ "description": "CLI tool for understanding how require() and \"import\" statements get resolved",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/microsoft/rushstack.git",
8
+ "directory": "apps/trace-import"
9
+ },
10
+ "bin": {
11
+ "trace-import": "./bin/trace-import"
12
+ },
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "@rushstack/node-core-library": "3.53.3",
16
+ "@rushstack/ts-command-line": "4.13.1",
17
+ "colors": "~1.2.1",
18
+ "resolve": "~1.22.1",
19
+ "semver": "~7.3.0",
20
+ "typescript": "~4.8.4"
21
+ },
22
+ "devDependencies": {
23
+ "@rushstack/eslint-config": "3.1.1",
24
+ "@rushstack/heft": "0.48.9",
25
+ "@rushstack/heft-node-rig": "1.11.10",
26
+ "@types/heft-jest": "1.0.1",
27
+ "@types/node": "12.20.24",
28
+ "@types/resolve": "1.20.2",
29
+ "@types/semver": "7.3.5"
30
+ },
31
+ "scripts": {
32
+ "start": "node lib/start",
33
+ "build": "heft build --clean",
34
+ "_phase:build": "heft build --clean",
35
+ "_phase:test": "heft test --no-build"
36
+ }
37
+ }