refactor: simplify plugin interfaces by getting rid of the "type" property

This is to prepare for having packages that contains multiple different plugins in the same file.
This commit is contained in:
Joakim Carlstein 2023-11-15 14:11:06 +01:00
parent 3e0ff07a64
commit 9f5abf727d
5 changed files with 26 additions and 34 deletions

View file

@ -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

View file

@ -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;

View file

@ -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 () => {
};
`,
};
});
};

View file

@ -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 = <T extends PluginType>(type: T, plugin: any): plugin is PluginFromType<T> => {

View file

@ -57,10 +57,11 @@ export type Storage = {
};
export type StoragePlugin = {
type: 'storage';
initialize(): Promise<Storage>;
initializeStorage(): Promise<Storage>;
};
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<MigrationFile>;
generateMigration(name: string): Promise<MigrationFile>;
};
export type GenerateMigrationFunction = GeneratorPlugin['generateMigration'];
export type Plugin = StoragePlugin | GeneratorPlugin;
export type PluginType = Plugin['type'];
export type PluginType = 'storage' | 'generator';
export type PluginFromType<T extends PluginType> = Extract<Plugin, { type: T }>;
export type PluginFromType<T extends PluginType> = T extends 'storage'
? StoragePlugin
: T extends 'generator'
? GeneratorPlugin
: never;