Skip to content

Commit

Permalink
fix: repl in pnpm environment, improve relative path module loading (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 29, 2023
1 parent ca55bf6 commit e7d29fd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
20 changes: 18 additions & 2 deletions packages/schema/src/cli/actions/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 15 additions & 7 deletions packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(providerField.value);
const provider = getLiteral<string>(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(
Expand All @@ -177,8 +187,6 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str
)
);
}
} catch {
// noop
}
}
}
Expand Down
27 changes: 19 additions & 8 deletions packages/schema/src/cli/plugin-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down Expand Up @@ -301,13 +302,23 @@ export class PluginRunner {
}

private getPluginModulePath(provider: string, options: Pick<PluginOptions, 'schemaPath'>) {
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<PluginOptions, 'schemaPath'>) {
const pluginModulePath = this.getPluginModulePath(provider, options);
return require(pluginModulePath);
}
}

0 comments on commit e7d29fd

Please sign in to comment.