diff --git a/.changeset/yellow-students-compare.md b/.changeset/yellow-students-compare.md new file mode 100644 index 0000000..3f767ae --- /dev/null +++ b/.changeset/yellow-students-compare.md @@ -0,0 +1,7 @@ +--- +'@emigrate/plugin-generate-js': minor +'@emigrate/plugin-tools': minor +'emigrate': minor +--- + +Simplify plugin interfaces by getting rid of the "type" string, in preparation for having packages that contains multiple different plugins diff --git a/packages/emigrate/src/new-command.ts b/packages/emigrate/src/new-command.ts index 3ee2d43..caf7a05 100644 --- a/packages/emigrate/src/new-command.ts +++ b/packages/emigrate/src/new-command.ts @@ -62,7 +62,7 @@ export default async function newCommand({ directory, template, plugins, name, e throw new Error('No generator plugin found, please specify a generator plugin using the plugin option'); } - const generated = await generatorPlugin.generate(name); + const generated = await generatorPlugin.generateMigration(name); filename = generated.filename; content = generated.content; diff --git a/packages/plugin-generate-js/src/index.ts b/packages/plugin-generate-js/src/index.ts index b94f80e..6d4dec3 100644 --- a/packages/plugin-generate-js/src/index.ts +++ b/packages/plugin-generate-js/src/index.ts @@ -1,6 +1,7 @@ -import { createGeneratorPlugin, getTimestampPrefix, sanitizeMigrationName } from '@emigrate/plugin-tools'; +import { getTimestampPrefix, sanitizeMigrationName } from '@emigrate/plugin-tools'; +import { type GenerateMigrationFunction } from '@emigrate/plugin-tools/types'; -export default createGeneratorPlugin(async (name) => { +export const generateMigration: GenerateMigrationFunction = async (name) => { return { filename: `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.js`, content: `// ${name} @@ -9,4 +10,4 @@ export default async () => { }; `, }; -}); +}; diff --git a/packages/plugin-tools/src/index.ts b/packages/plugin-tools/src/index.ts index f3d7b83..a96e8ac 100644 --- a/packages/plugin-tools/src/index.ts +++ b/packages/plugin-tools/src/index.ts @@ -1,30 +1,12 @@ import process from 'node:process'; import { type PluginFromType, type PluginType, type GeneratorPlugin, type StoragePlugin } from './types.js'; -export const createStoragePlugin = (initialize: StoragePlugin['initialize']): StoragePlugin => { - return { - type: 'storage', - initialize, - }; -}; - -export const createGeneratorPlugin = (generate: GeneratorPlugin['generate']): GeneratorPlugin => { - return { - type: 'generator', - generate, - }; -}; - export const isGeneratorPlugin = (plugin: any): plugin is GeneratorPlugin => { if (!plugin || typeof plugin !== 'object') { return false; } - if (plugin.type === 'generator') { - return typeof plugin.generate === 'function'; - } - - return false; + return typeof plugin.generateMigration === 'function'; }; export const isStoragePlugin = (plugin: any): plugin is StoragePlugin => { @@ -32,11 +14,7 @@ export const isStoragePlugin = (plugin: any): plugin is StoragePlugin => { return false; } - if (plugin.type === 'storage') { - return typeof plugin.initialize === 'function'; - } - - return false; + return typeof plugin.initializeStorage === 'function'; }; export const isPluginOfType = (type: T, plugin: any): plugin is PluginFromType => { diff --git a/packages/plugin-tools/src/types.ts b/packages/plugin-tools/src/types.ts index c56f549..2847dc2 100644 --- a/packages/plugin-tools/src/types.ts +++ b/packages/plugin-tools/src/types.ts @@ -57,10 +57,11 @@ export type Storage = { }; export type StoragePlugin = { - type: 'storage'; - initialize(): Promise; + initializeStorage(): Promise; }; +export type InitializeStorageFunction = StoragePlugin['initializeStorage']; + export type MigrationFile = { /** * The complete filename of the migration file, including the extension. @@ -75,18 +76,23 @@ export type MigrationFile = { }; export type GeneratorPlugin = { - type: 'generator'; /** * Used to generate a new migration file. * * @param name The name of the migration that should be generated (provided as arguments to the CLI) * @returns The generated migration file. */ - generate(name: string): Promise; + generateMigration(name: string): Promise; }; +export type GenerateMigrationFunction = GeneratorPlugin['generateMigration']; + export type Plugin = StoragePlugin | GeneratorPlugin; -export type PluginType = Plugin['type']; +export type PluginType = 'storage' | 'generator'; -export type PluginFromType = Extract; +export type PluginFromType = T extends 'storage' + ? StoragePlugin + : T extends 'generator' + ? GeneratorPlugin + : never;