From 9a99466d6a378be61902aefe54d34d643d96be88 Mon Sep 17 00:00:00 2001 From: Andy Choi Date: Fri, 28 Feb 2025 14:12:11 -0800 Subject: [PATCH] =?UTF-8?q?plugin=20=E2=80=9Cpreprocessor=E2=80=9D=20optio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds support for a “preprocessor” flag in plugin options that causes the plugin to be executed before the core zenstack plugins. This allows for plugins that modify the schema prior zenstack enhancements. --- packages/schema/src/cli/plugin-runner.ts | 46 +++++++++++++++--------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/schema/src/cli/plugin-runner.ts b/packages/schema/src/cli/plugin-runner.ts index a13ca2afc..54e13d41e 100644 --- a/packages/schema/src/cli/plugin-runner.ts +++ b/packages/schema/src/cli/plugin-runner.ts @@ -108,8 +108,14 @@ export class PluginRunner { }); } + const preprocessorPlugins = plugins.filter((p) => p.options.preprocessor); + const otherPlugins = plugins.filter((p) => !p.options.preprocessor); + // calculate all plugins (including core plugins implicitly enabled) - const { corePlugins, userPlugins } = this.calculateAllPlugins(runnerOptions, plugins); + const { corePlugins, userPlugins } = this.calculateAllPlugins( + runnerOptions, + otherPlugins, + ); const allPlugins = [...corePlugins, ...userPlugins]; // check dependencies @@ -139,6 +145,28 @@ export class PluginRunner { let prismaClientDtsPath: string | undefined = undefined; const project = createProject(); + + const runUserPlugins = async (plugins: PluginInfo[]) => { + for (const { name, description, run, options: pluginOptions } of plugins) { + const options = { ...pluginOptions, prismaClientPath, prismaClientDtsPath }; + const r = await this.runPlugin( + name, + description, + run, + runnerOptions, + options as PluginOptions, + dmmf, + shortNameMap, + project, + false + ); + warnings.push(...(r?.warnings ?? [])); // the null-check is for backward compatibility + } + }; + + // run preprocessor plugins + await runUserPlugins(preprocessorPlugins); + for (const { name, description, run, options: pluginOptions } of corePlugins) { const options = { ...pluginOptions, prismaClientPath }; const r = await this.runPlugin( @@ -175,21 +203,7 @@ export class PluginRunner { await compileProject(project, runnerOptions); // run user plugins - for (const { name, description, run, options: pluginOptions } of userPlugins) { - const options = { ...pluginOptions, prismaClientPath, prismaClientDtsPath }; - const r = await this.runPlugin( - name, - description, - run, - runnerOptions, - options as PluginOptions, - dmmf, - shortNameMap, - project, - false - ); - warnings.push(...(r?.warnings ?? [])); // the null-check is for backward compatibility - } + await runUserPlugins(userPlugins); console.log(colors.green(colors.bold('\n👻 All plugins completed successfully!'))); warnings.forEach((w) => console.warn(colors.yellow(w)));