Skip to content

Commit ca3acc7

Browse files
Copilotvobu
andcommitted
refactor: Extract hasPluginFile helper and improve naming
- Add hasPluginFile() helper to eliminate duplicate logic - Rename scopedPackageName to packageNameWithScope for clarity - Use consistent helper function across all plugin checks Co-authored-by: vobu <6573426+vobu@users.noreply.github.com>
1 parent 07def00 commit ca3acc7

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/commands/plugins.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ export async function loadPlugin(packageNameOrFrom?: string, fromUrl?: string):
8585
}
8686
}
8787

88+
/**
89+
* Check if a package has a c8ctl plugin file
90+
*/
91+
function hasPluginFile(packagePath: string): boolean {
92+
return existsSync(join(packagePath, 'c8ctl-plugin.js')) ||
93+
existsSync(join(packagePath, 'c8ctl-plugin.ts'));
94+
}
95+
8896
/**
8997
* Extract package name from URL or installed package
9098
* Tries to read package.json from installed package, falls back to URL parsing
@@ -103,27 +111,24 @@ function extractPackageNameFromUrl(url: string, pluginsDir: string): string {
103111
const scopePath = join(nodeModulesPath, entry);
104112
const scopedPackages = readdirSync(scopePath);
105113
for (const scopedPkg of scopedPackages) {
106-
const pkgJsonPath = join(scopePath, scopedPkg, 'package.json');
114+
const pkgPath = join(scopePath, scopedPkg);
115+
const pkgJsonPath = join(pkgPath, 'package.json');
107116
if (existsSync(pkgJsonPath)) {
108117
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
109118
// Check if this package has c8ctl-plugin file
110-
const scopedPackagePath = join(entry, scopedPkg);
111-
const hasPluginFile = existsSync(join(scopePath, scopedPkg, 'c8ctl-plugin.js')) ||
112-
existsSync(join(scopePath, scopedPkg, 'c8ctl-plugin.ts'));
113-
if (hasPluginFile && pkgJson.keywords?.includes('c8ctl')) {
119+
if (hasPluginFile(pkgPath) && pkgJson.keywords?.includes('c8ctl')) {
114120
return pkgJson.name;
115121
}
116122
}
117123
}
118124
} else {
119125
// Regular package
120-
const pkgJsonPath = join(nodeModulesPath, entry, 'package.json');
126+
const pkgPath = join(nodeModulesPath, entry);
127+
const pkgJsonPath = join(pkgPath, 'package.json');
121128
if (existsSync(pkgJsonPath)) {
122129
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
123130
// Check if this package has c8ctl-plugin file
124-
const hasPluginFile = existsSync(join(nodeModulesPath, entry, 'c8ctl-plugin.js')) ||
125-
existsSync(join(nodeModulesPath, entry, 'c8ctl-plugin.ts'));
126-
if (hasPluginFile && pkgJson.keywords?.includes('c8ctl')) {
131+
if (hasPluginFile(pkgPath) && pkgJson.keywords?.includes('c8ctl')) {
127132
return pkgJson.name;
128133
}
129134
}
@@ -204,11 +209,10 @@ export function listPlugins(): void {
204209
const scopedPackages = readdirSync(scopePath);
205210
for (const scopedPkg of scopedPackages) {
206211
if (!scopedPkg.startsWith('.')) {
207-
const scopedPackageName = join(entry, scopedPkg);
208-
const hasPluginFile = existsSync(join(nodeModulesPath, scopedPackageName, 'c8ctl-plugin.js')) ||
209-
existsSync(join(nodeModulesPath, scopedPackageName, 'c8ctl-plugin.ts'));
210-
if (hasPluginFile) {
211-
installedPlugins.add(scopedPackageName);
212+
const packageNameWithScope = `${entry}/${scopedPkg}`;
213+
const packagePath = join(nodeModulesPath, entry, scopedPkg);
214+
if (hasPluginFile(packagePath)) {
215+
installedPlugins.add(packageNameWithScope);
212216
}
213217
}
214218
}
@@ -217,9 +221,8 @@ export function listPlugins(): void {
217221
}
218222
} else {
219223
// Regular package
220-
const hasPluginFile = existsSync(join(nodeModulesPath, entry, 'c8ctl-plugin.js')) ||
221-
existsSync(join(nodeModulesPath, entry, 'c8ctl-plugin.ts'));
222-
if (hasPluginFile) {
224+
const packagePath = join(nodeModulesPath, entry);
225+
if (hasPluginFile(packagePath)) {
223226
installedPlugins.add(entry);
224227
}
225228
}

0 commit comments

Comments
 (0)