1 |
|
2 | import * as fs from 'fs-extra';
|
3 | import * as Path from 'path';
|
4 | import { loadTemplatizedYaml } from './renderer';
|
5 | import deepmerge = require('deepmerge');
|
6 |
|
7 |
|
8 |
|
9 | export interface RawVdevConfig {
|
10 | system: string;
|
11 | baseBlockDir: string;
|
12 | realms: { [name: string]: any };
|
13 | blocks: (string | any)[];
|
14 |
|
15 |
|
16 | imageTag: string;
|
17 | __version__: string;
|
18 |
|
19 | version?: {
|
20 | files?: string[]
|
21 | }
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const overwriteMerge = (target: any[], source: any[], options?: deepmerge.Options) => source
|
28 |
|
29 |
|
30 | export async function loadVdevConfig(rootDir?: string): Promise<RawVdevConfig> {
|
31 | rootDir = rootDir || "./";
|
32 | const vdevFile = Path.join(rootDir, 'vdev.yaml');
|
33 |
|
34 |
|
35 | const packageJson = await fs.readJSON('./package.json');
|
36 | let __version__ = packageJson.__version__ ?? packageJson.version;
|
37 |
|
38 |
|
39 |
|
40 | const data: any = { __version__ };
|
41 |
|
42 |
|
43 | let vdevObj = await loadVdevFile(vdevFile, true, data);
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | if (vdevObj.vdevFiles) {
|
49 | for (const subRelVdevFile of vdevObj.vdevFiles) {
|
50 | const subVdevFile = Path.join(rootDir, subRelVdevFile);
|
51 | const exists = await fs.pathExists(subVdevFile);
|
52 | if (exists) {
|
53 | console.log(`INFO - Loading extra vdev file: ${subVdevFile}`);
|
54 | try {
|
55 | let subVdevObj = await loadVdevFile(subVdevFile, false, data);
|
56 | vdevObj = deepmerge(vdevObj, subVdevObj, { arrayMerge: overwriteMerge });
|
57 | } catch (ex) {
|
58 | console.log(`ERROR - Cannot load vdev files ${subVdevFile} (skipping file)\n\tCause: ${ex}`);
|
59 | }
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 | vdevObj.__version__ = __version__;
|
66 | vdevObj.imageTag = vdevObj.imageTag ?? 'latest';
|
67 |
|
68 |
|
69 | return vdevObj as RawVdevConfig;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | async function loadVdevFile(vdevFile: string, base: boolean, data?: any): Promise<any> {
|
78 | const vdevRawObj = await loadTemplatizedYaml(vdevFile, data);
|
79 |
|
80 | return vdevRawObj;
|
81 | } |
\ | No newline at end of file |