From 42ffcf6d4997abb7ec44b095522383e7b74dc01b Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Tue, 13 Feb 2024 22:33:44 -0600 Subject: [PATCH 1/7] fix: use bun/bunx when running on bun --- packages/misc/redwood/src/cli-passthrough.ts | 6 ++++-- .../schema/src/plugins/prisma/schema-generator.ts | 6 ++++-- packages/testtools/src/schema.ts | 15 +++++++++------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/misc/redwood/src/cli-passthrough.ts b/packages/misc/redwood/src/cli-passthrough.ts index 367b5f06f..622e1fec7 100644 --- a/packages/misc/redwood/src/cli-passthrough.ts +++ b/packages/misc/redwood/src/cli-passthrough.ts @@ -15,13 +15,15 @@ async function runCommand(command: string, options: any) { } } + const packageExec = process?.versions?.bun ? 'bunx' : 'npx'; + console.log(); console.log(colors.green('Running ZenStack CLI...')); - console.log(colors.underline('$ npx ' + args.join(' '))); + console.log(colors.underline(`$ ${packageExec} ` + args.join(' '))); console.log(); try { - await execa('npx', args, { cwd: getPaths().api.base, shell: true, stdio: 'inherit', cleanup: true }); + await execa(packageExec, args, { cwd: getPaths().api.base, shell: true, stdio: 'inherit', cleanup: true }); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (e: any) { process.exit(e?.exitCode || 1); diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 2aa426b57..5ff6d25cf 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -124,10 +124,12 @@ export default class PrismaSchemaGenerator { } await writeFile(outFile, this.PRELUDE + prisma.toString()); + const packageExec = process?.versions?.bun ? 'bunx' : 'npx'; + if (options.format === true) { try { // run 'prisma format' - await execSync(`npx prisma format --schema ${outFile}`); + await execSync(`${packageExec} prisma format --schema ${outFile}`); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -136,7 +138,7 @@ export default class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `npx prisma generate --schema "${outFile}"`; + let generateCmd = `${packageExec} prisma generate --schema "${outFile}"`; if (typeof options.generateArgs === 'string') { generateCmd += ` ${options.generateArgs}`; } diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index f69a845cc..d3b3f42a6 100644 --- a/packages/testtools/src/schema.ts +++ b/packages/testtools/src/schema.ts @@ -189,20 +189,23 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { } } - run('npm install'); + const packageManager = process?.versions?.bun ? 'bun' : 'npm'; + const packageExec = packageManager === 'bun' ? 'bunx' : 'npx'; + + run(`${packageManager} install`); const outputArg = opt.output ? ` --output ${opt.output}` : ''; if (opt.customSchemaFilePath) { - run(`npx zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, { + run(`${packageExec} zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules', }); } else { - run(`npx zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' }); + run(`${packageExec} zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' }); } if (opt.pushDb) { - run('npx prisma db push'); + run(`${packageExec} prisma db push`); } if (opt.pulseApiKey) { @@ -233,7 +236,7 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { if (opt.compile) { console.log('Compiling...'); - run('npx tsc --init'); + run(`${packageExec} tsc --init`); // add generated '.zenstack/zod' folder to typescript's search path, // so that it can be resolved from symbolic-linked files @@ -242,7 +245,7 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { '.zenstack/zod/input': ['./node_modules/.zenstack/zod/input/index.d.ts'], }; fs.writeFileSync(path.join(projectRoot, './tsconfig.json'), JSON.stringify(tsconfig, null, 2)); - run('npx tsc --project tsconfig.json'); + run(`${packageExec} tsc --project tsconfig.json`); } if (options?.getPrismaOnly) { From 8d5958d1794c449185c0eb2961307a10907d47b4 Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Tue, 13 Feb 2024 22:35:12 -0600 Subject: [PATCH 2/7] fix: disable repl if bun is non-TTY terminal --- packages/schema/src/cli/actions/repl.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/schema/src/cli/actions/repl.ts b/packages/schema/src/cli/actions/repl.ts index 6ca3c3503..1b00d1b8e 100644 --- a/packages/schema/src/cli/actions/repl.ts +++ b/packages/schema/src/cli/actions/repl.ts @@ -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 @@ -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 is only available in TTY mode, 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`); From 02ea36b41ed844b26ed78c6d3dc97bc6bb38a6e2 Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Fri, 16 Feb 2024 19:58:28 -0600 Subject: [PATCH 3/7] Revert "fix: use bun/bunx when running on bun" This reverts commit 42ffcf6d4997abb7ec44b095522383e7b74dc01b. --- packages/misc/redwood/src/cli-passthrough.ts | 6 ++---- .../schema/src/plugins/prisma/schema-generator.ts | 6 ++---- packages/testtools/src/schema.ts | 15 ++++++--------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/packages/misc/redwood/src/cli-passthrough.ts b/packages/misc/redwood/src/cli-passthrough.ts index 622e1fec7..367b5f06f 100644 --- a/packages/misc/redwood/src/cli-passthrough.ts +++ b/packages/misc/redwood/src/cli-passthrough.ts @@ -15,15 +15,13 @@ async function runCommand(command: string, options: any) { } } - const packageExec = process?.versions?.bun ? 'bunx' : 'npx'; - console.log(); console.log(colors.green('Running ZenStack CLI...')); - console.log(colors.underline(`$ ${packageExec} ` + args.join(' '))); + console.log(colors.underline('$ npx ' + args.join(' '))); console.log(); try { - await execa(packageExec, args, { cwd: getPaths().api.base, shell: true, stdio: 'inherit', cleanup: true }); + await execa('npx', args, { cwd: getPaths().api.base, shell: true, stdio: 'inherit', cleanup: true }); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (e: any) { process.exit(e?.exitCode || 1); diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 5ff6d25cf..2aa426b57 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -124,12 +124,10 @@ export default class PrismaSchemaGenerator { } await writeFile(outFile, this.PRELUDE + prisma.toString()); - const packageExec = process?.versions?.bun ? 'bunx' : 'npx'; - if (options.format === true) { try { // run 'prisma format' - await execSync(`${packageExec} prisma format --schema ${outFile}`); + await execSync(`npx prisma format --schema ${outFile}`); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -138,7 +136,7 @@ export default class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `${packageExec} prisma generate --schema "${outFile}"`; + let generateCmd = `npx prisma generate --schema "${outFile}"`; if (typeof options.generateArgs === 'string') { generateCmd += ` ${options.generateArgs}`; } diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index d3b3f42a6..f69a845cc 100644 --- a/packages/testtools/src/schema.ts +++ b/packages/testtools/src/schema.ts @@ -189,23 +189,20 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { } } - const packageManager = process?.versions?.bun ? 'bun' : 'npm'; - const packageExec = packageManager === 'bun' ? 'bunx' : 'npx'; - - run(`${packageManager} install`); + run('npm install'); const outputArg = opt.output ? ` --output ${opt.output}` : ''; if (opt.customSchemaFilePath) { - run(`${packageExec} zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, { + run(`npx zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules', }); } else { - run(`${packageExec} zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' }); + run(`npx zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' }); } if (opt.pushDb) { - run(`${packageExec} prisma db push`); + run('npx prisma db push'); } if (opt.pulseApiKey) { @@ -236,7 +233,7 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { if (opt.compile) { console.log('Compiling...'); - run(`${packageExec} tsc --init`); + run('npx tsc --init'); // add generated '.zenstack/zod' folder to typescript's search path, // so that it can be resolved from symbolic-linked files @@ -245,7 +242,7 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { '.zenstack/zod/input': ['./node_modules/.zenstack/zod/input/index.d.ts'], }; fs.writeFileSync(path.join(projectRoot, './tsconfig.json'), JSON.stringify(tsconfig, null, 2)); - run(`${packageExec} tsc --project tsconfig.json`); + run('npx tsc --project tsconfig.json'); } if (options?.getPrismaOnly) { From ae0c4a9b5039cdf2d1caeab77cfa6465eceb2e3e Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Fri, 16 Feb 2024 19:59:08 -0600 Subject: [PATCH 4/7] fix: use bun/bunx when running on bun for schema --- packages/schema/src/plugins/prisma/schema-generator.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 2aa426b57..5ff6d25cf 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -124,10 +124,12 @@ export default class PrismaSchemaGenerator { } await writeFile(outFile, this.PRELUDE + prisma.toString()); + const packageExec = process?.versions?.bun ? 'bunx' : 'npx'; + if (options.format === true) { try { // run 'prisma format' - await execSync(`npx prisma format --schema ${outFile}`); + await execSync(`${packageExec} prisma format --schema ${outFile}`); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -136,7 +138,7 @@ export default class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `npx prisma generate --schema "${outFile}"`; + let generateCmd = `${packageExec} prisma generate --schema "${outFile}"`; if (typeof options.generateArgs === 'string') { generateCmd += ` ${options.generateArgs}`; } From c347065cf35ffd955203cd84b842b27722c809d4 Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Fri, 16 Feb 2024 22:03:11 -0600 Subject: [PATCH 5/7] style: rename package manager variable --- packages/schema/src/plugins/prisma/schema-generator.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 5ff6d25cf..60baffb1c 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -124,12 +124,12 @@ export default class PrismaSchemaGenerator { } await writeFile(outFile, this.PRELUDE + prisma.toString()); - const packageExec = process?.versions?.bun ? 'bunx' : 'npx'; + const packageManager = process?.versions?.bun ? 'bunx' : 'npx'; if (options.format === true) { try { // run 'prisma format' - await execSync(`${packageExec} prisma format --schema ${outFile}`); + await execSync(`${packageManager} prisma format --schema ${outFile}`); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -138,7 +138,7 @@ export default class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `${packageExec} prisma generate --schema "${outFile}"`; + let generateCmd = `${packageManager} prisma generate --schema "${outFile}"`; if (typeof options.generateArgs === 'string') { generateCmd += ` ${options.generateArgs}`; } From 8198e3ad7a92ce6103ce1622726671b0a144a446 Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Fri, 16 Feb 2024 22:03:44 -0600 Subject: [PATCH 6/7] style: update repl on bun in non-tty error message --- packages/schema/src/cli/actions/repl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/schema/src/cli/actions/repl.ts b/packages/schema/src/cli/actions/repl.ts index 1b00d1b8e..df15e30fb 100644 --- a/packages/schema/src/cli/actions/repl.ts +++ b/packages/schema/src/cli/actions/repl.ts @@ -11,7 +11,7 @@ import { inspect } from 'util'; */ export async function repl(projectPath: string, options: { prismaClient?: string; debug?: boolean; table?: boolean }) { if (!process?.stdout?.isTTY && process?.versions?.bun) { - console.error('REPL is only available in TTY mode, 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; } From 7a1f349f2a0920f8760603621b4c82e0c195f1c9 Mon Sep 17 00:00:00 2001 From: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Date: Mon, 19 Feb 2024 09:00:07 -0600 Subject: [PATCH 7/7] fix: implement execPackage function for schema --- .../schema/src/plugins/prisma/schema-generator.ts | 12 +++++------- packages/schema/src/utils/exec-utils.ts | 9 +++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 60baffb1c..881983caa 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -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, @@ -124,12 +124,10 @@ export default class PrismaSchemaGenerator { } await writeFile(outFile, this.PRELUDE + prisma.toString()); - const packageManager = process?.versions?.bun ? 'bunx' : 'npx'; - if (options.format === true) { try { // run 'prisma format' - await execSync(`${packageManager} prisma format --schema ${outFile}`); + await execPackage(`prisma format --schema ${outFile}`); } catch { warnings.push(`Failed to format Prisma schema file`); } @@ -138,18 +136,18 @@ export default class PrismaSchemaGenerator { const generateClient = options.generateClient !== false; if (generateClient) { - let generateCmd = `${packageManager} 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 } diff --git a/packages/schema/src/utils/exec-utils.ts b/packages/schema/src/utils/exec-utils.ts index f355ae2b4..8f0508dbb 100644 --- a/packages/schema/src/utils/exec-utils.ts +++ b/packages/schema/src/utils/exec-utils.ts @@ -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): void { + const packageManager = process?.versions?.bun ? 'bunx' : 'npx'; + const mergedEnv = { ...process.env, ...env }; + _exec(`${packageManager} ${cmd}`, { encoding: 'utf-8', stdio, env: mergedEnv }); +} \ No newline at end of file