File tree Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Original file line number Diff line number Diff line change 1+ import { getDefaultSchemaLocation , loadDocument } from '../cli-util' ;
2+
3+ type Options = {
4+ schema : string ;
5+ } ;
6+
7+ /**
8+ * CLI action for checking schema
9+ */
10+ export async function check ( _projectPath : string , options : Options ) {
11+ const schema = options . schema ?? getDefaultSchemaLocation ( ) ;
12+ await loadDocument ( schema ) ;
13+ console . log ( 'The schema is valid.' ) ;
14+ }
Original file line number Diff line number Diff line change 1+ export * from './check' ;
2+ export * from './format' ;
13export * from './generate' ;
24export * from './info' ;
35export * from './init' ;
46export * from './repl' ;
5- export * from './format' ;
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ const CHECK_VERSION_TIMEOUT = 1000;
2727 * @param services Language services
2828 * @returns Parsed and validated AST
2929 */
30- export async function loadDocument ( fileName : string ) : Promise < Model > {
30+ export async function loadDocument ( fileName : string , validateOnly = false ) : Promise < Model > {
3131 const services = createZModelServices ( NodeFileSystem ) . ZModel ;
3232 const extensions = services . LanguageMetaData . fileExtensions ;
3333 if ( ! extensions . includes ( path . extname ( fileName ) ) ) {
@@ -93,6 +93,10 @@ export async function loadDocument(fileName: string): Promise<Model> {
9393
9494 const model = document . parseResult . value as Model ;
9595
96+ if ( validateOnly ) {
97+ return model ;
98+ }
99+
96100 // merge all declarations into the main document
97101 const imported = mergeImportsDeclarations ( langiumDocuments , model ) ;
98102
Original file line number Diff line number Diff line change @@ -60,6 +60,16 @@ export const formatAction = async (options: Parameters<typeof actions.format>[1]
6060 ) ;
6161} ;
6262
63+ export const checkAction = async ( options : Parameters < typeof actions . check > [ 1 ] ) : Promise < void > => {
64+ await telemetry . trackSpan (
65+ 'cli:command:start' ,
66+ 'cli:command:complete' ,
67+ 'cli:command:error' ,
68+ { command : 'check' } ,
69+ ( ) => actions . check ( process . cwd ( ) , options )
70+ ) ;
71+ } ;
72+
6373export function createProgram ( ) {
6474 const program = new Command ( 'zenstack' ) ;
6575
@@ -131,6 +141,12 @@ export function createProgram() {
131141 . option ( '--no-prisma-style' , 'do not use prisma style' )
132142 . action ( formatAction ) ;
133143
144+ program
145+ . command ( 'check' )
146+ . description ( 'Check a ZenStack schema file for syntax or semantic errors.' )
147+ . addOption ( schemaOption )
148+ . action ( checkAction ) ;
149+
134150 // make sure config is loaded before actions run
135151 program . hook ( 'preAction' , async ( _ , actionCommand ) => {
136152 let configFile : string | undefined = actionCommand . opts ( ) . config ;
You can’t perform that action at this time.
0 commit comments