From 040a5f60410de47e7490026f4a31fa1924406908 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:49:05 -0800 Subject: [PATCH 1/2] fix: repl in pnpm environment, improve relative path module loading --- packages/schema/src/cli/actions/repl.ts | 20 +++++++++++++++-- packages/schema/src/cli/cli-util.ts | 22 +++++++++++++------ packages/schema/src/cli/plugin-runner.ts | 28 +++++++++++++++++------- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/packages/schema/src/cli/actions/repl.ts b/packages/schema/src/cli/actions/repl.ts index b7329bee4..8c40089b0 100644 --- a/packages/schema/src/cli/actions/repl.ts +++ b/packages/schema/src/cli/actions/repl.ts @@ -23,8 +23,24 @@ export async function repl(projectPath: string, options: { prismaClient?: string console.log(); console.log(`Running as anonymous user. Use ".auth" to set current user.`); - const prismaClientModule = options.prismaClient ?? path.join(projectPath, './node_modules/.prisma/client'); - const { PrismaClient } = require(prismaClientModule); + let PrismaClient: any; + + const prismaClientModule = options.prismaClient ?? '@prisma/client'; + + try { + // try direct require + const module = require(prismaClientModule); + PrismaClient = module.PrismaClient; + } catch (err) { + if (!path.isAbsolute(prismaClientModule)) { + // try relative require + const module = require(path.join(projectPath, prismaClientModule)); + PrismaClient = module.PrismaClient; + } else { + throw err; + } + } + const { enhance } = require('@zenstackhq/runtime'); let debug = !!options.debug; diff --git a/packages/schema/src/cli/cli-util.ts b/packages/schema/src/cli/cli-util.ts index d183e9be2..f7e090990 100644 --- a/packages/schema/src/cli/cli-util.ts +++ b/packages/schema/src/cli/cli-util.ts @@ -161,14 +161,24 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str if (isPlugin(decl)) { const providerField = decl.fields.find((f) => f.name === 'provider'); if (providerField) { - let provider = getLiteral(providerField.value); + const provider = getLiteral(providerField.value); if (provider) { + let pluginEntrance: string | undefined; try { - if (provider.startsWith('.')) { - // resolve relative path against the schema file - provider = path.resolve(path.dirname(fileName), provider); + // direct require + pluginEntrance = require.resolve(provider); + } catch { + if (!path.isAbsolute(provider)) { + // relative path + try { + pluginEntrance = require.resolve(path.join(path.dirname(fileName), provider)); + } catch { + // noop + } } - const pluginEntrance = require.resolve(`${provider}`); + } + + if (pluginEntrance) { const pluginModelFile = path.join(path.dirname(pluginEntrance), PLUGIN_MODULE_NAME); if (fs.existsSync(pluginModelFile)) { result.push( @@ -177,8 +187,6 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str ) ); } - } catch { - // noop } } } diff --git a/packages/schema/src/cli/plugin-runner.ts b/packages/schema/src/cli/plugin-runner.ts index f2a3f92a1..14ecfe7c0 100644 --- a/packages/schema/src/cli/plugin-runner.ts +++ b/packages/schema/src/cli/plugin-runner.ts @@ -62,13 +62,14 @@ export class PluginRunner { console.error(`Plugin ${pluginDecl.name} has invalid provider option`); throw new PluginError('', `Plugin ${pluginDecl.name} has invalid provider option`); } - const pluginModulePath = this.getPluginModulePath(pluginProvider, options); + // eslint-disable-next-line @typescript-eslint/no-explicit-any let pluginModule: any; + try { - pluginModule = require(pluginModulePath); + pluginModule = this.loadPluginModule(pluginProvider, options); } catch (err) { - console.error(`Unable to load plugin module ${pluginProvider}: ${pluginModulePath}, ${err}`); + console.error(`Unable to load plugin module ${pluginProvider}: ${err}`); throw new PluginError('', `Unable to load plugin module ${pluginProvider}`); } @@ -117,7 +118,7 @@ export class PluginRunner { plugins.unshift(existing); } else { // synthesize a plugin and insert front - const pluginModule = require(this.getPluginModulePath(corePlugin.provider, options)); + const pluginModule = require(this.getCorePluginPath(corePlugin.provider)); const pluginName = this.getPluginName(pluginModule, corePlugin.provider); plugins.unshift({ name: pluginName, @@ -300,14 +301,25 @@ export class PluginRunner { } } - private getPluginModulePath(provider: string, options: Pick) { - if (path.isAbsolute(provider) || provider.startsWith('.')) { - return resolvePath(provider, options); - } + private getCorePluginPath(provider: string) { let pluginModulePath = provider; if (pluginModulePath.startsWith('@core/')) { pluginModulePath = pluginModulePath.replace(/^@core/, path.join(__dirname, '../plugins')); } return pluginModulePath; } + + private loadPluginModule(provider: string, options: Pick) { + try { + // direct require + return require(provider); + } catch (err) { + if (!path.isAbsolute(provider)) { + // relative path + return require(resolvePath(provider, options)); + } else { + throw err; + } + } + } } From e6b4faf603f58750b0d5b2712e144cb03e48b9b0 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:31:54 -0800 Subject: [PATCH 2/2] update --- packages/schema/src/cli/plugin-runner.ts | 29 ++++++++++++------------ 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/schema/src/cli/plugin-runner.ts b/packages/schema/src/cli/plugin-runner.ts index 14ecfe7c0..d76d43522 100644 --- a/packages/schema/src/cli/plugin-runner.ts +++ b/packages/schema/src/cli/plugin-runner.ts @@ -118,7 +118,7 @@ export class PluginRunner { plugins.unshift(existing); } else { // synthesize a plugin and insert front - const pluginModule = require(this.getCorePluginPath(corePlugin.provider)); + const pluginModule = require(this.getPluginModulePath(corePlugin.provider, options)); const pluginName = this.getPluginName(pluginModule, corePlugin.provider); plugins.unshift({ name: pluginName, @@ -301,25 +301,24 @@ export class PluginRunner { } } - private getCorePluginPath(provider: string) { + private getPluginModulePath(provider: string, options: Pick) { let pluginModulePath = provider; - if (pluginModulePath.startsWith('@core/')) { - pluginModulePath = pluginModulePath.replace(/^@core/, path.join(__dirname, '../plugins')); + if (provider.startsWith('@core/')) { + pluginModulePath = provider.replace(/^@core/, path.join(__dirname, '../plugins')); + } else { + try { + // direct require + require.resolve(pluginModulePath); + } catch { + // relative + pluginModulePath = resolvePath(provider, options); + } } return pluginModulePath; } private loadPluginModule(provider: string, options: Pick) { - try { - // direct require - return require(provider); - } catch (err) { - if (!path.isAbsolute(provider)) { - // relative path - return require(resolvePath(provider, options)); - } else { - throw err; - } - } + const pluginModulePath = this.getPluginModulePath(provider, options); + return require(pluginModulePath); } }