From d916043061ea9202418b5ba4d2d51139c1a1b728 Mon Sep 17 00:00:00 2001 From: Joakim Carlstein Date: Thu, 7 Dec 2023 14:34:47 +0100 Subject: [PATCH] fix(plugin-tools): load plugins correctly when specified as strings --- .changeset/tricky-spies-attack.md | 5 ++++ packages/plugin-tools/src/index.ts | 43 +++++++++++++++--------------- 2 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 .changeset/tricky-spies-attack.md diff --git a/.changeset/tricky-spies-attack.md b/.changeset/tricky-spies-attack.md new file mode 100644 index 0000000..20498b0 --- /dev/null +++ b/.changeset/tricky-spies-attack.md @@ -0,0 +1,5 @@ +--- +'@emigrate/plugin-tools': patch +--- + +Fix a regression issue where plugins wasn't correctly loaded if specified as strings diff --git a/packages/plugin-tools/src/index.ts b/packages/plugin-tools/src/index.ts index 4b96c71..a617b3a 100644 --- a/packages/plugin-tools/src/index.ts +++ b/packages/plugin-tools/src/index.ts @@ -71,20 +71,28 @@ export const isPluginOfType = (type: T, plugin: any): plug export const getOrLoadStorage = async ( potentialStorages: Array>, ): Promise => { - return getOrLoad(potentialStorages, isEmigrateStorage); + return getOrLoad( + potentialStorages, + ['@emigrate/storage-', 'emigrate-storage-', '@emigrate/plugin-storage-', '@emigrate/'], + isEmigrateStorage, + ); }; export const getOrLoadReporter = async ( potentialReporters: Array>, ): Promise => { - return getOrLoad(potentialReporters, isEmigrateReporter); + return getOrLoad(potentialReporters, ['@emigrate/reporter-', 'emigrate-reporter-', '@emigrate/'], isEmigrateReporter); }; export const getOrLoadPlugin = async ( type: T, plugins: Array>, ): Promise | undefined> => { - return getOrLoad(plugins, (value: unknown): value is PluginFromType => isPluginOfType(type, value)); + return getOrLoad( + plugins, + ['@emigrate/plugin-', 'emigrate-plugin-', '@emigrate/'], + (value: unknown): value is PluginFromType => isPluginOfType(type, value), + ); }; export const getOrLoadPlugins = async ( @@ -120,10 +128,18 @@ export const getOrLoadPlugins = async ( return result; }; -const getOrLoad = async (potentials: Array>, check: (value: unknown) => value is T) => { +const getOrLoad = async ( + potentials: Array>, + prefixes: string[], + check: (value: unknown) => value is T, +) => { const reversed = [...potentials].reverse(); for await (let potential of reversed) { + if (typeof potential === 'string') { + return load(potential, prefixes, check); + } + if (typeof potential === 'function') { potential = await potential(); } @@ -152,25 +168,10 @@ const getImportFromEsm = async () => { return importFromEsm; }; -export const loadStorage = async (name: string): Promise => { - return load( - name, - ['@emigrate/', '@emigrate/storage-', 'emigrate-storage-', '@emigrate/plugin-storage-'], - isEmigrateStorage, - ); -}; - -export const loadReporter = async (name: string): Promise => { - return load(name, ['@emigrate/reporter-', 'emigrate-reporter-'], isEmigrateReporter); -}; - -export const loadPlugin = async ( - type: T, - plugin: string, -): Promise | undefined> => { +const loadPlugin = async (type: T, plugin: string): Promise | undefined> => { return load( plugin, - ['@emigrate/', '@emigrate/plugin-', 'emigrate-plugin-'], + ['@emigrate/plugin-', 'emigrate-plugin-', '@emigrate/'], (value: unknown): value is PluginFromType => { return isPluginOfType(type, value); },