dependency-detective 0.1.2 → 0.2.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/.claude/worktrees/amazing-bartik-b7ca06/.claude/settings.local.json +19 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/LICENSE +21 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/README.md +151 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/docs/superpowers/specs/2026-05-09-correctness-fixes-design.md +118 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/index.js +19 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/package-lock.json +915 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/package.json +36 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/src/alternative-suggester.js +64 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/src/analyzer.js +141 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/src/security-checker.js +62 -0
- package/.claude/worktrees/amazing-bartik-b7ca06/src/unused-detector.js +71 -0
- package/README.md +33 -11
- package/index.js +10 -3
- package/package.json +2 -1
- package/src/analyzer.js +8 -29
- package/src/ast-parser.js +79 -0
- package/src/config-scanner.js +65 -0
- package/src/security-checker.js +6 -10
- package/src/unused-detector.js +49 -71
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
const CONFIG_FILES = [
|
|
5
|
+
'tailwind.config.js',
|
|
6
|
+
'tailwind.config.ts',
|
|
7
|
+
'postcss.config.js',
|
|
8
|
+
'postcss.config.cjs',
|
|
9
|
+
'babel.config.js',
|
|
10
|
+
'babel.config.json',
|
|
11
|
+
'jest.config.js',
|
|
12
|
+
'jest.config.ts',
|
|
13
|
+
'vite.config.js',
|
|
14
|
+
'vite.config.ts',
|
|
15
|
+
'webpack.config.js',
|
|
16
|
+
'webpack.config.cjs',
|
|
17
|
+
'prettier.config.js',
|
|
18
|
+
'.eslintrc',
|
|
19
|
+
'.eslintrc.js',
|
|
20
|
+
'.eslintrc.cjs',
|
|
21
|
+
'.eslintrc.json',
|
|
22
|
+
'.babelrc',
|
|
23
|
+
'.babelrc.json',
|
|
24
|
+
'vitest.config.js',
|
|
25
|
+
'vitest.config.ts',
|
|
26
|
+
'tsconfig.json',
|
|
27
|
+
'.stylelintrc',
|
|
28
|
+
'.stylelintrc.json',
|
|
29
|
+
'svelte.config.js',
|
|
30
|
+
'astro.config.mjs',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
function escapeRegex(str) {
|
|
34
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isReferenced(content, depName) {
|
|
38
|
+
const pattern = new RegExp(`['"\`]${escapeRegex(depName)}['"\`/]`);
|
|
39
|
+
return pattern.test(content);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function scanConfigFiles(directory, dependencyNames) {
|
|
43
|
+
if (!Array.isArray(dependencyNames) && !dependencyNames?.[Symbol.iterator]) {
|
|
44
|
+
return new Set();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const referenced = new Set();
|
|
48
|
+
|
|
49
|
+
await Promise.all(
|
|
50
|
+
CONFIG_FILES.map(async (configFile) => {
|
|
51
|
+
try {
|
|
52
|
+
const content = await fs.promises.readFile(
|
|
53
|
+
path.join(directory, configFile), 'utf8'
|
|
54
|
+
);
|
|
55
|
+
for (const depName of dependencyNames) {
|
|
56
|
+
if (isReferenced(content, depName)) referenced.add(depName);
|
|
57
|
+
}
|
|
58
|
+
} catch {
|
|
59
|
+
// file missing or unreadable — skip
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return referenced;
|
|
65
|
+
}
|
package/src/security-checker.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import https from "https";
|
|
2
1
|
import axios from "axios";
|
|
3
2
|
|
|
4
3
|
export async function checkVulnerabilities(dependencies) {
|
|
4
|
+
if (!process.env.SNYK_API_TOKEN) {
|
|
5
|
+
return null;
|
|
6
|
+
}
|
|
7
|
+
|
|
5
8
|
const vulnerabilities = {};
|
|
6
9
|
|
|
7
10
|
for (const [depName, version] of Object.entries(dependencies)) {
|
|
@@ -20,20 +23,13 @@ export async function checkVulnerabilities(dependencies) {
|
|
|
20
23
|
return vulnerabilities;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
|
|
26
|
+
async function checkPackageVulnerabilities(packageName, version) {
|
|
24
27
|
try {
|
|
25
|
-
const SNYK_API_TOKEN = "5867f5b0-1275-4bff-99b3-c307c545b6dd";
|
|
26
|
-
if (!SNYK_API_TOKEN) {
|
|
27
|
-
throw new Error(
|
|
28
|
-
"Snyk API token is missing. Please set the SNYK_API_TOKEN environment variable."
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
28
|
const response = await axios.get(
|
|
33
29
|
`https://snyk.io/api/v1/test/npm/${packageName}/${version}`,
|
|
34
30
|
{
|
|
35
31
|
headers: {
|
|
36
|
-
Authorization: `token ${SNYK_API_TOKEN}`,
|
|
32
|
+
Authorization: `token ${process.env.SNYK_API_TOKEN}`,
|
|
37
33
|
},
|
|
38
34
|
}
|
|
39
35
|
);
|
package/src/unused-detector.js
CHANGED
|
@@ -1,71 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
];
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// Check package.json for dependencies that might be used in scripts
|
|
52
|
-
const packageJson = JSON.parse(fs.readFileSync(path.join(directory, 'package.json'), 'utf8'));
|
|
53
|
-
const scripts = packageJson.scripts || {};
|
|
54
|
-
|
|
55
|
-
if (!isUsed) {
|
|
56
|
-
// Check if it's used in npm scripts
|
|
57
|
-
isUsed = Object.values(scripts).some(script => script.includes(dep));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// If the dependency is still not found to be used, add it to the unused list
|
|
61
|
-
if (!isUsed) {
|
|
62
|
-
unusedDeps.push({
|
|
63
|
-
name: dep,
|
|
64
|
-
version,
|
|
65
|
-
reason: `Not imported or used in scripts`,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return unusedDeps;
|
|
71
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { glob } from 'glob';
|
|
4
|
+
import { parseFileImports } from './ast-parser.js';
|
|
5
|
+
import { scanConfigFiles } from './config-scanner.js';
|
|
6
|
+
|
|
7
|
+
export async function findUnusedDependencies(directory, dependencies) {
|
|
8
|
+
const allDeps = Object.keys(dependencies);
|
|
9
|
+
|
|
10
|
+
const files = await glob('**/*.{js,jsx,ts,tsx}', {
|
|
11
|
+
cwd: directory,
|
|
12
|
+
ignore: ['**/node_modules/**', '**/dist/**', '**/build/**'],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const fileSets = await Promise.all(
|
|
16
|
+
files.map(file => parseFileImports(path.join(directory, file)))
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const allDepsSet = new Set(allDeps);
|
|
20
|
+
const referenced = new Set();
|
|
21
|
+
for (const set of fileSets) {
|
|
22
|
+
for (const pkg of set) {
|
|
23
|
+
if (allDepsSet.has(pkg)) referenced.add(pkg);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const configReferenced = await scanConfigFiles(directory, allDeps);
|
|
28
|
+
for (const pkg of configReferenced) referenced.add(pkg);
|
|
29
|
+
|
|
30
|
+
const packageJson = JSON.parse(fs.readFileSync(path.join(directory, 'package.json'), 'utf8'));
|
|
31
|
+
const scripts = Object.values(packageJson.scripts || {});
|
|
32
|
+
for (const dep of allDeps) {
|
|
33
|
+
const pattern = new RegExp(`(?:^|\\s|['"./])${dep.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:$|\\s|['"./])`);
|
|
34
|
+
if (scripts.some(script => pattern.test(script))) referenced.add(dep);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const unusedDeps = [];
|
|
38
|
+
for (const dep of allDeps) {
|
|
39
|
+
if (!referenced.has(dep)) {
|
|
40
|
+
unusedDeps.push({
|
|
41
|
+
name: dep,
|
|
42
|
+
version: dependencies[dep],
|
|
43
|
+
reason: 'Not imported in any source or config file',
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return unusedDeps;
|
|
49
|
+
}
|