Skip to content

feat: allow specifying zmodel location in package.json #879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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