Skip to content

Commit

Permalink
feat: allow specifying zmodel location in package.json (zenstackhq#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Dec 3, 2023
1 parent 4577232 commit bb149bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
15 changes: 11 additions & 4 deletions packages/schema/src/cli/actions/format.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 6 additions & 3 deletions packages/schema/src/cli/actions/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { CliError } from '../cli-error';
import {
checkNewVersion,
checkRequiredPackage,
getDefaultSchemaLocation,
getZenStackPackages,
loadDocument,
requiredPrismaVersion,
} from '../cli-util';
import { PluginRunner, PluginRunnerOptions } from '../plugin-runner';

type Options = {
schema: string;
schema?: string;
output?: string;
dependencyCheck: boolean;
versionCheck: boolean;
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions packages/schema/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export function createProgram() {
.showHelpAfterError()
.showSuggestionAfterError();

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

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

0 comments on commit bb149bd

Please sign in to comment.