wtf-dependencies 1.0.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/README.md +41 -0
- package/index.js +68 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# WTF-Dependency 
|
|
2
|
+
|
|
3
|
+
**wtf-dependency** is a CLI tool to trace where (and how deeply) a given dependency is nested in your Node.js project's dependency tree (from package-lock.json). Useful for understanding why a package is present in node_modules.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
**Install:**
|
|
8
|
+
- From npm (recommended):
|
|
9
|
+
- `npm install -g wtf-dependency`
|
|
10
|
+
- Or clone: `git clone <repo-url> && cd wtf-dependency && npm install`
|
|
11
|
+
- (Optional, for local dev): `npm link`
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
**Usage:**
|
|
15
|
+
- After global install, run from any project root (where package-lock.json is):
|
|
16
|
+
- `wtf-dependency <dependency-name>`
|
|
17
|
+
- Example: `wtf-dependency lodash`
|
|
18
|
+
|
|
19
|
+
**Output Examples:**
|
|
20
|
+
- Direct: `Dependency path: lodash`
|
|
21
|
+
- Nested: `Dependency path: foo → bar → lodash`
|
|
22
|
+
- Not found: `No dependencies found in package-lock.json` or `Usage: wtf-dependency <dependency-name>`
|
|
23
|
+
|
|
24
|
+
**Error Handling:**
|
|
25
|
+
- No package-lock.json found: Run from the correct directory.
|
|
26
|
+
- No dependencies found: Run `npm install` to generate dependencies.
|
|
27
|
+
- No dependency name: Add the name to the command.
|
|
28
|
+
|
|
29
|
+
**Notes:**
|
|
30
|
+
- Supports npm v7+ (package-lock v2/v3) and older lockfile formats.
|
|
31
|
+
- Run from your project root, not node_modules.
|
|
32
|
+
|
|
33
|
+
**Contributing:**
|
|
34
|
+
- Fork, branch, commit, and open a pull request.
|
|
35
|
+
- For bugs/features, open an issue.
|
|
36
|
+
|
|
37
|
+
**License:** ISC. See [LICENSE](LICENSE).
|
|
38
|
+
|
|
39
|
+
**Changelog:**
|
|
40
|
+
- 1.0.0: Initial release (npm v7+ and legacy support)
|
|
41
|
+
|
package/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
function findDependency(depName, pkgPath = process.cwd(), parents = []) {
|
|
6
|
+
const packageLockPath = path.join(pkgPath, 'package-lock.json');
|
|
7
|
+
|
|
8
|
+
if (!fs.existsSync(packageLockPath)) {
|
|
9
|
+
console.error('No package-lock.json found in', pkgPath);
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const lockFile = JSON.parse(fs.readFileSync(packageLockPath, 'utf-8'));
|
|
14
|
+
|
|
15
|
+
// Support for package-lock v2/v3 (npm 7+)
|
|
16
|
+
if (lockFile.packages) {
|
|
17
|
+
// Build a map of package -> dependencies
|
|
18
|
+
const packageDeps = {};
|
|
19
|
+
for (const [pkgPath, pkgInfo] of Object.entries(lockFile.packages)) {
|
|
20
|
+
packageDeps[pkgPath] = pkgInfo.dependencies || {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Recursively search from root ('')
|
|
24
|
+
function traverseV3(currentPath, chain = []) {
|
|
25
|
+
const deps = packageDeps[currentPath] || {};
|
|
26
|
+
for (const [name, versionRange] of Object.entries(deps)) {
|
|
27
|
+
const depPath = currentPath === '' ? `node_modules/${name}` : `${currentPath}/node_modules/${name}`;
|
|
28
|
+
const newChain = [...chain, name];
|
|
29
|
+
if (name === depName) {
|
|
30
|
+
console.log('Dependency path:', newChain.join(' → '));
|
|
31
|
+
}
|
|
32
|
+
if (packageDeps[depPath]) {
|
|
33
|
+
traverseV3(depPath, newChain);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
traverseV3('');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Fallback for old lockfile format
|
|
42
|
+
function traverse(deps, chain = []) {
|
|
43
|
+
for (const [name, info] of Object.entries(deps)) {
|
|
44
|
+
const newChain = [...chain, name];
|
|
45
|
+
if (name === depName) {
|
|
46
|
+
console.log('Dependency path:', newChain.join(' → '));
|
|
47
|
+
}
|
|
48
|
+
if (info.dependencies) {
|
|
49
|
+
traverse(info.dependencies, newChain);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!lockFile.dependencies) {
|
|
55
|
+
console.error('No dependencies found in package-lock.json');
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
traverse(lockFile.dependencies);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// CLI input
|
|
62
|
+
const dep = process.argv[2];
|
|
63
|
+
if (!dep) {
|
|
64
|
+
console.error('Usage: wtf-dependency <dependency-name>');
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
findDependency(dep);
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wtf-dependencies",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"bin": {
|
|
9
|
+
"wtf-dependency": "index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"description": "",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"read-pkg": "^10.0.0"
|
|
17
|
+
}
|
|
18
|
+
}
|