Skip to content

Commit

Permalink
fix: load schema files when loading bot project (#2170)
Browse files Browse the repository at this point in the history
* 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 <christopher.whitten@microsoft.com>
  • Loading branch information
a-b-r-o-w-n and cwhitten authored Mar 6, 2020
1 parent a05a5fc commit 3d64659
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<FileInfo[]> => {
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<FileInfo | undefined> => {
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;
Expand Down

0 comments on commit 3d64659

Please sign in to comment.