UNPKG

2.55 kBPlain TextView Raw
1
2import * as fs from 'fs-extra';
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 realms: { [name: string]: any };
13 blocks: (string | any)[];
14
15
16 imageTag: string; // set if not found
17 __version__: string; // popuplate if not found
18
19 version?: {
20 files?: string[]
21 }
22}
23// --------- /Public Types --------- //
24
25
26// --------- Public Loaders --------- //
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
34 //// Get the __version__
35 const packageJson = await fs.readJSON('./package.json');
36 let __version__ = packageJson.__version__ ?? packageJson.version;
37
38 //// build the data for the vdev template
39 // TODO: probably needs to put the other variable from the handlebars helpers here
40 const data: any = { __version__ };
41
42 // NOTE: Here we use the loadTempletezedYaml, but for now, the vdev files are not templates, just the k8s yamls are.
43 let vdevObj = await loadVdevFile(vdevFile, true, data);
44
45 // TODO: probably need to do some validate here.
46
47 // load the sub vdev files if defined
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 //// Allow the __version__ to be updated
65 vdevObj.__version__ = __version__;
66 vdevObj.imageTag = vdevObj.imageTag ?? 'latest';
67
68
69 return vdevObj as RawVdevConfig;
70}
71// --------- /Public Loaders --------- //
72
73/**
74 * Load a single vdev yaml file. Can be the base or a sub vdev file
75 * @param base If the vdev file is the base (should have more validation)
76 */
77async function loadVdevFile(vdevFile: string, base: boolean, data?: any): Promise<any> {
78 const vdevRawObj = await loadTemplatizedYaml(vdevFile, data);
79 // TODO: need to do validation
80 return vdevRawObj;
81}
\No newline at end of file