Skip to content
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
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