diff --git a/packages/schema/src/cli/actions/format.ts b/packages/schema/src/cli/actions/format.ts index 0d717343c..67f2d8a14 100644 --- a/packages/schema/src/cli/actions/format.ts +++ b/packages/schema/src/cli/actions/format.ts @@ -1,14 +1,21 @@ import { getVersion } from '@zenstackhq/runtime'; -import { formatDocument } from '../cli-util'; import colors from 'colors'; -import ora from 'ora'; +import fs from 'fs'; import { writeFile } from 'fs/promises'; +import ora from 'ora'; +import { CliError } from '../cli-error'; +import { formatDocument, getDefaultSchemaLocation } from '../cli-util'; -export async function format(projectPath: string, options: { schema: string }) { +export async function format(_projectPath: string, options: { schema: string }) { const version = getVersion(); console.log(colors.bold(`⌛️ ZenStack CLI v${version}`)); - const schemaFile = options.schema; + const schemaFile = options.schema ?? getDefaultSchemaLocation(); + if (!fs.existsSync(schemaFile)) { + console.error(colors.red(`File ${schemaFile} does not exist.`)); + throw new CliError('schema file does not exist'); + } + const spinner = ora(`Formatting ${schemaFile}`).start(); try { const formattedDoc = await formatDocument(schemaFile); diff --git a/packages/schema/src/cli/actions/generate.ts b/packages/schema/src/cli/actions/generate.ts index ead57803f..bb6b798dd 100644 --- a/packages/schema/src/cli/actions/generate.ts +++ b/packages/schema/src/cli/actions/generate.ts @@ -5,6 +5,7 @@ import { CliError } from '../cli-error'; import { checkNewVersion, checkRequiredPackage, + getDefaultSchemaLocation, getZenStackPackages, loadDocument, requiredPrismaVersion, @@ -12,7 +13,7 @@ import { import { PluginRunner, PluginRunnerOptions } from '../plugin-runner'; type Options = { - schema: string; + schema?: string; output?: string; dependencyCheck: boolean; versionCheck: boolean; @@ -52,11 +53,13 @@ export async function generate(projectPath: string, options: Options) { } async function runPlugins(options: Options) { - const model = await loadDocument(options.schema); + const schema = options.schema ?? getDefaultSchemaLocation(); + + const model = await loadDocument(schema); const runnerOpts: PluginRunnerOptions = { schema: model, - schemaPath: path.resolve(options.schema), + schemaPath: path.resolve(schema), defaultPlugins: options.defaultPlugins, output: options.output, compile: options.compile, diff --git a/packages/schema/src/cli/cli-util.ts b/packages/schema/src/cli/cli-util.ts index 04a64af24..bb9aa525b 100644 --- a/packages/schema/src/cli/cli-util.ts +++ b/packages/schema/src/cli/cli-util.ts @@ -276,3 +276,22 @@ export async function formatDocument(fileName: string) { const edits = await formatter.formatDocument(document, { options, textDocument: identifier }); return TextDocument.applyEdits(document.textDocument, edits); } + +export function getDefaultSchemaLocation() { + let location = path.resolve('schema.zmodel'); + + if (fs.existsSync('./package.json')) { + try { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkgJson = require(path.resolve('./package.json')); + if (typeof pkgJson.zenstack?.schema === 'string') { + location = path.resolve(pkgJson.zenstack.schema); + } + } catch (e) { + console.error(e); + // noop + } + } + + return location; +} diff --git a/packages/schema/src/cli/index.ts b/packages/schema/src/cli/index.ts index 84861bdd4..7012d05ed 100644 --- a/packages/schema/src/cli/index.ts +++ b/packages/schema/src/cli/index.ts @@ -76,8 +76,9 @@ export function createProgram() { .showHelpAfterError() .showSuggestionAfterError(); - const schemaOption = new Option('--schema ', `schema file (with extension ${schemaExtensions})`).default( - './schema.zmodel' + const schemaOption = new Option( + '--schema ', + `schema file (with extension ${schemaExtensions}). Defaults to "schema.zmodel" unless specified in package.json.` ); const configOption = new Option('-c, --config [file]', 'config file');