Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 15 additions & 8 deletions packages/cli/src/actions/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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';

type Options = {
schema?: string;
output?: string;
silent: boolean;
};

/**
Expand All @@ -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';
Expand All @@ -38,6 +40,7 @@ const client = new ZenStackClient(schema, {
dialect: { ... }
});
\`\`\``);
}
}

function getOutputPath(options: Options, schemaFile: string) {
Expand All @@ -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<string, unknown> }[] = [];

Expand Down Expand Up @@ -95,17 +98,21 @@ 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,
model,
defaultOutputPath: outputPath,
pluginOptions,
});
spinner.succeed();
spinner?.succeed();
} catch (err) {
spinner.fail();
spinner?.fail();
console.error(err);
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const checkAction = async (options: Parameters<typeof actions.check>[0]): Promis
};

export function createProgram() {
const program = new Command('zenstack');
const program = new Command('zen');

program.version(getVersion()!, '-v --version', 'display CLI version');

Expand All @@ -55,6 +55,7 @@ export function createProgram() {
.description('Run code generation plugins.')
.addOption(schemaOption)
.addOption(new Option('-o, --output <path>', '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.');
Expand Down
39 changes: 32 additions & 7 deletions packages/create-zenstack/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -61,6 +61,9 @@ function initProject(name: string) {
),
);

// create VSCode config files
createVsCodeConfig();

// install packages
const packages = [
{ name: '@zenstackhq/cli@next', dev: true },
Expand Down Expand Up @@ -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));
}