Skip to content

fix: repl in pnpm environment, improve relative path module loading #866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}