@gearbox-protocol/cli-utils 5.33.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/resolveYamlFiles.d.ts +23 -0
- package/dist/resolveYamlFiles.js +67 -0
- package/package.json +45 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./resolveYamlFiles.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./resolveYamlFiles.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import YAML from "yaml";
|
|
2
|
+
/**
|
|
3
|
+
* Merges yaml files, respecting .extends references.
|
|
4
|
+
* Performs env variable substitution on the config.
|
|
5
|
+
* Respects YAML anchors and aliases, and merge keys - so that they can be shared across two files
|
|
6
|
+
*
|
|
7
|
+
* x-* anchors are removed from resulting document (a-la docker compose yaml)
|
|
8
|
+
*
|
|
9
|
+
* @param file
|
|
10
|
+
* @param templateData
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveYamlFiles(file: string, templateData?: Record<string, string>): Promise<object>;
|
|
14
|
+
/**
|
|
15
|
+
* Merges yaml documents, respecting .extends references.
|
|
16
|
+
* Optionally, performs env-style variable substitution on the config.
|
|
17
|
+
* Respects YAML anchors and aliases, and merge keys - so that they can be shared across two files
|
|
18
|
+
*
|
|
19
|
+
* @param file
|
|
20
|
+
* @param templateData
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveYamlDoc(file: string, templateData?: Record<string, string>): Promise<YAML.Document>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve as pathResolve } from "node:path";
|
|
3
|
+
import { template } from "lodash-es";
|
|
4
|
+
import YAML, { isPair, isScalar, isSeq } from "yaml";
|
|
5
|
+
/**
|
|
6
|
+
* Merges yaml files, respecting .extends references.
|
|
7
|
+
* Performs env variable substitution on the config.
|
|
8
|
+
* Respects YAML anchors and aliases, and merge keys - so that they can be shared across two files
|
|
9
|
+
*
|
|
10
|
+
* x-* anchors are removed from resulting document (a-la docker compose yaml)
|
|
11
|
+
*
|
|
12
|
+
* @param file
|
|
13
|
+
* @param templateData
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export async function resolveYamlFiles(file, templateData) {
|
|
17
|
+
const doc = await resolveYamlDoc(file, templateData);
|
|
18
|
+
const obj = doc.toJSON();
|
|
19
|
+
return Object.fromEntries(Object.entries(obj).filter(([k]) => !k.startsWith("x-")));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Merges yaml documents, respecting .extends references.
|
|
23
|
+
* Optionally, performs env-style variable substitution on the config.
|
|
24
|
+
* Respects YAML anchors and aliases, and merge keys - so that they can be shared across two files
|
|
25
|
+
*
|
|
26
|
+
* @param file
|
|
27
|
+
* @param templateData
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
export async function resolveYamlDoc(file, templateData) {
|
|
31
|
+
let content = await readFile(file, "utf-8");
|
|
32
|
+
if (templateData) {
|
|
33
|
+
const compiled = template(content);
|
|
34
|
+
content = compiled(templateData);
|
|
35
|
+
}
|
|
36
|
+
let cfgRaw = YAML.parseDocument(content, { merge: true });
|
|
37
|
+
// let cfgRaw = YAML.parse(content, { merge: true });
|
|
38
|
+
if (cfgRaw.has("extends")) {
|
|
39
|
+
const baseFile = cfgRaw.get("extends");
|
|
40
|
+
const extended = cfgRaw;
|
|
41
|
+
// extends is a relative path to the base file
|
|
42
|
+
cfgRaw = await resolveYamlDoc(pathResolve(dirname(file), baseFile), templateData);
|
|
43
|
+
YAML.visit(extended, {
|
|
44
|
+
Pair(_, pair, rawPath) {
|
|
45
|
+
const path = getNodesPath(rawPath);
|
|
46
|
+
if (isScalar(pair.key) && (isScalar(pair.value) || isSeq(pair.value))) {
|
|
47
|
+
if (pair.key.value === "extends") {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
cfgRaw.setIn([...path, pair.key.value], pair.value);
|
|
51
|
+
return YAML.visit.SKIP;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return cfgRaw;
|
|
57
|
+
}
|
|
58
|
+
function getNodesPath(nodes) {
|
|
59
|
+
return nodes
|
|
60
|
+
.map(n => {
|
|
61
|
+
if (isPair(n) && isScalar(n.key)) {
|
|
62
|
+
return n.key.value;
|
|
63
|
+
}
|
|
64
|
+
return "";
|
|
65
|
+
})
|
|
66
|
+
.filter(Boolean);
|
|
67
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gearbox-protocol/cli-utils",
|
|
3
|
+
"description": "Utils for creating cli apps",
|
|
4
|
+
"version": "5.33.0-next.2",
|
|
5
|
+
"homepage": "https://gearbox.fi",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"gearbox"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/Gearbox-protocol/deploy-tools"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"license": "BUSL-1.1",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "rm -rf ./dist",
|
|
25
|
+
"build": "tsc --p tsconfig.build.json",
|
|
26
|
+
"dev": "tsc --p tsconfig.build.json --watch",
|
|
27
|
+
"prettier": "prettier --write .",
|
|
28
|
+
"prettier:ci": "npx prettier --check .",
|
|
29
|
+
"lint": "eslint \"**/*.ts\" --fix",
|
|
30
|
+
"lint:ci": "eslint \"**/*.ts\"",
|
|
31
|
+
"typecheck:ci": "tsc --noEmit",
|
|
32
|
+
"test": "vitest",
|
|
33
|
+
"package:version": "yarn version"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"lodash-es": "^4.17.21",
|
|
37
|
+
"yaml": "^2.8.0",
|
|
38
|
+
"zod": "^3.25.42"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/lodash-es": "^4.17.12",
|
|
42
|
+
"@types/node": "^22.15.21"
|
|
43
|
+
},
|
|
44
|
+
"prettier": "@gearbox-protocol/prettier-config"
|
|
45
|
+
}
|