@gearbox-protocol/cli-utils 5.38.5 → 5.39.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
@@ -6,7 +6,7 @@ export declare const zommandRegistry: z4.$ZodRegistry<{
6
6
  env?: string;
7
7
  description?: string;
8
8
  }, z4.$ZodType<unknown, unknown, z4.$ZodTypeInternals<unknown, unknown>>>;
9
- export interface ZommandOptions<T extends z4.$ZodObject> {
9
+ export interface ZommandOptions<T extends z4.$ZodTypes> {
10
10
  schema: T;
11
11
  templateData?: TemplateData;
12
12
  /**
@@ -14,7 +14,7 @@ export interface ZommandOptions<T extends z4.$ZodObject> {
14
14
  */
15
15
  configFile?: boolean | string;
16
16
  }
17
- export declare class Zommand<T extends z4.$ZodObject, Args extends any[] = []> extends Command<Args, z4.output<T>> {
17
+ export declare class Zommand<T extends z4.$ZodTypes, Args extends any[] = []> extends Command<Args, z4.output<T>> {
18
18
  #private;
19
19
  constructor(name: string, opts: ZommandOptions<T>);
20
20
  action(fn: (this: this, ...args: [...Args, z4.output<T>, this]) => void | Promise<void>): this;
package/dist/Zommand.js CHANGED
@@ -5,21 +5,26 @@ export const zommandRegistry = z4.registry();
5
5
  export class Zommand extends Command {
6
6
  #schema;
7
7
  #templatedData;
8
+ #envToObjectKeys = {};
8
9
  constructor(name, opts) {
9
10
  super(name);
10
11
  const { schema, templateData, configFile = true } = opts;
11
12
  this.#schema = schema;
12
13
  this.#templatedData = templateData;
13
14
  const def = this.#schema._zod.def;
14
- if (def.type !== "object") {
15
- throw new Error(`expected object schema, got ${this.#schema._zod.def.type}`);
15
+ if (def.type === "object") {
16
+ this.#addOptionsFromObject(def);
16
17
  }
17
- for (const optionSchema of Object.values(def.shape)) {
18
- const meta = zommandRegistry.get(optionSchema);
19
- if (!meta) {
20
- continue;
18
+ else if (def.type === "union") {
19
+ for (const o of def.options) {
20
+ const opt = o;
21
+ if (opt._zod.def.type === "object") {
22
+ this.#addOptionsFromObject(opt._zod.def);
23
+ }
21
24
  }
22
- this.addOption(new Option(meta.flags, meta.description));
25
+ }
26
+ else {
27
+ throw new Error(`expected object schema, got ${this.#schema._zod.def.type}`);
23
28
  }
24
29
  if (configFile) {
25
30
  let opt = new Option("--config [file]", "config file");
@@ -29,6 +34,23 @@ export class Zommand extends Command {
29
34
  this.addOption(opt);
30
35
  }
31
36
  }
37
+ #addOptionsFromObject(def) {
38
+ const shape = def.shape;
39
+ for (const [key, optionSchema] of Object.entries(shape)) {
40
+ const meta = zommandRegistry.get(optionSchema);
41
+ if (!meta) {
42
+ continue;
43
+ }
44
+ // this avoids duplicate options in case of discriminated unions
45
+ const existing = this.options.find(o => o.flags === meta.flags);
46
+ if (!existing) {
47
+ this.addOption(new Option(meta.flags, meta.description));
48
+ if (meta.env) {
49
+ this.#envToObjectKeys[meta.env] = key;
50
+ }
51
+ }
52
+ }
53
+ }
32
54
  action(fn) {
33
55
  return super.action(async (...args) => {
34
56
  const argz = args;
@@ -41,12 +63,10 @@ export class Zommand extends Command {
41
63
  ...this.#templatedData,
42
64
  });
43
65
  }
44
- const def = this.#schema._zod.def;
45
66
  const schemaFromEnv = {};
46
- for (const [key, optionSchema] of Object.entries(def.shape)) {
47
- const meta = zommandRegistry.get(optionSchema);
48
- if (meta?.env && process.env[meta.env]) {
49
- schemaFromEnv[key] = process.env[meta.env];
67
+ for (const [env, key] of Object.entries(this.#envToObjectKeys)) {
68
+ if (process.env[env]) {
69
+ schemaFromEnv[key] = process.env[env];
50
70
  }
51
71
  }
52
72
  // deep merge is not required: env and cli flags are for top-level only
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.38.5",
4
+ "version": "5.39.1",
5
5
  "homepage": "https://gearbox.fi",
6
6
  "keywords": [
7
7
  "gearbox"