Skip to content

Commit bb149bd

Browse files
authored
feat: allow specifying zmodel location in package.json (#879)
1 parent 4577232 commit bb149bd

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

packages/schema/src/cli/actions/format.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { getVersion } from '@zenstackhq/runtime';
2-
import { formatDocument } from '../cli-util';
32
import colors from 'colors';
4-
import ora from 'ora';
3+
import fs from 'fs';
54
import { writeFile } from 'fs/promises';
5+
import ora from 'ora';
6+
import { CliError } from '../cli-error';
7+
import { formatDocument, getDefaultSchemaLocation } from '../cli-util';
68

7-
export async function format(projectPath: string, options: { schema: string }) {
9+
export async function format(_projectPath: string, options: { schema: string }) {
810
const version = getVersion();
911
console.log(colors.bold(`⌛️ ZenStack CLI v${version}`));
1012

11-
const schemaFile = options.schema;
13+
const schemaFile = options.schema ?? getDefaultSchemaLocation();
14+
if (!fs.existsSync(schemaFile)) {
15+
console.error(colors.red(`File ${schemaFile} does not exist.`));
16+
throw new CliError('schema file does not exist');
17+
}
18+
1219
const spinner = ora(`Formatting ${schemaFile}`).start();
1320
try {
1421
const formattedDoc = await formatDocument(schemaFile);

packages/schema/src/cli/actions/generate.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { CliError } from '../cli-error';
55
import {
66
checkNewVersion,
77
checkRequiredPackage,
8+
getDefaultSchemaLocation,
89
getZenStackPackages,
910
loadDocument,
1011
requiredPrismaVersion,
1112
} from '../cli-util';
1213
import { PluginRunner, PluginRunnerOptions } from '../plugin-runner';
1314

1415
type Options = {
15-
schema: string;
16+
schema?: string;
1617
output?: string;
1718
dependencyCheck: boolean;
1819
versionCheck: boolean;
@@ -52,11 +53,13 @@ export async function generate(projectPath: string, options: Options) {
5253
}
5354

5455
async function runPlugins(options: Options) {
55-
const model = await loadDocument(options.schema);
56+
const schema = options.schema ?? getDefaultSchemaLocation();
57+
58+
const model = await loadDocument(schema);
5659

5760
const runnerOpts: PluginRunnerOptions = {
5861
schema: model,
59-
schemaPath: path.resolve(options.schema),
62+
schemaPath: path.resolve(schema),
6063
defaultPlugins: options.defaultPlugins,
6164
output: options.output,
6265
compile: options.compile,

packages/schema/src/cli/cli-util.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,22 @@ export async function formatDocument(fileName: string) {
276276
const edits = await formatter.formatDocument(document, { options, textDocument: identifier });
277277
return TextDocument.applyEdits(document.textDocument, edits);
278278
}
279+
280+
export function getDefaultSchemaLocation() {
281+
let location = path.resolve('schema.zmodel');
282+
283+
if (fs.existsSync('./package.json')) {
284+
try {
285+
// eslint-disable-next-line @typescript-eslint/no-var-requires
286+
const pkgJson = require(path.resolve('./package.json'));
287+
if (typeof pkgJson.zenstack?.schema === 'string') {
288+
location = path.resolve(pkgJson.zenstack.schema);
289+
}
290+
} catch (e) {
291+
console.error(e);
292+
// noop
293+
}
294+
}
295+
296+
return location;
297+
}

packages/schema/src/cli/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ export function createProgram() {
7676
.showHelpAfterError()
7777
.showSuggestionAfterError();
7878

79-
const schemaOption = new Option('--schema <file>', `schema file (with extension ${schemaExtensions})`).default(
80-
'./schema.zmodel'
79+
const schemaOption = new Option(
80+
'--schema <file>',
81+
`schema file (with extension ${schemaExtensions}). Defaults to "schema.zmodel" unless specified in package.json.`
8182
);
8283

8384
const configOption = new Option('-c, --config [file]', 'config file');

0 commit comments

Comments
 (0)