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.
@@ -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
+ }
@@ -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
- export async function checkPackageVulnerabilities(packageName, version) {
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
  );
@@ -1,71 +1,49 @@
1
- //const fs = require('fs');
2
- //const path = require('path');
3
- //const glob = require('glob');
4
-
5
- import fs from "fs"
6
- import path from "path"
7
- import { glob } from "glob"
8
-
9
- export async function findUnusedDependencies(directory, dependencies) {
10
- const unusedDeps = [];
11
- const allDeps = Object.keys(dependencies);
12
-
13
- // Get all JavaScript/TypeScript files in the project
14
- const files = glob.sync('**/*.{js,jsx,ts,tsx}', {
15
- cwd: directory,
16
- ignore: ['**/node_modules/**', '**/dist/**', '**/build/**'],
17
- });
18
-
19
- const fileContents = files.map(file =>
20
- fs.readFileSync(path.join(directory, file), 'utf8')
21
- );
22
-
23
- allDeps.forEach(dep => {
24
- const version = dependencies[dep];
25
- let isUsed = false;
26
-
27
-
28
- const importPatterns = [
29
- new RegExp(`import.*from\\s+['"]${dep}['\"]`),
30
- new RegExp(`import\\s+['"]${dep}['\"]`),
31
- new RegExp(`require\\s*\\(\\s*['"]${dep}['\"]\\s*\\)`),
32
- new RegExp(`import\\s*\\(\\s*['"]${dep}['\"]\\s*\\)`),
33
- ];
34
-
35
- // Also check for submodule imports
36
- const submodulePattern = new RegExp(`import.*from\\s+['"]${dep}/`);
37
- const submoduleRequirePattern = new RegExp(`require\\s*\\(\\s*['"]${dep}/`);
38
-
39
- // Check if the dependency is mentioned in any file
40
- for (const content of fileContents) {
41
- if (
42
- importPatterns.some(pattern => pattern.test(content)) ||
43
- submodulePattern.test(content) ||
44
- submoduleRequirePattern.test(content)
45
- ) {
46
- isUsed = true;
47
- break;
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
+ }