-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfileSchemaReader.ts
65 lines (59 loc) · 1.81 KB
/
fileSchemaReader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
SchemaFormat,
SchemaData,
SchemaReader,
} from '@vulcan-sql/build/models';
import * as glob from 'glob';
import { promises as fs } from 'fs';
import * as path from 'path';
import { SchemaParserOptions } from '@vulcan-sql/build/options';
import {
VulcanExtensionId,
VulcanInternalExtension,
TYPES as CORE_TYPES,
} from '@vulcan-sql/core';
import { inject } from 'inversify';
import { TYPES } from '@vulcan-sql/build/containers';
export interface FileSchemaReaderOptions {
folderPath: string;
}
@VulcanInternalExtension()
@VulcanExtensionId('LocalFile')
export class FileSchemaReader extends SchemaReader {
private options: SchemaParserOptions;
constructor(
@inject(TYPES.SchemaParserOptions) options: SchemaParserOptions,
@inject(CORE_TYPES.ExtensionConfig) config: any,
@inject(CORE_TYPES.ExtensionName) moduleName: string
) {
super(config, moduleName);
this.options = options;
}
public async *readSchema(): AsyncGenerator<SchemaData> {
if (!this.options?.folderPath)
throw new Error(`Config schema-parser.folderPath must be defined`);
const files = await this.getSchemaFilePaths();
for (const file of files) {
const fileName = path.relative(this.options!.folderPath, file);
const { ext } = path.parse(fileName);
const sourceName = fileName.replace(new RegExp(`\\${ext}$`), '');
yield {
sourceName,
content: await fs.readFile(file, 'utf8'),
type: SchemaFormat.YAML,
};
}
}
private async getSchemaFilePaths(): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(
path.resolve(this.options!.folderPath, '**', '*.yaml'),
{ nodir: true },
(err, files) => {
if (err) return reject(err);
else return resolve(files);
}
);
});
}
}