Skip to content

Commit de9bf4c

Browse files
committed
fix(enhance): use robust package.json path resolution
Address PR #130 feedback: Replace hardcoded relative path assumption with findNearestPackageJson() that walks up the directory tree. This fixes the issue where plugin analyzer would fail if run from non-standard directory structure.
1 parent 7e41f34 commit de9bf4c

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

lib/enhance/plugin-analyzer.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ const securityPatterns = require('./security-patterns');
1414
const reporter = require('./reporter');
1515
const fixer = require('./fixer');
1616

17+
/**
18+
* Find nearest package.json by walking up directory tree
19+
* @param {string} startPath - Starting directory path
20+
* @param {number} maxLevels - Maximum levels to traverse (default: 5)
21+
* @returns {string|null} Path to package.json or null if not found
22+
*/
23+
function findNearestPackageJson(startPath, maxLevels = 5) {
24+
let currentPath = path.resolve(startPath);
25+
26+
for (let i = 0; i < maxLevels; i++) {
27+
const packageJsonPath = path.join(currentPath, 'package.json');
28+
if (fs.existsSync(packageJsonPath)) {
29+
return packageJsonPath;
30+
}
31+
32+
const parentPath = path.dirname(currentPath);
33+
if (parentPath === currentPath) {
34+
// Reached root
35+
break;
36+
}
37+
currentPath = parentPath;
38+
}
39+
40+
return null;
41+
}
42+
1743
/**
1844
* Analyze a single plugin
1945
* @param {string} pluginPath - Path to plugin directory
@@ -68,10 +94,10 @@ async function analyzePlugin(pluginPath, options = {}) {
6894
}
6995
}
7096

71-
// Check package.json for version comparison
72-
const packageJsonPath = path.join(pluginPath, '..', '..', 'package.json');
97+
// Check package.json for version comparison (walk up to find it)
98+
const packageJsonPath = findNearestPackageJson(pluginPath);
7399
let packageJson = null;
74-
if (fs.existsSync(packageJsonPath)) {
100+
if (packageJsonPath) {
75101
try {
76102
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
77103
} catch (err) {

0 commit comments

Comments
 (0)