Skip to content

Commit

Permalink
fix: zenstack cli errors while using bun/bunx during docker build (#1011
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ErikMCM authored Feb 19, 2024
1 parent b2e1635 commit 0704f9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
8 changes: 7 additions & 1 deletion packages/schema/src/cli/actions/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import colors from 'colors';
import path from 'path';
import prettyRepl from 'pretty-repl';
import { inspect } from 'util';

// inspired by: https://github.com/Kinjalrk2k/prisma-console
Expand All @@ -11,6 +10,13 @@ import { inspect } from 'util';
* CLI action for starting a REPL session
*/
export async function repl(projectPath: string, options: { 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.');
return;
}

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

console.log('Welcome to ZenStack REPL. See help with the ".help" command.');
console.log('Global variables:');
console.log(` ${colors.blue('db')} to access enhanced PrismaClient`);
Expand Down
10 changes: 5 additions & 5 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import stripColor from 'strip-color';
import { name } from '.';
import { getStringLiteral } from '../../language-server/validator/utils';
import telemetry from '../../telemetry';
import { execSync } from '../../utils/exec-utils';
import { execPackage } from '../../utils/exec-utils';
import { findPackageJson } from '../../utils/pkg-utils';
import {
ModelFieldType,
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class PrismaSchemaGenerator {
if (options.format === true) {
try {
// run 'prisma format'
await execSync(`npx prisma format --schema ${outFile}`);
await execPackage(`prisma format --schema ${outFile}`);
} catch {
warnings.push(`Failed to format Prisma schema file`);
}
Expand All @@ -136,18 +136,18 @@ export default class PrismaSchemaGenerator {
const generateClient = options.generateClient !== false;

if (generateClient) {
let generateCmd = `npx prisma generate --schema "${outFile}"`;
let generateCmd = `prisma generate --schema "${outFile}"`;
if (typeof options.generateArgs === 'string') {
generateCmd += ` ${options.generateArgs}`;
}
try {
// run 'prisma generate'
await execSync(generateCmd, 'ignore');
await execPackage(generateCmd, 'ignore');
} catch {
await this.trackPrismaSchemaError(outFile);
try {
// run 'prisma generate' again with output to the console
await execSync(generateCmd);
await execPackage(generateCmd);
} catch {
// noop
}
Expand Down
9 changes: 9 additions & 0 deletions packages/schema/src/utils/exec-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ export function execSync(cmd: string, stdio: StdioOptions = 'inherit', env?: Rec
const mergedEnv = { ...process.env, ...env };
_exec(cmd, { encoding: 'utf-8', stdio, env: mergedEnv });
}

/**
* Utility for running package commands through npx/bunx
*/
export function execPackage(cmd: string, stdio: StdioOptions = 'inherit', env?: Record<string, string>): void {
const packageManager = process?.versions?.bun ? 'bunx' : 'npx';
const mergedEnv = { ...process.env, ...env };
_exec(`${packageManager} ${cmd}`, { encoding: 'utf-8', stdio, env: mergedEnv });
}

0 comments on commit 0704f9d

Please sign in to comment.