Skip to content

Commit 07def00

Browse files
Copilotvobu
andcommitted
refactor: Address code review feedback
- Simplify extractPackageNameFromUrl to scan node_modules first - Remove duplicate path logic for scoped packages - Rename packageName to scopedPackageName for clarity in listPlugins Co-authored-by: vobu <6573426+vobu@users.noreply.github.com>
1 parent 3094855 commit 07def00

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

src/commands/plugins.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,8 @@ export async function loadPlugin(packageNameOrFrom?: string, fromUrl?: string):
9090
* Tries to read package.json from installed package, falls back to URL parsing
9191
*/
9292
function extractPackageNameFromUrl(url: string, pluginsDir: string): string {
93-
// First, try to extract from URL pattern
94-
const match = url.match(/\/([^\/]+?)(\.git)?$/);
95-
const urlBasedName = match ? match[1] : url.replace(/[^a-zA-Z0-9-_@\/]/g, '-');
96-
97-
// Try to read the actual package name from package.json in node_modules
93+
// Try to scan node_modules to find the package by reading package.json
9894
try {
99-
const possiblePaths = [
100-
join(pluginsDir, 'node_modules', urlBasedName, 'package.json'),
101-
];
102-
103-
// Also check if it might be a scoped package
104-
if (urlBasedName.includes('/')) {
105-
possiblePaths.push(join(pluginsDir, 'node_modules', urlBasedName, 'package.json'));
106-
}
107-
108-
// Scan node_modules to find the package
10995
const nodeModulesPath = join(pluginsDir, 'node_modules');
11096
if (existsSync(nodeModulesPath)) {
11197
const entries = readdirSync(nodeModulesPath);
@@ -121,6 +107,7 @@ function extractPackageNameFromUrl(url: string, pluginsDir: string): string {
121107
if (existsSync(pkgJsonPath)) {
122108
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
123109
// Check if this package has c8ctl-plugin file
110+
const scopedPackagePath = join(entry, scopedPkg);
124111
const hasPluginFile = existsSync(join(scopePath, scopedPkg, 'c8ctl-plugin.js')) ||
125112
existsSync(join(scopePath, scopedPkg, 'c8ctl-plugin.ts'));
126113
if (hasPluginFile && pkgJson.keywords?.includes('c8ctl')) {
@@ -144,11 +131,12 @@ function extractPackageNameFromUrl(url: string, pluginsDir: string): string {
144131
}
145132
}
146133
} catch (error) {
147-
// Fall through to URL-based name
134+
// Fall through to URL-based name extraction
148135
}
149136

150-
// Fallback to URL-based name
151-
return urlBasedName;
137+
// Fallback: extract from URL pattern
138+
const match = url.match(/\/([^\/]+?)(\.git)?$/);
139+
return match ? match[1] : url.replace(/[^a-zA-Z0-9-_@\/]/g, '-');
152140
}
153141

154142
/**
@@ -216,11 +204,11 @@ export function listPlugins(): void {
216204
const scopedPackages = readdirSync(scopePath);
217205
for (const scopedPkg of scopedPackages) {
218206
if (!scopedPkg.startsWith('.')) {
219-
const packageName = join(entry, scopedPkg);
220-
const hasPluginFile = existsSync(join(nodeModulesPath, packageName, 'c8ctl-plugin.js')) ||
221-
existsSync(join(nodeModulesPath, packageName, 'c8ctl-plugin.ts'));
207+
const scopedPackageName = join(entry, scopedPkg);
208+
const hasPluginFile = existsSync(join(nodeModulesPath, scopedPackageName, 'c8ctl-plugin.js')) ||
209+
existsSync(join(nodeModulesPath, scopedPackageName, 'c8ctl-plugin.ts'));
222210
if (hasPluginFile) {
223-
installedPlugins.add(packageName);
211+
installedPlugins.add(scopedPackageName);
224212
}
225213
}
226214
}

0 commit comments

Comments
 (0)