@gearbox-protocol/cli-utils 5.69.0 → 5.69.1

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/Zommand.js CHANGED
@@ -73,10 +73,10 @@ export class Zommand extends Command {
73
73
  }
74
74
  let fromInlineConfig = {};
75
75
  if (inlineConfig) {
76
- fromInlineConfig = decompressObject(inlineConfig);
76
+ fromInlineConfig = await decompressObject(inlineConfig, this.#templatedData);
77
77
  }
78
78
  else if (process.env.INLINE_CONFIG) {
79
- fromInlineConfig = decompressObject(process.env.INLINE_CONFIG);
79
+ fromInlineConfig = await decompressObject(process.env.INLINE_CONFIG, this.#templatedData);
80
80
  }
81
81
  const schemaFromEnv = {};
82
82
  for (const [env, key] of Object.entries(this.#envToObjectKeys)) {
@@ -5,5 +5,6 @@
5
5
  export declare function compressObject(obj: unknown): string;
6
6
  /**
7
7
  * Decompress a base64url string back into a JS object
8
+ * Allows to pass template data to resolve promises/env
8
9
  */
9
- export declare function decompressObject<T = unknown>(value: string): T;
10
+ export declare function decompressObject<T = unknown>(value: string, templateData?: Record<string, unknown>): Promise<T>;
@@ -1,4 +1,5 @@
1
1
  import { deflateSync, inflateSync } from "node:zlib";
2
+ import { resolveFromObject } from "./resolveYamlFiles.js";
2
3
  /**
3
4
  * Compress a plain JS object into a base64url string
4
5
  * suitable for env vars.
@@ -10,9 +11,12 @@ export function compressObject(obj) {
10
11
  }
11
12
  /**
12
13
  * Decompress a base64url string back into a JS object
14
+ * Allows to pass template data to resolve promises/env
13
15
  */
14
- export function decompressObject(value) {
16
+ export async function decompressObject(value, templateData) {
15
17
  const compressed = Buffer.from(value, "base64url");
16
- const json = inflateSync(compressed).toString("utf8");
17
- return JSON.parse(json);
18
+ const str = inflateSync(compressed).toString("utf8");
19
+ const json = JSON.parse(str);
20
+ const result = await resolveFromObject(json, templateData);
21
+ return result;
18
22
  }
@@ -23,6 +23,14 @@ export declare function resolveYamlFiles(file: string, templateData?: TemplateDa
23
23
  * @returns
24
24
  */
25
25
  export declare function resolveYamlDoc(file: string, templateData?: TemplateData): Promise<YAML.Document>;
26
+ /**
27
+ * Helper function that gets object with templated values
28
+ * Just because async ast already exists in yaml, but not in json reviver
29
+ * @param json
30
+ * @param templateData
31
+ * @returns
32
+ */
33
+ export declare function resolveFromObject(json: object, templateData?: TemplateData): Promise<object>;
26
34
  /**
27
35
  * Given file and a relative file path, returns full path of the relative file
28
36
  * Supports s3 links for base file
@@ -80,6 +80,34 @@ export async function resolveYamlDoc(file, templateData) {
80
80
  });
81
81
  return cfgRaw;
82
82
  }
83
+ /**
84
+ * Helper function that gets object with templated values
85
+ * Just because async ast already exists in yaml, but not in json reviver
86
+ * @param json
87
+ * @param templateData
88
+ * @returns
89
+ */
90
+ export async function resolveFromObject(json, templateData) {
91
+ const content = YAML.stringify(json);
92
+ const cfgRaw = YAML.parseDocument(content, { merge: true });
93
+ await YAML.visitAsync(cfgRaw, {
94
+ Scalar: async (_, node) => {
95
+ if (typeof node.value === "string") {
96
+ let val = node.value;
97
+ const matches = val.matchAll(templateKeyRegex);
98
+ for (const match of matches) {
99
+ const templateKey = match[0].slice(2, -1);
100
+ const templateValue = await getPromisePath(templateData, templateKey);
101
+ if (templateValue) {
102
+ val = val.replace(match[0], templateValue);
103
+ }
104
+ }
105
+ node.value = val;
106
+ }
107
+ },
108
+ });
109
+ return cfgRaw.toJSON();
110
+ }
83
111
  function getNodesPath(nodes) {
84
112
  return nodes
85
113
  .map(n => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/cli-utils",
3
3
  "description": "Utils for creating cli apps",
4
- "version": "5.69.0",
4
+ "version": "5.69.1",
5
5
  "homepage": "https://gearbox.fi",
6
6
  "keywords": [
7
7
  "gearbox"