From 3d646592cd4e9d02345d943a6ba2f6e138e26e29 Mon Sep 17 00:00:00 2001 From: Andy Brown Date: Fri, 6 Mar 2020 14:21:32 -0800 Subject: [PATCH] fix: load schema files when loading bot project (#2170) * fix: correctly fetch the bot project schema * do not throw error if schemas directory does not exist * fix type imports Co-authored-by: Chris Whitten --- .../server/src/models/bot/botProject.ts | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/Composer/packages/server/src/models/bot/botProject.ts b/Composer/packages/server/src/models/bot/botProject.ts index 036b7b6d64..bfba48e2c9 100644 --- a/Composer/packages/server/src/models/bot/botProject.ts +++ b/Composer/packages/server/src/models/bot/botProject.ts @@ -485,7 +485,7 @@ export class BotProject { } const fileList: FileInfo[] = []; - const patterns = ['**/*.dialog', '**/*.lg', '**/*.lu', '**/*.schema']; + const patterns = ['**/*.dialog', '**/*.lg', '**/*.lu']; for (const pattern of patterns) { // load only from the data dir, otherwise may get "build" versions from // deployment process @@ -494,22 +494,56 @@ export class BotProject { for (const filePath of paths.sort()) { const realFilePath: string = Path.join(root, filePath); - // skip lg files for now - if ((await this.fileStorage.stat(realFilePath)).isFile) { - const content: string = await this.fileStorage.readFile(realFilePath); - fileList.push({ - name: Path.basename(filePath), - content: content, - path: realFilePath, - relativePath: Path.relative(this.dir, realFilePath), - }); + const fileInfo = await this._getFileInfo(realFilePath); + if (fileInfo) { + fileList.push(fileInfo); } } } + const schemas = await this._getSchemas(); + fileList.push(...schemas); + return fileList; }; + private _getSchemas = async (): Promise => { + if (!(await this.exists())) { + throw new Error(`${this.dir} is not a valid path`); + } + + const schemasDir = Path.join(this.dir, 'Schemas'); + + if (!(await this.fileStorage.exists(schemasDir))) { + debug('No schemas directory found.'); + return []; + } + + const schemas: FileInfo[] = []; + const paths = await this.fileStorage.glob('*.schema', schemasDir); + + for (const path of paths) { + const fileInfo = await this._getFileInfo(Path.join(schemasDir, path)); + if (fileInfo) { + schemas.push(fileInfo); + } + } + + return schemas; + }; + + private _getFileInfo = async (path: string): Promise => { + if ((await this.fileStorage.stat(path)).isFile) { + const content: string = await this.fileStorage.readFile(path); + return { + name: Path.basename(path), + content: content, + path: path, + relativePath: Path.relative(this.dir, path), + }; + } + }; + // check project stracture is valid or not, if not, try fix it. private _checkProjectStructure = async () => { const dialogs: DialogInfo[] = this.dialogs;