Skip to content

Commit f652c55

Browse files
authored
Merge branch 'main' into main
2 parents a4d7f9c + 4591469 commit f652c55

File tree

11 files changed

+4386
-8662
lines changed

11 files changed

+4386
-8662
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ name: CI
33
on:
44
push:
55
branches:
6-
- "**"
7-
- "!dependabot/**"
6+
- "main"
87
pull_request:
98
workflow_dispatch:
109

@@ -23,16 +22,16 @@ jobs:
2322
fail-fast: false
2423
matrix:
2524
os: [ubuntu-latest, windows-latest]
26-
node: [14, 16, 18, 20]
25+
node: [18, 20, 22]
2726

2827
steps:
2928
- name: Clone repository
30-
uses: actions/checkout@v3
29+
uses: actions/checkout@v4
3130
with:
3231
persist-credentials: false
3332

3433
- name: Set up Node.js
35-
uses: actions/setup-node@v3
34+
uses: actions/setup-node@v4
3635
with:
3736
node-version: ${{ matrix.node }}
3837
cache: npm

.github/workflows/codeql.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ on:
44
push:
55
branches:
66
- main
7-
- "!dependabot/**"
87
pull_request:
98
branches:
109
- main
11-
- "!dependabot/**"
1210
schedule:
1311
- cron: "0 0 * * 0"
1412
workflow_dispatch:
@@ -24,21 +22,21 @@ jobs:
2422

2523
steps:
2624
- name: Clone repository
27-
uses: actions/checkout@v3
25+
uses: actions/checkout@v4
2826
with:
2927
persist-credentials: false
3028

3129
- name: Initialize CodeQL
32-
uses: github/codeql-action/init@v2
30+
uses: github/codeql-action/init@v3
3331
with:
3432
config-file: ./.github/codeql/codeql-config.yml
3533
languages: "javascript"
3634
queries: +security-and-quality
3735

3836
- name: Autobuild
39-
uses: github/codeql-action/autobuild@v2
37+
uses: github/codeql-action/autobuild@v3
4038

4139
- name: Perform CodeQL Analysis
42-
uses: github/codeql-action/analyze@v2
40+
uses: github/codeql-action/analyze@v3
4341
with:
4442
category: "/language:javascript"

.github/workflows/lint.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ name: Lint
33
on:
44
push:
55
branches:
6-
- "**"
7-
- "!dependabot/**"
6+
- "main"
87
pull_request:
98
workflow_dispatch:
109

1110
env:
1211
FORCE_COLOR: 2
13-
NODE: 18
12+
NODE: 20
1413

1514
permissions:
1615
contents: read
@@ -21,12 +20,12 @@ jobs:
2120

2221
steps:
2322
- name: Clone repository
24-
uses: actions/checkout@v3
23+
uses: actions/checkout@v4
2524
with:
2625
persist-credentials: false
2726

2827
- name: Set up Node.js
29-
uses: actions/setup-node@v3
28+
uses: actions/setup-node@v4
3029
with:
3130
node-version: ${{ env.NODE }}
3231
cache: npm

.npmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
npm install dependency-tree
1111
```
1212

13-
* Works for JS (AMD, CommonJS, ES6 modules), Typescript, and CSS preprocessors (CSS (PostCSS), Sass, Stylus, and Less); basically, any module type supported by [Precinct](https://github.com/dependents/node-precinct).
13+
* Works for JS (AMD, CommonJS, ES6 modules), TypeScript, and CSS preprocessors (CSS (PostCSS), Sass, Stylus, and Less); basically, any module type supported by [Precinct](https://github.com/dependents/node-precinct).
1414
- For CommonJS modules, 3rd party dependencies (npm installed dependencies) are included in the tree by default
1515
- Dependency path resolutions are handled by [filing-cabinet](https://github.com/dependents/node-filing-cabinet)
1616
- Supports RequireJS and Webpack loaders
@@ -52,6 +52,7 @@ const list = dependencyTree.toList({
5252
* `requireConfig`: path to a requirejs config for AMD modules (allows for the result of aliased module paths)
5353
* `webpackConfig`: path to a webpack config for aliased modules
5454
* `tsConfig`: path to a typescript config (or a preloaded object representing the typescript config)
55+
* `tsConfigPath`: a (virtual) path to typescript config file when `tsConfig` option is given as an object, not a string. Needed to calculate [Path Mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping). If not given when `tsConfig` is an object, **Path Mapping** is ignored. This is not needed when `tsConfig` is given as a path string.
5556
* `nodeModulesConfig`: config for resolving entry file for node_modules
5657
* `visited`: object used for avoiding redundant subtree generations via memoization.
5758
* `nonExistent`: array used for storing the list of partial paths that do not exist

bin/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict';
44

5-
const program = require('commander');
5+
const { program } = require('commander');
66
const dependencyTree = require('../index.js');
77
const { name, description, version } = require('../package.json');
88

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const fs = require('node:fs');
4+
const path = require('node:path');
45
const { debuglog } = require('node:util');
56
const cabinet = require('filing-cabinet');
67
const precinct = require('precinct');
@@ -106,6 +107,7 @@ module.exports._getDependencies = function(config = {}) {
106107
webpackConfig: config.webpackConfig,
107108
nodeModulesConfig: config.nodeModulesConfig,
108109
tsConfig: config.tsConfig,
110+
tsConfigPath: config.tsConfigPath,
109111
noTypeDefinitions: config.noTypeDefinitions
110112
});
111113

@@ -167,6 +169,7 @@ function traverse(config = {}) {
167169
for (const dependency of dependencies) {
168170
const localConfig = config.clone();
169171
localConfig.filename = dependency;
172+
localConfig.directory = getDirectory(localConfig);
170173

171174
if (localConfig.isListForm) {
172175
for (const item of traverse(localConfig)) {
@@ -198,3 +201,25 @@ function dedupeNonExistent(nonExistent) {
198201
i++;
199202
}
200203
}
204+
205+
// If the file is in a node module, use the root directory of the module
206+
function getDirectory(localConfig) {
207+
if (!localConfig.filename.includes('node_modules')) {
208+
return localConfig.directory;
209+
}
210+
211+
return getProjectPath(path.dirname(localConfig.filename)) || localConfig.directory;
212+
}
213+
214+
function getProjectPath(filename) {
215+
try {
216+
const nodeModuleParts = filename.split('node_modules');
217+
const packageSubPathPath = nodeModuleParts.pop().split(path.sep).filter(Boolean);
218+
const packageName = packageSubPathPath[0].startsWith('@') ? `${packageSubPathPath[0]}${path.sep}${packageSubPathPath[1]}` : packageSubPathPath[0];
219+
220+
return path.normalize([...nodeModuleParts, `${path.sep}${packageName}`].join('node_modules'));
221+
} catch {
222+
debug(`Could not determine the root directory of package file ${filename}. Using default`);
223+
return null;
224+
}
225+
}

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module.exports = class Config {
2020
this.detectiveConfig.includeCore = this.includeCore = options.includeCore || false;
2121
this.detectiveConfig.includeNonExisting = this.includeNonExisting = options.includeNonExisting || options.includeCore || false;
2222
this.tsConfig = options.tsConfig;
23+
this.tsConfigPath = options.tsConfigPath;
2324
this.noTypeDefinitions = options.noTypeDefinitions;
24-
2525
this.filter = options.filter;
2626

2727
if (!this.filename) throw new Error('filename not given');
@@ -33,6 +33,7 @@ module.exports = class Config {
3333
const ts = require('typescript');
3434
const tsParsedConfig = ts.readJsonConfigFile(this.tsConfig, ts.sys.readFile);
3535
const obj = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path.dirname(this.tsConfig));
36+
this.tsConfigPath ||= this.tsConfig;
3637
this.tsConfig = obj.raw;
3738
}
3839

0 commit comments

Comments
 (0)