Skip to content

Commit

Permalink
feat(cli): new "check" command for validating ZModel (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Aug 21, 2024
1 parent b863060 commit ca2a4f4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/schema/src/cli/actions/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getDefaultSchemaLocation, loadDocument } from '../cli-util';

type Options = {
schema: string;
};

/**
* CLI action for checking schema
*/
export async function check(_projectPath: string, options: Options) {
const schema = options.schema ?? getDefaultSchemaLocation();
await loadDocument(schema);
console.log('The schema is valid.');
}
3 changes: 2 additions & 1 deletion packages/schema/src/cli/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './check';
export * from './format';
export * from './generate';
export * from './info';
export * from './init';
export * from './repl';
export * from './format';
6 changes: 5 additions & 1 deletion packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CHECK_VERSION_TIMEOUT = 1000;
* @param services Language services
* @returns Parsed and validated AST
*/
export async function loadDocument(fileName: string): Promise<Model> {
export async function loadDocument(fileName: string, validateOnly = false): Promise<Model> {
const services = createZModelServices(NodeFileSystem).ZModel;
const extensions = services.LanguageMetaData.fileExtensions;
if (!extensions.includes(path.extname(fileName))) {
Expand Down Expand Up @@ -93,6 +93,10 @@ export async function loadDocument(fileName: string): Promise<Model> {

const model = document.parseResult.value as Model;

if (validateOnly) {
return model;
}

// merge all declarations into the main document
const imported = mergeImportsDeclarations(langiumDocuments, model);

Expand Down
16 changes: 16 additions & 0 deletions packages/schema/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export const formatAction = async (options: Parameters<typeof actions.format>[1]
);
};

export const checkAction = async (options: Parameters<typeof actions.check>[1]): Promise<void> => {
await telemetry.trackSpan(
'cli:command:start',
'cli:command:complete',
'cli:command:error',
{ command: 'check' },
() => actions.check(process.cwd(), options)
);
};

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

Expand Down Expand Up @@ -131,6 +141,12 @@ export function createProgram() {
.option('--no-prisma-style', 'do not use prisma style')
.action(formatAction);

program
.command('check')
.description('Check a ZenStack schema file for syntax or semantic errors.')
.addOption(schemaOption)
.action(checkAction);

// make sure config is loaded before actions run
program.hook('preAction', async (_, actionCommand) => {
let configFile: string | undefined = actionCommand.opts().config;
Expand Down

0 comments on commit ca2a4f4

Please sign in to comment.