@gearbox-protocol/cli-utils 6.8.3 → 6.9.0

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.
@@ -8,8 +8,31 @@ import getPromisePath from "./getPromisePath.js";
8
8
  * Consists of alphanumeric+underscore parts separated by dots, all wrappped in env-style brackets
9
9
  * e.g. ${KEY} or ${KEY.SUBKEY}
10
10
  *
11
+ * Optionally supports a bash-style default value via `:-`, e.g. `${KEY:-fallback}`.
12
+ * The default is used when the resolved value is undefined, null, or an empty string.
13
+ * Capture groups: 1 = key path, 2 = optional default literal (no `}` allowed).
11
14
  */
12
- const templateKeyRegex = /\$\{(?:[\w-]+(?:\.[\w-]+)*)\}/g;
15
+ const templateKeyRegex = /\$\{([\w-]+(?:\.[\w-]+)*)(?::-([^}]*))?\}/g;
16
+ /**
17
+ * Replaces all `${...}` template tokens in a string using the provided template data.
18
+ * Supports bash-style `${KEY:-default}` fallback when the resolved value is missing
19
+ * (undefined, null, or empty string). The default is a literal and is not further
20
+ * interpolated.
21
+ */
22
+ async function resolveScalarValue(val, templateData) {
23
+ const matches = val.matchAll(templateKeyRegex);
24
+ for (const match of matches) {
25
+ const [whole, key, defaultValue] = match;
26
+ const resolved = await getPromisePath(templateData, key);
27
+ if (resolved !== undefined && resolved !== null && resolved !== "") {
28
+ val = val.replace(whole, String(resolved));
29
+ }
30
+ else if (defaultValue !== undefined) {
31
+ val = val.replace(whole, defaultValue);
32
+ }
33
+ }
34
+ return val;
35
+ }
13
36
  /**
14
37
  * Merges yaml files, respecting .extends references.
15
38
  * Performs env variable substitution on the config.
@@ -65,16 +88,7 @@ export async function resolveYamlDoc(file, templateData) {
65
88
  await YAML.visitAsync(cfgRaw, {
66
89
  Scalar: async (_, node) => {
67
90
  if (typeof node.value === "string") {
68
- let val = node.value;
69
- const matches = val.matchAll(templateKeyRegex);
70
- for (const match of matches) {
71
- const templateKey = match[0].slice(2, -1);
72
- const templateValue = await getPromisePath(templateData, templateKey);
73
- if (templateValue) {
74
- val = val.replace(match[0], templateValue);
75
- }
76
- }
77
- node.value = val;
91
+ node.value = await resolveScalarValue(node.value, templateData);
78
92
  }
79
93
  },
80
94
  });
@@ -93,16 +107,7 @@ export async function resolveFromObject(json, templateData) {
93
107
  await YAML.visitAsync(cfgRaw, {
94
108
  Scalar: async (_, node) => {
95
109
  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;
110
+ node.value = await resolveScalarValue(node.value, templateData);
106
111
  }
107
112
  },
108
113
  });
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": "6.8.3",
4
+ "version": "6.9.0",
5
5
  "homepage": "https://gearbox.fi",
6
6
  "keywords": [
7
7
  "gearbox"
@@ -27,7 +27,7 @@
27
27
  "@aws-sdk/client-s3": "^3.1033.0",
28
28
  "@aws-sdk/client-secrets-manager": "^3.1033.0",
29
29
  "@aws-sdk/client-ssm": "^3.1033.0",
30
- "@gearbox-protocol/sdk": "^14.8.0-next.1",
30
+ "@gearbox-protocol/sdk": "^14.8.1",
31
31
  "@vlad-yakovlev/telegram-md": "^2.1.0",
32
32
  "abitype": "^1.2.4",
33
33
  "commander": "^14.0.3",