UNPKG

2.58 kBPlain TextView Raw
1
2import * as fs from 'fs-extra-plus';
3import * as Path from 'path';
4import { loadTemplatizedYaml } from './renderer';
5import deepmerge = require('deepmerge');
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
17 imageTag: string; // set if not found
18 __version__: string; // popuplate if not found
19
20 version?: {
21 files?: string[]
22 }
23}
24// --------- /Public Types --------- //
25
26
27// --------- Public Loaders --------- //
28const overwriteMerge = (target: any[], source: any[], options?: deepmerge.Options) => source
29
30/** Parse the ./vdev.yaml and return as is */
31export async function loadVdevConfig(rootDir?: string): Promise<RawVdevConfig> {
32 rootDir = rootDir || "./";
33 const vdevFile = Path.join(rootDir, 'vdev.yaml');
34
35 //// Get the __version__
36 const packageJson = await fs.readJSON('./package.json');
37 let __version__ = packageJson.__version__ ?? packageJson.version;
38
39 //// build the data for the vdev template
40 // TODO: probably needs to put the other variable from the handlebars helpers here
41 const data: any = { __version__ };
42
43 // NOTE: Here we use the loadTempletezedYaml, but for now, the vdev files are not templates, just the k8s yamls are.
44 let vdevObj = await loadVdevFile(vdevFile, true, data);
45
46 // TODO: probably need to do some validate here.
47
48 // load the sub vdev files if defined
49 if (vdevObj.vdevFiles) {
50 for (const subRelVdevFile of vdevObj.vdevFiles) {
51 const subVdevFile = Path.join(rootDir, subRelVdevFile);
52 const exists = await fs.pathExists(subVdevFile);
53 if (exists) {
54 console.log(`INFO - Loading extra vdev file: ${subVdevFile}`);
55 try {
56 let subVdevObj = await loadVdevFile(subVdevFile, false, data);
57 vdevObj = deepmerge(vdevObj, subVdevObj, { arrayMerge: overwriteMerge });
58 } catch (ex) {
59 console.log(`ERROR - Cannot load vdev files ${subVdevFile} (skipping file)\n\tCause: ${ex}`);
60 }
61 }
62 }
63 }
64
65 //// Allow the __version__ to be updated
66 vdevObj.__version__ = __version__;
67 vdevObj.imageTag = vdevObj.imageTag ?? 'latest';
68
69
70 return vdevObj as RawVdevConfig;
71}
72// --------- /Public Loaders --------- //
73
74/**
75 * Load a single vdev yaml file. Can be the base or a sub vdev file
76 * @param base If the vdev file is the base (should have more validation)
77 */
78async function loadVdevFile(vdevFile: string, base: boolean, data?: any): Promise<any> {
79 const vdevRawObj = await loadTemplatizedYaml(vdevFile, data);
80 // TODO: need to do validation
81 return vdevRawObj;
82}
\No newline at end of file