Skip to content

Commit d985a73

Browse files
authored
feat(repl): add CLI option to specify custom zenstack load path (#1823)
1 parent 00ecb2a commit d985a73

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import { inspect } from 'util';
99
/**
1010
* CLI action for starting a REPL session
1111
*/
12-
export async function repl(projectPath: string, options: { prismaClient?: string; debug?: boolean; table?: boolean }) {
12+
export async function repl(
13+
projectPath: string,
14+
options: { loadPath?: string; prismaClient?: string; debug?: boolean; table?: boolean }
15+
) {
1316
if (!process?.stdout?.isTTY && process?.versions?.bun) {
14-
console.error('REPL on Bun is only available in a TTY terminal at this time. Please use npm/npx to run the command in this context instead of bun/bunx.');
17+
console.error(
18+
'REPL on Bun is only available in a TTY terminal at this time. Please use npm/npx to run the command in this context instead of bun/bunx.'
19+
);
1520
return;
1621
}
1722

18-
const prettyRepl = await import('pretty-repl')
23+
const prettyRepl = await import('pretty-repl');
1924

2025
console.log('Welcome to ZenStack REPL. See help with the ".help" command.');
2126
console.log('Global variables:');
@@ -47,7 +52,9 @@ export async function repl(projectPath: string, options: { prismaClient?: string
4752
}
4853
}
4954

50-
const { enhance } = require('@zenstackhq/runtime');
55+
const { enhance } = options.loadPath
56+
? require(path.join(path.resolve(options.loadPath), 'enhance'))
57+
: require('@zenstackhq/runtime');
5158

5259
let debug = !!options.debug;
5360
let table = !!options.table;
@@ -63,7 +70,11 @@ export async function repl(projectPath: string, options: { prismaClient?: string
6370
let r: any = undefined;
6471
let isPrismaCall = false;
6572

66-
if (cmd.includes('await ')) {
73+
if (/^\s*user\s*=[^=]/.test(cmd)) {
74+
// assigning to user variable, reset auth
75+
eval(cmd);
76+
setAuth(user);
77+
} else if (/^\s*await\s+/.test(cmd)) {
6778
// eval can't handle top-level await, so we wrap it in an async function
6879
cmd = `(async () => (${cmd}))()`;
6980
r = eval(cmd);
@@ -137,14 +148,18 @@ export async function repl(projectPath: string, options: { prismaClient?: string
137148

138149
// .auth command
139150
replServer.defineCommand('auth', {
140-
help: 'Set current user. Run without argument to switch to anonymous. Pass an user object to set current user.',
151+
help: 'Set current user. Run without argument to switch to anonymous. Pass an user object to set current user. Run ".auth info" to show current user.',
141152
action(value: string) {
142153
this.clearBufferedCommand();
143154
try {
144155
if (!value?.trim()) {
145156
// set anonymous
146157
setAuth(undefined);
147158
console.log(`Auth user: anonymous. Use ".auth { id: ... }" to change.`);
159+
} else if (value.trim() === 'info') {
160+
// refresh auth user
161+
setAuth(user);
162+
console.log(`Current user: ${user ? inspect(user) : 'anonymous'}`);
148163
} else {
149164
// set current user
150165
const user = eval(`(${value})`);

packages/schema/src/cli/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ export function createProgram() {
133133
program
134134
.command('repl')
135135
.description('Start a REPL session.')
136-
.option('--prisma-client <module>', 'path to Prisma client module')
136+
.option('--load-path <path>', 'path to load modules generated by ZenStack')
137+
.option('--prisma-client <path>', 'path to Prisma client module')
137138
.option('--debug', 'enable debug output')
138139
.option('--table', 'enable table format output')
139140
.action(replAction);

0 commit comments

Comments
 (0)