Skip to content

Commit

Permalink
Allow multiple schema directories to be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
willosborne committed Feb 11, 2025
1 parent ea15b32 commit 559fd04
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
6 changes: 5 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ async function parseDocumentLoaderConfig(options): Promise<DocumentLoaderOptions
}
}

throw Error('Either a filesystem path or CALMHub URL must be supplied for loading required documents')
console.log('Warning, no schema loading mechanism was defined. Only the bundled core schemas will be available; you may see empty definitions or errors.')
return {
loadMode: 'filesystem',
schemaDirectoryPath: undefined
}
}

program.parse(process.argv);
7 changes: 5 additions & 2 deletions shared/src/commands/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ function loadFile(path: string): object {
function getDocumentLoader(docLoaderOpts: DocumentLoaderOptions, debug: boolean): DocumentLoader {
switch(docLoaderOpts.loadMode) {
case 'filesystem':

Check failure on line 41 in shared/src/commands/generate/generate.ts

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint Shared Module

Expected indentation of 4 spaces but found 8
// TODO allow it to load multiple directories so we can load core schemas and an additional schemaDir
return new FileSystemDocumentLoader(CALM_META_SCHEMA_DIRECTORY, debug);
const directoryPaths = [CALM_META_SCHEMA_DIRECTORY];

Check failure on line 42 in shared/src/commands/generate/generate.ts

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint Shared Module

Expected indentation of 8 spaces but found 12

Check failure on line 42 in shared/src/commands/generate/generate.ts

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint Shared Module

Unexpected lexical declaration in case block
if (docLoaderOpts.schemaDirectoryPath) {

Check failure on line 43 in shared/src/commands/generate/generate.ts

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint Shared Module

Expected indentation of 8 spaces but found 12
directoryPaths.push(docLoaderOpts.schemaDirectoryPath);

Check failure on line 44 in shared/src/commands/generate/generate.ts

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint Shared Module

Expected indentation of 12 spaces but found 16
}

Check failure on line 45 in shared/src/commands/generate/generate.ts

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint Shared Module

Expected indentation of 8 spaces but found 12
return new FileSystemDocumentLoader(directoryPaths, debug);
case 'calmhub':
return new CALMHubDocumentLoader(docLoaderOpts.calmHubUrl, debug);
}
Expand Down
2 changes: 1 addition & 1 deletion shared/src/commands/validate/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function loadMetaSchemas(metaSchemaLocation: string): Promise<SchemaDirect


// TODO pass debug
const documentLoader = new FileSystemDocumentLoader(metaSchemaLocation, true);
const documentLoader = new FileSystemDocumentLoader([metaSchemaLocation], true);
const schemaDirectory = new SchemaDirectory(documentLoader);
await schemaDirectory.loadSchemas(metaSchemaLocation);

Expand Down
20 changes: 13 additions & 7 deletions shared/src/document-loader/file-system-document-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@ import { SchemaDirectory } from "../schema-directory";

export class FileSystemDocumentLoader implements DocumentLoader {
private readonly logger: Logger;
private readonly directoryPath: string;
private readonly directoryPaths: string[];
// private schemaDirectory: SchemaDirectory;

constructor(directoryPath: string, debug: boolean) {
constructor(directoryPaths: string[], debug: boolean) {
this.logger = initLogger(debug);
this.directoryPath = directoryPath;
this.directoryPaths = directoryPaths;
// this.schemaDirectory = schemaDirectory;
}

async initialise(schemaDirectory: SchemaDirectory): Promise<void> {
for (const directoryPath of this.directoryPaths) {
await this.loadDocumentsFromDirectory(schemaDirectory, directoryPath);
}
}

async loadDocumentsFromDirectory(schemaDirectory: SchemaDirectory, directoryPath: string): Promise<void> {
try {
// const map = new Map<string, object>();

this.logger.debug('Loading schemas from ' + this.directoryPath);
const files = await readdir(this.directoryPath, { recursive: true });
this.logger.debug('Loading schemas from ' + directoryPath);
const files = await readdir(directoryPath, { recursive: true });

const schemaPaths = files.filter(str => str.match(/^.*(json|yaml|yml)$/))
.map(schemaPath => join(this.directoryPath, schemaPath));
.map(schemaPath => join(directoryPath, schemaPath));

for (const schemaPath of schemaPaths) {
const schemaDef = await this.loadDocument(schemaPath);
Expand All @@ -35,7 +41,7 @@ export class FileSystemDocumentLoader implements DocumentLoader {
}
} catch (err) {
if (err.code === 'ENOENT') {
this.logger.error('Specified directory not found while loading documents: ' + this.directoryPath + ', error: ' + err.message);
this.logger.error('Specified directory not found while loading documents: ' + directoryPath + ', error: ' + err.message);
} else {
this.logger.error(err);
}
Expand Down

0 comments on commit 559fd04

Please sign in to comment.