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..d76d43522 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}`); } @@ -301,13 +302,23 @@ export class PluginRunner { } private getPluginModulePath(provider: string, options: Pick) { - if (path.isAbsolute(provider) || provider.startsWith('.')) { - return resolvePath(provider, options); - } 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) { + const pluginModulePath = this.getPluginModulePath(provider, options); + return require(pluginModulePath); + } }