@gearbox-protocol/cli-utils 5.33.0-next.2 → 5.33.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.
- package/dist/Zommand.d.ts +17 -0
- package/dist/Zommand.js +58 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/schema-primitives.d.ts +9 -0
- package/dist/schema-primitives.js +25 -0
- package/package.json +4 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Command } from "@commander-js/extra-typings";
|
|
2
|
+
import * as z4 from "zod/v4/core";
|
|
3
|
+
export declare const zodCmdRegistry: z4.$ZodRegistry<{
|
|
4
|
+
flags: string;
|
|
5
|
+
env?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
}, z4.$ZodType<unknown, unknown>>;
|
|
8
|
+
export interface ZommandOptions<T extends z4.$ZodObject> {
|
|
9
|
+
schema: T;
|
|
10
|
+
templateData?: Record<string, string>;
|
|
11
|
+
configFile?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class Zommand<T extends z4.$ZodObject, Args extends any[] = []> extends Command<Args, z4.output<T>> {
|
|
14
|
+
#private;
|
|
15
|
+
constructor(name: string, opts: ZommandOptions<T>);
|
|
16
|
+
action(fn: (this: this, ...args: [...Args, z4.output<T>, this]) => void | Promise<void>): this;
|
|
17
|
+
}
|
package/dist/Zommand.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Command, Option } from "@commander-js/extra-typings";
|
|
2
|
+
import * as z4 from "zod/v4/core";
|
|
3
|
+
import { resolveYamlFiles } from "./resolveYamlFiles.js";
|
|
4
|
+
export const zodCmdRegistry = z4.registry();
|
|
5
|
+
export class Zommand extends Command {
|
|
6
|
+
#schema;
|
|
7
|
+
#templatedData;
|
|
8
|
+
constructor(name, opts) {
|
|
9
|
+
super(name);
|
|
10
|
+
const { schema, templateData, configFile = true } = opts;
|
|
11
|
+
this.#schema = schema;
|
|
12
|
+
this.#templatedData = templateData;
|
|
13
|
+
const def = this.#schema._zod.def;
|
|
14
|
+
if (def.type !== "object") {
|
|
15
|
+
throw new Error(`expected object schema, got ${this.#schema._zod.def.type}`);
|
|
16
|
+
}
|
|
17
|
+
for (const optionSchema of Object.values(def.shape)) {
|
|
18
|
+
const meta = zodCmdRegistry.get(optionSchema);
|
|
19
|
+
if (!meta) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
this.addOption(new Option(meta.flags, meta.description));
|
|
23
|
+
}
|
|
24
|
+
if (configFile) {
|
|
25
|
+
this.addOption(new Option("--config [file]", "config file"));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
action(fn) {
|
|
29
|
+
return super.action(async (...args) => {
|
|
30
|
+
const argz = args;
|
|
31
|
+
const _cmd = argz.splice(-1);
|
|
32
|
+
let [{ config, ...opts }] = argz.splice(-1);
|
|
33
|
+
let fromYaml = {};
|
|
34
|
+
if (config) {
|
|
35
|
+
fromYaml = await resolveYamlFiles(config, {
|
|
36
|
+
...process.env,
|
|
37
|
+
...this.#templatedData,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const def = this.#schema._zod.def;
|
|
41
|
+
const schemaFromEnv = {};
|
|
42
|
+
for (const [key, optionSchema] of Object.entries(def.shape)) {
|
|
43
|
+
const meta = zodCmdRegistry.get(optionSchema);
|
|
44
|
+
if (meta?.env && process.env[meta.env]) {
|
|
45
|
+
schemaFromEnv[key] = process.env[meta.env];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// deep merge is not required: env and cli flags are for top-level only
|
|
49
|
+
let combined = {
|
|
50
|
+
...schemaFromEnv,
|
|
51
|
+
...fromYaml,
|
|
52
|
+
...opts,
|
|
53
|
+
};
|
|
54
|
+
const data = await z4.parse(this.#schema, combined);
|
|
55
|
+
await fn.apply(this, [...argz, data, this]);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from "zod/v4";
|
|
2
|
+
export declare const boolLike: z.ZodUnion<readonly [z.ZodCoercedBoolean<unknown>, z.ZodPipe<z.ZodUnknown, z.ZodBoolean>]>;
|
|
3
|
+
export declare const stringArrayLike: z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>]>;
|
|
4
|
+
/**
|
|
5
|
+
* Like Address from abitype/zod, but for zod/v4
|
|
6
|
+
*/
|
|
7
|
+
export declare const addressLike: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
|
|
8
|
+
export declare const addressArrayLike: z.ZodPipe<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>]>, z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>>>;
|
|
9
|
+
export declare const optionalAddressArrayLike: z.ZodPipe<z.ZodOptional<z.ZodPipe<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>]>, z.ZodArray<z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>>>>, z.ZodTransform<`0x${string}`[] | undefined, `0x${string}`[] | undefined>>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { z } from "zod/v4";
|
|
2
|
+
export const boolLike = z.union([z.coerce.boolean(), z.stringbool()]);
|
|
3
|
+
export const stringArrayLike = z.union([
|
|
4
|
+
z.array(z.string()),
|
|
5
|
+
z.string().transform(val => val.split(",")),
|
|
6
|
+
]);
|
|
7
|
+
/**
|
|
8
|
+
* Like Address from abitype/zod, but for zod/v4
|
|
9
|
+
*/
|
|
10
|
+
export const addressLike = z.string().transform((val, ctx) => {
|
|
11
|
+
const regex = /^0x[a-fA-F0-9]{40}$/;
|
|
12
|
+
if (!regex.test(val)) {
|
|
13
|
+
ctx.issues.push({
|
|
14
|
+
code: "custom",
|
|
15
|
+
message: `invalid address ${val}`,
|
|
16
|
+
input: ctx.value,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return val;
|
|
20
|
+
});
|
|
21
|
+
export const addressArrayLike = stringArrayLike.pipe(z.array(addressLike));
|
|
22
|
+
export const optionalAddressArrayLike = stringArrayLike
|
|
23
|
+
.pipe(z.array(addressLike))
|
|
24
|
+
.optional()
|
|
25
|
+
.transform(v => (v?.length ? v : undefined));
|
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.33.0
|
|
4
|
+
"version": "5.33.0",
|
|
5
5
|
"homepage": "https://gearbox.fi",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"gearbox"
|
|
@@ -33,11 +33,14 @@
|
|
|
33
33
|
"package:version": "yarn version"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"abitype": "^1.0.8",
|
|
37
|
+
"commander": "^14.0.0",
|
|
36
38
|
"lodash-es": "^4.17.21",
|
|
37
39
|
"yaml": "^2.8.0",
|
|
38
40
|
"zod": "^3.25.42"
|
|
39
41
|
},
|
|
40
42
|
"devDependencies": {
|
|
43
|
+
"@commander-js/extra-typings": "^14.0.0",
|
|
41
44
|
"@types/lodash-es": "^4.17.12",
|
|
42
45
|
"@types/node": "^22.15.21"
|
|
43
46
|
},
|