Skip to content

Commit e7d29fd

Browse files
authored
fix: repl in pnpm environment, improve relative path module loading (#866)
1 parent ca55bf6 commit e7d29fd

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

packages/schema/src/cli/actions/repl.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,24 @@ export async function repl(projectPath: string, options: { prismaClient?: string
2323
console.log();
2424
console.log(`Running as anonymous user. Use ".auth" to set current user.`);
2525

26-
const prismaClientModule = options.prismaClient ?? path.join(projectPath, './node_modules/.prisma/client');
27-
const { PrismaClient } = require(prismaClientModule);
26+
let PrismaClient: any;
27+
28+
const prismaClientModule = options.prismaClient ?? '@prisma/client';
29+
30+
try {
31+
// try direct require
32+
const module = require(prismaClientModule);
33+
PrismaClient = module.PrismaClient;
34+
} catch (err) {
35+
if (!path.isAbsolute(prismaClientModule)) {
36+
// try relative require
37+
const module = require(path.join(projectPath, prismaClientModule));
38+
PrismaClient = module.PrismaClient;
39+
} else {
40+
throw err;
41+
}
42+
}
43+
2844
const { enhance } = require('@zenstackhq/runtime');
2945

3046
let debug = !!options.debug;

packages/schema/src/cli/cli-util.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,24 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str
161161
if (isPlugin(decl)) {
162162
const providerField = decl.fields.find((f) => f.name === 'provider');
163163
if (providerField) {
164-
let provider = getLiteral<string>(providerField.value);
164+
const provider = getLiteral<string>(providerField.value);
165165
if (provider) {
166+
let pluginEntrance: string | undefined;
166167
try {
167-
if (provider.startsWith('.')) {
168-
// resolve relative path against the schema file
169-
provider = path.resolve(path.dirname(fileName), provider);
168+
// direct require
169+
pluginEntrance = require.resolve(provider);
170+
} catch {
171+
if (!path.isAbsolute(provider)) {
172+
// relative path
173+
try {
174+
pluginEntrance = require.resolve(path.join(path.dirname(fileName), provider));
175+
} catch {
176+
// noop
177+
}
170178
}
171-
const pluginEntrance = require.resolve(`${provider}`);
179+
}
180+
181+
if (pluginEntrance) {
172182
const pluginModelFile = path.join(path.dirname(pluginEntrance), PLUGIN_MODULE_NAME);
173183
if (fs.existsSync(pluginModelFile)) {
174184
result.push(
@@ -177,8 +187,6 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str
177187
)
178188
);
179189
}
180-
} catch {
181-
// noop
182190
}
183191
}
184192
}

packages/schema/src/cli/plugin-runner.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ export class PluginRunner {
6262
console.error(`Plugin ${pluginDecl.name} has invalid provider option`);
6363
throw new PluginError('', `Plugin ${pluginDecl.name} has invalid provider option`);
6464
}
65-
const pluginModulePath = this.getPluginModulePath(pluginProvider, options);
65+
6666
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6767
let pluginModule: any;
68+
6869
try {
69-
pluginModule = require(pluginModulePath);
70+
pluginModule = this.loadPluginModule(pluginProvider, options);
7071
} catch (err) {
71-
console.error(`Unable to load plugin module ${pluginProvider}: ${pluginModulePath}, ${err}`);
72+
console.error(`Unable to load plugin module ${pluginProvider}: ${err}`);
7273
throw new PluginError('', `Unable to load plugin module ${pluginProvider}`);
7374
}
7475

@@ -301,13 +302,23 @@ export class PluginRunner {
301302
}
302303

303304
private getPluginModulePath(provider: string, options: Pick<PluginOptions, 'schemaPath'>) {
304-
if (path.isAbsolute(provider) || provider.startsWith('.')) {
305-
return resolvePath(provider, options);
306-
}
307305
let pluginModulePath = provider;
308-
if (pluginModulePath.startsWith('@core/')) {
309-
pluginModulePath = pluginModulePath.replace(/^@core/, path.join(__dirname, '../plugins'));
306+
if (provider.startsWith('@core/')) {
307+
pluginModulePath = provider.replace(/^@core/, path.join(__dirname, '../plugins'));
308+
} else {
309+
try {
310+
// direct require
311+
require.resolve(pluginModulePath);
312+
} catch {
313+
// relative
314+
pluginModulePath = resolvePath(provider, options);
315+
}
310316
}
311317
return pluginModulePath;
312318
}
319+
320+
private loadPluginModule(provider: string, options: Pick<PluginOptions, 'schemaPath'>) {
321+
const pluginModulePath = this.getPluginModulePath(provider, options);
322+
return require(pluginModulePath);
323+
}
313324
}

0 commit comments

Comments
 (0)