Skip to content

Commit

Permalink
Fix up create-new-plugin script
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 6, 2025
1 parent 3c0a6c5 commit f5a1ede
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"qa": "bun lint && bun run build && bun knip && bun knip:production && bun run test",
"release": "release-it",
"create-plugin": "bun ./scripts/create-new-plugin.ts",
"postcreate-plugin": "biome format --write schema.json schema-jsonc.json src/ConfigurationValidator.ts",
"postcreate-plugin": "bun run build && biome format --write schema.json schema-jsonc.json src/ConfigurationValidator.ts",
"generate-plugin-defs": "node ./scripts/generate-plugin-defs.js && biome check --write src/plugins/index.ts src/types/PluginNames.ts src/schema/plugins.ts"
},
"files": [
Expand Down
16 changes: 2 additions & 14 deletions packages/knip/scripts/create-new-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ if (!/[a-z][a-z0-9_-]+/.test(name)) {
process.exit(1);
}

const toCamelCase = (name: string) =>
name.toLowerCase().replace(/(-[a-z])/g, group => group.toUpperCase().replace('-', ''));

const cwd = process.cwd();
const pluginsDir = path.join(cwd, 'src/plugins');
const templateDir = path.join(pluginsDir, '_template');
const newPluginDir = path.join(pluginsDir, name);
const newPluginFile = path.join(newPluginDir, 'index.ts');
const pluginsBarrelFilePath = path.join(pluginsDir, 'index.ts');
const schemaFilePath = path.join(cwd, 'schema.json');
const pluginTestsDir = path.join(cwd, 'test/plugins');
const validatorFilePath = path.join(cwd, 'src/ConfigurationValidator.ts');
Expand All @@ -39,7 +35,6 @@ const pluginTestFixturesDir = path.join(cwd, 'fixtures/plugins');
const pluginTestFixtureTemplateDir = path.join(pluginTestFixturesDir, '_template');
const pluginTestFixturePluginDir = path.join(pluginTestFixturesDir, name);
const pluginTestFixtureManifest = path.join(pluginTestFixturePluginDir, 'package.json');
const camelCasedName = toCamelCase(name);

const relative = to => path.relative(cwd, to);

Expand All @@ -50,13 +45,6 @@ await fs.cp(templateDir, newPluginDir, {
force: false,
});

// Add plugin to barrel file
const barrelFile = String(await fs.readFile(pluginsBarrelFilePath));
await fs.writeFile(
pluginsBarrelFilePath,
`${barrelFile}export { default as ${camelCasedName} } from './${name}/index.js';`
);

// Add plugin to Zod validator
const validatorContent = String(await fs.readFile(validatorFilePath));
const pluginsPrefix = 'const pluginsSchema = z.object({';
Expand All @@ -79,7 +67,7 @@ await fs.cp(pluginTestTemplateFilePath, pluginTestFilePath, {
// String replacements
for (const filePath of [newPluginFile, pluginTestFilePath, pluginTestFixtureManifest]) {
const content = String(await fs.readFile(filePath));
await fs.writeFile(filePath, content.replaceAll('_template', camelCasedName).replaceAll('__PLUGIN_NAME__', name));
await fs.writeFile(filePath, content.replaceAll('_template', name).replaceAll('__PLUGIN_NAME__', name));
}

// Add plugin to JSON Schema
Expand All @@ -101,6 +89,6 @@ await fs.writeFile(schemaFilePath, JSON.stringify(schema, null, 2));

console.log(`✔️ Created new plugin in ${relative(newPluginDir)}`);
console.log(`✔️ Created a test file at ${relative(pluginTestFilePath)}`);
console.log(`✔️ Added plugin to ${relative(pluginsBarrelFilePath)} and ${relative(schemaFilePath)}`);
console.log(`✔️ Added plugin to ${relative(schemaFilePath)}`);
console.log('');
console.log('Documentation: https://knip.dev/guides/writing-a-plugin');
2 changes: 1 addition & 1 deletion packages/knip/src/plugins/_template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { PluginConfig } from './types.js';

// link to __PLUGIN_NAME__ docs

const title = '_template';
const title = '__PLUGIN_NAME__';

const enablers = ['__PLUGIN_NAME__'];

Expand Down

0 comments on commit f5a1ede

Please sign in to comment.