Skip to content

feat(repl): add CLI option to specify custom zenstack load path #1823

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 1 commit into from
Nov 1, 2024
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
27 changes: 21 additions & 6 deletions packages/schema/src/cli/actions/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import { inspect } from 'util';
/**
* CLI action for starting a REPL session
*/
export async function repl(projectPath: string, options: { prismaClient?: string; debug?: boolean; table?: boolean }) {
export async function repl(
projectPath: string,
options: { loadPath?: string; prismaClient?: string; debug?: boolean; table?: boolean }
) {
if (!process?.stdout?.isTTY && process?.versions?.bun) {
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.');
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.'
);
return;
}

const prettyRepl = await import('pretty-repl')
const prettyRepl = await import('pretty-repl');

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

const { enhance } = require('@zenstackhq/runtime');
const { enhance } = options.loadPath
? require(path.join(path.resolve(options.loadPath), 'enhance'))
: require('@zenstackhq/runtime');

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

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

// .auth command
replServer.defineCommand('auth', {
help: 'Set current user. Run without argument to switch to anonymous. Pass an user object to set current user.',
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.',
action(value: string) {
this.clearBufferedCommand();
try {
if (!value?.trim()) {
// set anonymous
setAuth(undefined);
console.log(`Auth user: anonymous. Use ".auth { id: ... }" to change.`);
} else if (value.trim() === 'info') {
// refresh auth user
setAuth(user);
console.log(`Current user: ${user ? inspect(user) : 'anonymous'}`);
} else {
// set current user
const user = eval(`(${value})`);
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export function createProgram() {
program
.command('repl')
.description('Start a REPL session.')
.option('--prisma-client <module>', 'path to Prisma client module')
.option('--load-path <path>', 'path to load modules generated by ZenStack')
.option('--prisma-client <path>', 'path to Prisma client module')
.option('--debug', 'enable debug output')
.option('--table', 'enable table format output')
.action(replAction);
Expand Down
Loading