UNPKG

2.12 kBPlain TextView Raw
1
2import { loadTemplatizedYaml } from './renderer';
3import * as Path from 'path';
4import deepmerge = require('deepmerge');
5import * as fs from 'fs-extra-plus';
6
7
8// --------- Public Types --------- //
9export interface RawVdevConfig {
10 system: string;
11 baseBlockDir: string;
12 k8sDir: string;
13 realms: { [name: string]: any };
14 blocks: (string | any)[];
15
16 versionFiles: string[];
17}
18// --------- /Public Types --------- //
19
20
21// --------- Public Loaders --------- //
22export async function loadVersionFiles() {
23 const rawConfig = await loadVdevConfig();
24 return rawConfig.versionFiles || [];
25}
26
27const overwriteMerge = (target: any[], source: any[], options?: deepmerge.Options) => source
28
29/** Parse the ./vdev.yaml and return as is */
30export async function loadVdevConfig(rootDir?: string): Promise<RawVdevConfig> {
31 rootDir = rootDir || "./";
32 const vdevFile = Path.join(rootDir, 'vdev.yaml');
33 // NOTE: Here we use the loadTempletezedYaml, but for now, the vdev files are not templates, just the k8s yamls are.
34 let vdevObj = await loadVdevFile(vdevFile, true);
35
36 // TODO: probably need to do some validate here.
37
38 // load the sub vdev files if defined
39 if (vdevObj.vdevFiles) {
40 for (const subRelVdevFile of vdevObj.vdevFiles) {
41 const subVdevFile = Path.join(rootDir, subRelVdevFile);
42 const exists = await fs.pathExists(subVdevFile);
43 if (exists) {
44 console.log(`INFO - Loading extra vdev file: ${subVdevFile}`);
45 try {
46 let subVdevObj = await loadVdevFile(subVdevFile);
47 vdevObj = deepmerge(vdevObj, subVdevObj, { arrayMerge: overwriteMerge });
48 } catch (ex) {
49 console.log(`ERROR - Cannot load vdev files ${subVdevFile} (skipping file)\n\tCause: ${ex}`);
50 }
51 }
52
53 }
54 }
55
56
57 return vdevObj as RawVdevConfig;
58}
59// --------- /Public Loaders --------- //
60
61/**
62 * Load a single vdev yaml file. Can be the base or a sub vdev file
63 * @param base If the vdev file is the base (should have more validation)
64 */
65async function loadVdevFile(vdevFile: string, base = false): Promise<any> {
66 const vdevRawObj = await loadTemplatizedYaml(vdevFile);
67 // TODO: need to do validation
68 return vdevRawObj;
69}
\No newline at end of file