@gearbox-protocol/cli-utils 5.68.3 → 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.d.ts CHANGED
@@ -13,6 +13,10 @@ export interface ZommandOptions<T extends z4.$ZodTypes> {
13
13
  * If value is string, it's used as default value for config file
14
14
  */
15
15
  configFile?: boolean | string;
16
+ /**
17
+ * If true, allows to pass config zlib compressed config as --inline-config=... or INLINE_CONFIG env var
18
+ */
19
+ inlineConfig?: boolean;
16
20
  }
17
21
  export declare class Zommand<T extends z4.$ZodTypes, Args extends any[] = []> extends Command<Args, z4.output<T>> {
18
22
  #private;
package/dist/Zommand.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Command, Option } from "@commander-js/extra-typings";
2
2
  import * as z4 from "zod/v4/core";
3
+ import { decompressObject } from "./compressedConfig.js";
3
4
  import { resolveYamlFiles } from "./resolveYamlFiles.js";
4
5
  export const zommandRegistry = z4.registry();
5
6
  export class Zommand extends Command {
@@ -8,7 +9,7 @@ export class Zommand extends Command {
8
9
  #envToObjectKeys = {};
9
10
  constructor(name, opts) {
10
11
  super(name);
11
- const { schema, templateData, configFile = true } = opts;
12
+ const { schema, templateData, configFile = true, inlineConfig = true, } = opts;
12
13
  this.#schema = schema;
13
14
  this.#templatedData = templateData;
14
15
  const def = this.#schema._zod.def;
@@ -33,6 +34,9 @@ export class Zommand extends Command {
33
34
  }
34
35
  this.addOption(opt);
35
36
  }
37
+ if (inlineConfig) {
38
+ this.addOption(new Option("--inline-config [config]", "inline config"));
39
+ }
36
40
  }
37
41
  #addOptionsFromObject(def) {
38
42
  const shape = def.shape;
@@ -59,7 +63,7 @@ export class Zommand extends Command {
59
63
  return super.action(async (...args) => {
60
64
  const argz = args;
61
65
  const _cmd = argz.splice(-1);
62
- const [{ config, ...opts }] = argz.splice(-1);
66
+ const [{ config, inlineConfig, ...opts }] = argz.splice(-1);
63
67
  let fromYaml = {};
64
68
  if (config) {
65
69
  fromYaml = await resolveYamlFiles(config, {
@@ -67,6 +71,13 @@ export class Zommand extends Command {
67
71
  ...this.#templatedData,
68
72
  });
69
73
  }
74
+ let fromInlineConfig = {};
75
+ if (inlineConfig) {
76
+ fromInlineConfig = await decompressObject(inlineConfig, this.#templatedData);
77
+ }
78
+ else if (process.env.INLINE_CONFIG) {
79
+ fromInlineConfig = await decompressObject(process.env.INLINE_CONFIG, this.#templatedData);
80
+ }
70
81
  const schemaFromEnv = {};
71
82
  for (const [env, key] of Object.entries(this.#envToObjectKeys)) {
72
83
  if (process.env[env]) {
@@ -77,6 +88,7 @@ export class Zommand extends Command {
77
88
  const combined = {
78
89
  ...schemaFromEnv,
79
90
  ...fromYaml,
91
+ ...fromInlineConfig,
80
92
  ...opts,
81
93
  };
82
94
  const data = z4.parse(this.#schema, combined);
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Compress a plain JS object into a base64url string
3
+ * suitable for env vars.
4
+ */
5
+ export declare function compressObject(obj: unknown): string;
6
+ /**
7
+ * Decompress a base64url string back into a JS object
8
+ * Allows to pass template data to resolve promises/env
9
+ */
10
+ export declare function decompressObject<T = unknown>(value: string, templateData?: Record<string, unknown>): Promise<T>;
@@ -0,0 +1,22 @@
1
+ import { deflateSync, inflateSync } from "node:zlib";
2
+ import { resolveFromObject } from "./resolveYamlFiles.js";
3
+ /**
4
+ * Compress a plain JS object into a base64url string
5
+ * suitable for env vars.
6
+ */
7
+ export function compressObject(obj) {
8
+ const json = JSON.stringify(obj);
9
+ const compressed = deflateSync(Buffer.from(json, "utf8"));
10
+ return compressed.toString("base64url");
11
+ }
12
+ /**
13
+ * Decompress a base64url string back into a JS object
14
+ * Allows to pass template data to resolve promises/env
15
+ */
16
+ export async function decompressObject(value, templateData) {
17
+ const compressed = Buffer.from(value, "base64url");
18
+ const str = inflateSync(compressed).toString("utf8");
19
+ const json = JSON.parse(str);
20
+ const result = await resolveFromObject(json, templateData);
21
+ return result;
22
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./CensoredString.js";
2
2
  export * from "./CensoredURL.js";
3
+ export * from "./compressedConfig.js";
3
4
  export * from "./createRevolverTransport.js";
4
5
  export * from "./notifications/index.js";
5
6
  export * from "./providers.js";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./CensoredString.js";
2
2
  export * from "./CensoredURL.js";
3
+ export * from "./compressedConfig.js";
3
4
  export * from "./createRevolverTransport.js";
4
5
  export * from "./notifications/index.js";
5
6
  export * from "./providers.js";
@@ -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.68.3",
4
+ "version": "5.69.1",
5
5
  "homepage": "https://gearbox.fi",
6
6
  "keywords": [
7
7
  "gearbox"