From e20269ed98d790d535012ec3c944a53687d8b3a8 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sat, 30 Aug 2025 18:10:51 +0800 Subject: [PATCH 1/2] chore: misc CLI improvements --- packages/cli/src/actions/generate.ts | 23 ++++++++++------ packages/cli/src/index.ts | 3 ++- packages/create-zenstack/src/index.ts | 39 ++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/packages/cli/src/actions/generate.ts b/packages/cli/src/actions/generate.ts index f0041c3d..51291ff7 100644 --- a/packages/cli/src/actions/generate.ts +++ b/packages/cli/src/actions/generate.ts @@ -4,7 +4,7 @@ import { getLiteral, getLiteralArray } from '@zenstackhq/language/utils'; import { type CliPlugin } from '@zenstackhq/sdk'; import colors from 'colors'; import path from 'node:path'; -import ora from 'ora'; +import ora, { type Ora } from 'ora'; import { CliError } from '../cli-error'; import * as corePlugins from '../plugins'; import { getPkgJsonConfig, getSchemaFile, loadSchemaDocument } from './action-utils'; @@ -12,6 +12,7 @@ import { getPkgJsonConfig, getSchemaFile, loadSchemaDocument } from './action-ut type Options = { schema?: string; output?: string; + silent: boolean; }; /** @@ -25,10 +26,11 @@ export async function run(options: Options) { const model = await loadSchemaDocument(schemaFile); const outputPath = getOutputPath(options, schemaFile); - await runPlugins(schemaFile, model, outputPath); + await runPlugins(schemaFile, model, outputPath, options); - console.log(colors.green(`Generation completed successfully in ${Date.now() - start}ms.\n`)); - console.log(`You can now create a ZenStack client with it. + if (!options.silent) { + console.log(colors.green(`Generation completed successfully in ${Date.now() - start}ms.\n`)); + console.log(`You can now create a ZenStack client with it. \`\`\`ts import { ZenStackClient } from '@zenstackhq/runtime'; @@ -38,6 +40,7 @@ const client = new ZenStackClient(schema, { dialect: { ... } }); \`\`\``); + } } function getOutputPath(options: Options, schemaFile: string) { @@ -52,7 +55,7 @@ function getOutputPath(options: Options, schemaFile: string) { } } -async function runPlugins(schemaFile: string, model: Model, outputPath: string) { +async function runPlugins(schemaFile: string, model: Model, outputPath: string, options: Options) { const plugins = model.declarations.filter(isPlugin); const processedPlugins: { cliPlugin: CliPlugin; pluginOptions: Record }[] = []; @@ -95,7 +98,11 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string) ); // run plugin generator - const spinner = ora(cliPlugin.statusText ?? `Running plugin ${cliPlugin.name}`).start(); + let spinner: Ora | undefined; + + if (!options.silent) { + spinner = ora(cliPlugin.statusText ?? `Running plugin ${cliPlugin.name}`).start(); + } try { await cliPlugin.generate({ schemaFile, @@ -103,9 +110,9 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string) defaultOutputPath: outputPath, pluginOptions, }); - spinner.succeed(); + spinner?.succeed(); } catch (err) { - spinner.fail(); + spinner?.fail(); console.error(err); } } diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 7b17a37e..ac55ae6c 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -30,7 +30,7 @@ const checkAction = async (options: Parameters[0]): Promis }; export function createProgram() { - const program = new Command('zenstack'); + const program = new Command('zen'); program.version(getVersion()!, '-v --version', 'display CLI version'); @@ -55,6 +55,7 @@ export function createProgram() { .description('Run code generation plugins.') .addOption(schemaOption) .addOption(new Option('-o, --output ', 'default output directory for code generation')) + .addOption(new Option('--silent', 'suppress all output except errors').default(false)) .action(generateAction); const migrateCommand = program.command('migrate').description('Run database schema migration related tasks.'); diff --git a/packages/create-zenstack/src/index.ts b/packages/create-zenstack/src/index.ts index 092a784e..aa7f5602 100644 --- a/packages/create-zenstack/src/index.ts +++ b/packages/create-zenstack/src/index.ts @@ -1,6 +1,6 @@ import colors from 'colors'; import { Command } from 'commander'; -import { execSync } from 'node:child_process'; +import { execSync, type StdioOptions } from 'node:child_process'; import fs from 'node:fs'; import ora from 'ora'; import { STARTER_MAIN_TS, STARTER_ZMODEL } from './templates'; @@ -61,6 +61,9 @@ function initProject(name: string) { ), ); + // create VSCode config files + createVsCodeConfig(); + // install packages const packages = [ { name: '@zenstackhq/cli@next', dev: true }, @@ -103,24 +106,46 @@ function initProject(name: string) { // create main.ts fs.writeFileSync('main.ts', STARTER_MAIN_TS); - // run `zenstack generate` - runCommand(`${agentExec} zenstack generate`, 'Running `zenstack generate`'); + // run `zen generate` + runCommand(`${agentExec} zen generate`, 'Running `zen generate`'); + + // run `zen db push` + runCommand(`${agentExec} zen db push`, 'Running `zen db push`'); - // run `zenstack db push` - runCommand(`${agentExec} zenstack db push`, 'Running `zenstack db push`'); + // run `$agent run dev` + console.log(`Running \`${agent} run dev\``); + execSync(`${agent} run dev`, { stdio: 'inherit' }); + console.log(colors.green('Project setup completed!')); } function installPackage(pkg: { name: string; dev: boolean }) { runCommand(`${agent} install ${pkg.name} ${pkg.dev ? saveDev : ''}`, `Installing "${pkg.name}"`); } -function runCommand(cmd: string, status: string) { +function runCommand(cmd: string, status: string, stdio: StdioOptions = 'ignore') { const spinner = ora(status).start(); try { - execSync(cmd); + execSync(cmd, { stdio }); spinner.succeed(); } catch (e) { spinner.fail(); throw e; } } + +function createVsCodeConfig() { + fs.mkdirSync('.vscode'); + fs.writeFileSync( + '.vscode/settings.json', + JSON.stringify( + { + 'files.associations': { + '*.zmodel': 'zmodel-v3', + }, + }, + null, + 4, + ), + ); + fs.writeFileSync('.vscode/extensions.json', JSON.stringify({ recommendations: ['zenstack.zenstack-v3'] }, null, 4)); +} From 623c8a593ffd6e4d68595a92afd8ef48bfede86f Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sat, 30 Aug 2025 18:13:34 +0800 Subject: [PATCH 2/2] update --- packages/create-zenstack/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-zenstack/src/index.ts b/packages/create-zenstack/src/index.ts index aa7f5602..14af8a12 100644 --- a/packages/create-zenstack/src/index.ts +++ b/packages/create-zenstack/src/index.ts @@ -134,7 +134,7 @@ function runCommand(cmd: string, status: string, stdio: StdioOptions = 'ignore') } function createVsCodeConfig() { - fs.mkdirSync('.vscode'); + fs.mkdirSync('.vscode', { recursive: true }); fs.writeFileSync( '.vscode/settings.json', JSON.stringify(