@variantlab/cli 0.1.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/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/index.cjs +458 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +451 -0
- package/dist/index.js.map +1 -0
- package/dist/variantlab.js +664 -0
- package/dist/variantlab.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 variantlab contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var core = require('@variantlab/core');
|
|
5
|
+
var promises = require('fs/promises');
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
|
|
8
|
+
// src/commands/eval.ts
|
|
9
|
+
async function readTextFile(path) {
|
|
10
|
+
return promises.readFile(path, "utf-8");
|
|
11
|
+
}
|
|
12
|
+
async function writeTextFile(path$1, content) {
|
|
13
|
+
await promises.mkdir(path.dirname(path$1), { recursive: true });
|
|
14
|
+
await promises.writeFile(path$1, content, "utf-8");
|
|
15
|
+
}
|
|
16
|
+
async function fileExists(path) {
|
|
17
|
+
try {
|
|
18
|
+
await promises.access(path);
|
|
19
|
+
return true;
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/utils/printer.ts
|
|
26
|
+
var ESC = "\x1B[";
|
|
27
|
+
var RESET = `${ESC}0m`;
|
|
28
|
+
var codes = {
|
|
29
|
+
red: `${ESC}31m`,
|
|
30
|
+
green: `${ESC}32m`,
|
|
31
|
+
yellow: `${ESC}33m`,
|
|
32
|
+
blue: `${ESC}34m`,
|
|
33
|
+
magenta: `${ESC}35m`,
|
|
34
|
+
cyan: `${ESC}36m`,
|
|
35
|
+
gray: `${ESC}90m`,
|
|
36
|
+
bold: `${ESC}1m`,
|
|
37
|
+
dim: `${ESC}2m`
|
|
38
|
+
};
|
|
39
|
+
function supportsColor() {
|
|
40
|
+
if (typeof process === "undefined") return false;
|
|
41
|
+
if (process.env.NO_COLOR !== void 0) return false;
|
|
42
|
+
if (process.env.FORCE_COLOR !== void 0) return true;
|
|
43
|
+
if (process.stdout && "isTTY" in process.stdout) return !!process.stdout.isTTY;
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
var useColor = supportsColor();
|
|
47
|
+
function wrap(code, text) {
|
|
48
|
+
return useColor ? `${code}${text}${RESET}` : text;
|
|
49
|
+
}
|
|
50
|
+
var color = {
|
|
51
|
+
red: (t) => wrap(codes.red, t),
|
|
52
|
+
green: (t) => wrap(codes.green, t),
|
|
53
|
+
yellow: (t) => wrap(codes.yellow, t),
|
|
54
|
+
blue: (t) => wrap(codes.blue, t),
|
|
55
|
+
magenta: (t) => wrap(codes.magenta, t),
|
|
56
|
+
cyan: (t) => wrap(codes.cyan, t),
|
|
57
|
+
gray: (t) => wrap(codes.gray, t),
|
|
58
|
+
bold: (t) => wrap(codes.bold, t),
|
|
59
|
+
dim: (t) => wrap(codes.dim, t)
|
|
60
|
+
};
|
|
61
|
+
function success(msg) {
|
|
62
|
+
console.log(`${color.green("\u2713")} ${msg}`);
|
|
63
|
+
}
|
|
64
|
+
function info(msg) {
|
|
65
|
+
console.log(`${color.blue("\u2139")} ${msg}`);
|
|
66
|
+
}
|
|
67
|
+
function error(msg) {
|
|
68
|
+
console.error(`${color.red("\u2717")} ${msg}`);
|
|
69
|
+
}
|
|
70
|
+
function verbose(msg, isVerbose) {
|
|
71
|
+
if (isVerbose) console.log(`${color.dim(` ${msg}`)}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/commands/eval.ts
|
|
75
|
+
async function evalCommand(configPathArg, options = {}) {
|
|
76
|
+
const configPath = path.resolve(configPathArg ?? "experiments.json");
|
|
77
|
+
if (!options.experiment) {
|
|
78
|
+
error("Missing required flag: --experiment <id>");
|
|
79
|
+
return 4;
|
|
80
|
+
}
|
|
81
|
+
if (!await fileExists(configPath)) {
|
|
82
|
+
error(`Config not found: ${configPath}`);
|
|
83
|
+
return 1;
|
|
84
|
+
}
|
|
85
|
+
let config;
|
|
86
|
+
try {
|
|
87
|
+
const raw = await readTextFile(configPath);
|
|
88
|
+
const parsed = JSON.parse(raw);
|
|
89
|
+
config = core.validateConfig(parsed);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
if (err instanceof core.ConfigValidationError) {
|
|
92
|
+
error(`Invalid config: ${err.issues.length} issue(s)`);
|
|
93
|
+
return 2;
|
|
94
|
+
}
|
|
95
|
+
error(`Failed to load config: ${err.message}`);
|
|
96
|
+
return err instanceof SyntaxError ? 2 : 3;
|
|
97
|
+
}
|
|
98
|
+
let context = {};
|
|
99
|
+
if (options.contextFile) {
|
|
100
|
+
try {
|
|
101
|
+
const raw = await readTextFile(path.resolve(options.contextFile));
|
|
102
|
+
context = JSON.parse(raw);
|
|
103
|
+
} catch (err) {
|
|
104
|
+
error(`Failed to load context file: ${err.message}`);
|
|
105
|
+
return 3;
|
|
106
|
+
}
|
|
107
|
+
} else if (options.context) {
|
|
108
|
+
try {
|
|
109
|
+
context = JSON.parse(options.context);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
error(`Invalid context JSON: ${err.message}`);
|
|
112
|
+
return 4;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const experiment = config.experiments.find((e) => e.id === options.experiment);
|
|
116
|
+
if (!experiment) {
|
|
117
|
+
error(`Experiment not found: ${options.experiment}`);
|
|
118
|
+
const ids = config.experiments.map((e) => e.id);
|
|
119
|
+
if (ids.length > 0) {
|
|
120
|
+
info(`Available experiments: ${ids.join(", ")}`);
|
|
121
|
+
}
|
|
122
|
+
return 4;
|
|
123
|
+
}
|
|
124
|
+
console.log(color.bold(`Experiment: ${experiment.id}`));
|
|
125
|
+
console.log(` Name: ${experiment.name}`);
|
|
126
|
+
console.log(` Type: ${experiment.type ?? "render"}`);
|
|
127
|
+
console.log(` Assignment: ${experiment.assignment ?? "default"}`);
|
|
128
|
+
console.log(` Default: ${experiment.default}`);
|
|
129
|
+
console.log(` Variants: ${experiment.variants.map((v) => v.id).join(", ")}`);
|
|
130
|
+
console.log("");
|
|
131
|
+
console.log(color.bold("Context:"));
|
|
132
|
+
if (Object.keys(context).length === 0) {
|
|
133
|
+
console.log(" (empty)");
|
|
134
|
+
} else {
|
|
135
|
+
for (const [key, val] of Object.entries(context)) {
|
|
136
|
+
console.log(` ${key}: ${JSON.stringify(val)}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
console.log("");
|
|
140
|
+
console.log(color.bold("Targeting trace:"));
|
|
141
|
+
const result = core.explain(experiment, context);
|
|
142
|
+
if (result.steps.length === 0) {
|
|
143
|
+
console.log(" (no targeting rules)");
|
|
144
|
+
} else {
|
|
145
|
+
for (const step of result.steps) {
|
|
146
|
+
const icon = step.matched ? color.green("PASS") : color.red("FAIL");
|
|
147
|
+
const detail = step.detail ? ` \u2014 ${step.detail}` : "";
|
|
148
|
+
console.log(` ${icon} ${step.field}${detail}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
console.log("");
|
|
152
|
+
const engine = core.createEngine(config, { context });
|
|
153
|
+
const variantId = engine.getVariant(experiment.id);
|
|
154
|
+
const variant = experiment.variants.find((v) => v.id === variantId);
|
|
155
|
+
console.log(color.bold("Result:"));
|
|
156
|
+
console.log(` Targeted: ${result.matched ? color.green("yes") : color.red("no")}`);
|
|
157
|
+
console.log(` Variant: ${color.cyan(variantId)}`);
|
|
158
|
+
if (variant?.value !== void 0) {
|
|
159
|
+
console.log(` Value: ${JSON.stringify(variant.value)}`);
|
|
160
|
+
}
|
|
161
|
+
if (!result.matched && result.reason) {
|
|
162
|
+
console.log(` Reason: failed on ${color.yellow(result.reason)}`);
|
|
163
|
+
}
|
|
164
|
+
console.log("");
|
|
165
|
+
engine.dispose();
|
|
166
|
+
return 0;
|
|
167
|
+
}
|
|
168
|
+
async function generate(options = {}) {
|
|
169
|
+
const configPath = path.resolve(options.config ?? "experiments.json");
|
|
170
|
+
const outPath = path.resolve(options.out ?? "src/variantlab.generated.ts");
|
|
171
|
+
const code = await runGenerate(configPath, outPath, options.verbose ?? false);
|
|
172
|
+
if (code !== 0) return code;
|
|
173
|
+
if (options.watch) {
|
|
174
|
+
info(`Watching ${configPath} for changes...`);
|
|
175
|
+
let debounce = null;
|
|
176
|
+
fs.watch(configPath, () => {
|
|
177
|
+
if (debounce) clearTimeout(debounce);
|
|
178
|
+
debounce = setTimeout(async () => {
|
|
179
|
+
info("Config changed, regenerating...");
|
|
180
|
+
await runGenerate(configPath, outPath, options.verbose ?? false);
|
|
181
|
+
}, 100);
|
|
182
|
+
});
|
|
183
|
+
await new Promise(() => {
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
async function runGenerate(configPath, outPath, isVerbose) {
|
|
189
|
+
if (!await fileExists(configPath)) {
|
|
190
|
+
error(`Config not found: ${configPath}`);
|
|
191
|
+
return 1;
|
|
192
|
+
}
|
|
193
|
+
let raw;
|
|
194
|
+
try {
|
|
195
|
+
raw = await readTextFile(configPath);
|
|
196
|
+
} catch (err) {
|
|
197
|
+
error(`Failed to read config: ${err.message}`);
|
|
198
|
+
return 3;
|
|
199
|
+
}
|
|
200
|
+
let parsed;
|
|
201
|
+
try {
|
|
202
|
+
parsed = JSON.parse(raw);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
error(`Invalid JSON: ${err.message}`);
|
|
205
|
+
return 2;
|
|
206
|
+
}
|
|
207
|
+
let config;
|
|
208
|
+
try {
|
|
209
|
+
config = core.validateConfig(parsed);
|
|
210
|
+
} catch (err) {
|
|
211
|
+
error(`Validation failed: ${err.message}`);
|
|
212
|
+
return 2;
|
|
213
|
+
}
|
|
214
|
+
verbose(`Loaded ${config.experiments.length} experiment(s)`, isVerbose);
|
|
215
|
+
const output = emitTypeScript(config);
|
|
216
|
+
try {
|
|
217
|
+
await writeTextFile(outPath, output);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
error(`Failed to write output: ${err.message}`);
|
|
220
|
+
return 3;
|
|
221
|
+
}
|
|
222
|
+
success(`Generated ${outPath}`);
|
|
223
|
+
return 0;
|
|
224
|
+
}
|
|
225
|
+
function emitTypeScript(config) {
|
|
226
|
+
const lines = [
|
|
227
|
+
"// AUTO-GENERATED by @variantlab/cli",
|
|
228
|
+
`// Generated at ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
229
|
+
"// DO NOT EDIT BY HAND",
|
|
230
|
+
"// Regenerate with: variantlab generate",
|
|
231
|
+
"",
|
|
232
|
+
"export interface VariantLabExperiments {"
|
|
233
|
+
];
|
|
234
|
+
for (const exp of config.experiments) {
|
|
235
|
+
if (exp.name) {
|
|
236
|
+
lines.push(` /** ${escapeComment(exp.name)} */`);
|
|
237
|
+
}
|
|
238
|
+
lines.push(` "${exp.id}": {`);
|
|
239
|
+
lines.push(` type: "${exp.type ?? "render"}";`);
|
|
240
|
+
lines.push(` variants: ${variantUnion(exp)};`);
|
|
241
|
+
lines.push(` default: "${exp.default}";`);
|
|
242
|
+
if (exp.type === "value") {
|
|
243
|
+
lines.push(` value: ${valueUnion(exp)};`);
|
|
244
|
+
}
|
|
245
|
+
lines.push(" };");
|
|
246
|
+
}
|
|
247
|
+
lines.push("}");
|
|
248
|
+
lines.push("");
|
|
249
|
+
lines.push('declare module "@variantlab/core" {');
|
|
250
|
+
lines.push(" interface VariantLabRegistry extends VariantLabExperiments {}");
|
|
251
|
+
lines.push("}");
|
|
252
|
+
lines.push("");
|
|
253
|
+
lines.push("export type ExperimentId = keyof VariantLabExperiments;");
|
|
254
|
+
lines.push("");
|
|
255
|
+
lines.push("export type VariantId<T extends ExperimentId> =");
|
|
256
|
+
lines.push(' VariantLabExperiments[T]["variants"];');
|
|
257
|
+
lines.push("");
|
|
258
|
+
lines.push("export type VariantValueType<T extends ExperimentId> =");
|
|
259
|
+
lines.push(" VariantLabExperiments[T] extends { value: infer V } ? V : never;");
|
|
260
|
+
lines.push("");
|
|
261
|
+
return lines.join("\n");
|
|
262
|
+
}
|
|
263
|
+
function variantUnion(exp) {
|
|
264
|
+
return exp.variants.map((v) => `"${escapeStr(v.id)}"`).join(" | ");
|
|
265
|
+
}
|
|
266
|
+
function valueUnion(exp) {
|
|
267
|
+
const values = exp.variants.map((v) => v.value).filter((v) => v !== void 0);
|
|
268
|
+
if (values.length === 0) return "never";
|
|
269
|
+
return values.map((v) => {
|
|
270
|
+
if (typeof v === "string") return `"${escapeStr(v)}"`;
|
|
271
|
+
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
272
|
+
return "unknown";
|
|
273
|
+
}).join(" | ");
|
|
274
|
+
}
|
|
275
|
+
function escapeStr(s) {
|
|
276
|
+
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
277
|
+
}
|
|
278
|
+
function escapeComment(s) {
|
|
279
|
+
return s.replace(/\*\//g, "* /");
|
|
280
|
+
}
|
|
281
|
+
var STARTER_CONFIG = `{
|
|
282
|
+
"$schema": "https://variantlab.dev/schemas/experiments.schema.json",
|
|
283
|
+
"version": 1,
|
|
284
|
+
"experiments": [
|
|
285
|
+
{
|
|
286
|
+
"id": "welcome-message",
|
|
287
|
+
"name": "Welcome message copy",
|
|
288
|
+
"type": "value",
|
|
289
|
+
"default": "hello",
|
|
290
|
+
"variants": [
|
|
291
|
+
{ "id": "hello", "label": "Hello", "value": "Hello, welcome!" },
|
|
292
|
+
{ "id": "hey", "label": "Hey", "value": "Hey there, glad you're here!" }
|
|
293
|
+
]
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
"id": "hero-layout",
|
|
297
|
+
"name": "Hero section layout",
|
|
298
|
+
"type": "render",
|
|
299
|
+
"default": "centered",
|
|
300
|
+
"variants": [
|
|
301
|
+
{ "id": "centered", "label": "Centered" },
|
|
302
|
+
{ "id": "split", "label": "Split view" }
|
|
303
|
+
]
|
|
304
|
+
}
|
|
305
|
+
]
|
|
306
|
+
}
|
|
307
|
+
`;
|
|
308
|
+
async function init(options = {}) {
|
|
309
|
+
const configPath = path.resolve("experiments.json");
|
|
310
|
+
if (!options.force && await fileExists(configPath)) {
|
|
311
|
+
error("experiments.json already exists. Use --force to overwrite.");
|
|
312
|
+
return 1;
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
await writeTextFile(configPath, STARTER_CONFIG);
|
|
316
|
+
success("Created experiments.json");
|
|
317
|
+
} catch (err) {
|
|
318
|
+
error(`Failed to write experiments.json: ${err.message}`);
|
|
319
|
+
return 3;
|
|
320
|
+
}
|
|
321
|
+
const pkgPath = path.resolve("package.json");
|
|
322
|
+
if (await fileExists(pkgPath)) {
|
|
323
|
+
try {
|
|
324
|
+
const raw = await readTextFile(pkgPath);
|
|
325
|
+
const pkg = JSON.parse(raw);
|
|
326
|
+
const scripts = pkg.scripts ?? {};
|
|
327
|
+
if (!scripts["variantlab:generate"]) {
|
|
328
|
+
scripts["variantlab:generate"] = "variantlab generate";
|
|
329
|
+
pkg.scripts = scripts;
|
|
330
|
+
await writeTextFile(pkgPath, `${JSON.stringify(pkg, null, 2)}
|
|
331
|
+
`);
|
|
332
|
+
success('Added "variantlab:generate" script to package.json');
|
|
333
|
+
}
|
|
334
|
+
} catch {
|
|
335
|
+
verbose("Could not update package.json (non-fatal)", true);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
console.log("");
|
|
339
|
+
info("Next steps:");
|
|
340
|
+
console.log(" 1. Edit experiments.json to define your experiments");
|
|
341
|
+
console.log(" 2. Run: variantlab generate");
|
|
342
|
+
console.log(" 3. Import the generated types in your app");
|
|
343
|
+
console.log("");
|
|
344
|
+
return 0;
|
|
345
|
+
}
|
|
346
|
+
async function validate(configPathArg, options = {}) {
|
|
347
|
+
const configPath = path.resolve(configPathArg ?? "experiments.json");
|
|
348
|
+
if (!await fileExists(configPath)) {
|
|
349
|
+
error(`Config not found: ${configPath}`);
|
|
350
|
+
return 1;
|
|
351
|
+
}
|
|
352
|
+
let raw;
|
|
353
|
+
try {
|
|
354
|
+
raw = await readTextFile(configPath);
|
|
355
|
+
} catch (err) {
|
|
356
|
+
error(`Failed to read config: ${err.message}`);
|
|
357
|
+
return 3;
|
|
358
|
+
}
|
|
359
|
+
let parsed;
|
|
360
|
+
try {
|
|
361
|
+
parsed = JSON.parse(raw);
|
|
362
|
+
} catch (err) {
|
|
363
|
+
error(`Invalid JSON in ${configPath}`);
|
|
364
|
+
error(err.message);
|
|
365
|
+
return 2;
|
|
366
|
+
}
|
|
367
|
+
try {
|
|
368
|
+
const config = core.validateConfig(parsed);
|
|
369
|
+
success(`Valid config: ${config.experiments.length} experiment(s)`);
|
|
370
|
+
if (options.verbose) {
|
|
371
|
+
for (const exp of config.experiments) {
|
|
372
|
+
console.log(
|
|
373
|
+
` ${color.cyan(exp.id)} \u2014 ${exp.variants.length} variants, assignment: ${exp.assignment ?? "default"}, status: ${exp.status ?? "active"}`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return 0;
|
|
378
|
+
} catch (err) {
|
|
379
|
+
if (err instanceof core.ConfigValidationError) {
|
|
380
|
+
error(`Validation failed with ${err.issues.length} issue(s):`);
|
|
381
|
+
console.log("");
|
|
382
|
+
for (const issue of err.issues) {
|
|
383
|
+
console.log(` ${color.red(issue.code)} at ${color.yellow(issue.path)}`);
|
|
384
|
+
console.log(` ${issue.message}`);
|
|
385
|
+
}
|
|
386
|
+
console.log("");
|
|
387
|
+
return 2;
|
|
388
|
+
}
|
|
389
|
+
error(`Unexpected error: ${err.message}`);
|
|
390
|
+
return 2;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// src/utils/arg-parser.ts
|
|
395
|
+
function parseArgs(argv) {
|
|
396
|
+
const flags = {};
|
|
397
|
+
const positionals = [];
|
|
398
|
+
let command;
|
|
399
|
+
let i = 0;
|
|
400
|
+
while (i < argv.length) {
|
|
401
|
+
const arg = argv[i];
|
|
402
|
+
if (arg === "--") {
|
|
403
|
+
positionals.push(...argv.slice(i + 1));
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
if (arg.startsWith("--")) {
|
|
407
|
+
const eqIdx = arg.indexOf("=");
|
|
408
|
+
if (eqIdx !== -1) {
|
|
409
|
+
const key = camelCase(arg.slice(2, eqIdx));
|
|
410
|
+
flags[key] = arg.slice(eqIdx + 1);
|
|
411
|
+
} else {
|
|
412
|
+
const raw = arg.slice(2);
|
|
413
|
+
if (raw.startsWith("no-")) {
|
|
414
|
+
flags[camelCase(raw.slice(3))] = false;
|
|
415
|
+
} else {
|
|
416
|
+
const key = camelCase(raw);
|
|
417
|
+
const next = argv[i + 1];
|
|
418
|
+
if (next !== void 0 && !next.startsWith("-")) {
|
|
419
|
+
flags[key] = next;
|
|
420
|
+
i++;
|
|
421
|
+
} else {
|
|
422
|
+
flags[key] = true;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
} else if (arg.startsWith("-") && arg.length === 2) {
|
|
427
|
+
const key = arg[1];
|
|
428
|
+
const next = argv[i + 1];
|
|
429
|
+
if (next !== void 0 && !next.startsWith("-")) {
|
|
430
|
+
flags[key] = next;
|
|
431
|
+
i++;
|
|
432
|
+
} else {
|
|
433
|
+
flags[key] = true;
|
|
434
|
+
}
|
|
435
|
+
} else if (command === void 0 && !arg.startsWith("-")) {
|
|
436
|
+
command = arg;
|
|
437
|
+
} else {
|
|
438
|
+
positionals.push(arg);
|
|
439
|
+
}
|
|
440
|
+
i++;
|
|
441
|
+
}
|
|
442
|
+
return { flags, positionals, command };
|
|
443
|
+
}
|
|
444
|
+
function camelCase(s) {
|
|
445
|
+
return s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// src/index.ts
|
|
449
|
+
var VERSION = "0.0.0";
|
|
450
|
+
|
|
451
|
+
exports.VERSION = VERSION;
|
|
452
|
+
exports.evalCommand = evalCommand;
|
|
453
|
+
exports.generate = generate;
|
|
454
|
+
exports.init = init;
|
|
455
|
+
exports.parseArgs = parseArgs;
|
|
456
|
+
exports.validate = validate;
|
|
457
|
+
//# sourceMappingURL=index.cjs.map
|
|
458
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/file.ts","../src/utils/printer.ts","../src/commands/eval.ts","../src/commands/generate.ts","../src/commands/init.ts","../src/commands/validate.ts","../src/utils/arg-parser.ts","../src/index.ts"],"names":["readFile","path","mkdir","dirname","writeFile","access","resolve","validateConfig","ConfigValidationError","explain","createEngine","watch"],"mappings":";;;;;;;;AAGA,eAAsB,aAAa,IAAA,EAA+B;AAChE,EAAA,OAAOA,iBAAA,CAAS,MAAM,OAAO,CAAA;AAC/B;AAEA,eAAsB,aAAA,CAAcC,QAAc,OAAA,EAAgC;AAChF,EAAA,MAAMC,eAAMC,YAAA,CAAQF,MAAI,GAAG,EAAE,SAAA,EAAW,MAAM,CAAA;AAC9C,EAAA,MAAMG,kBAAA,CAAUH,MAAA,EAAM,OAAA,EAAS,OAAO,CAAA;AACxC;AAEA,eAAsB,WAAW,IAAA,EAAgC;AAC/D,EAAA,IAAI;AACF,IAAA,MAAMI,gBAAO,IAAI,CAAA;AACjB,IAAA,OAAO,IAAA;AAAA,EACT,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,KAAA;AAAA,EACT;AACF;;;ACdA,IAAM,GAAA,GAAM,OAAA;AACZ,IAAM,KAAA,GAAQ,GAAG,GAAG,CAAA,EAAA,CAAA;AAEpB,IAAM,KAAA,GAAQ;AAAA,EACZ,GAAA,EAAK,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACX,KAAA,EAAO,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACb,MAAA,EAAQ,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACd,IAAA,EAAM,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACZ,OAAA,EAAS,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACf,IAAA,EAAM,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACZ,IAAA,EAAM,GAAG,GAAG,CAAA,GAAA,CAAA;AAAA,EACZ,IAAA,EAAM,GAAG,GAAG,CAAA,EAAA,CAAA;AAAA,EACZ,GAAA,EAAK,GAAG,GAAG,CAAA,E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s/promises\";\nimport { dirname } from \"node:path\";\n\nexport async function readTextFile(path: string): Promise<string> {\n return readFile(path, \"utf-8\");\n}\n\nexport async function writeTextFile(path: string, content: string): Promise<void> {\n await mkdir(dirname(path), { recursive: true });\n await writeFile(path, content, \"utf-8\");\n}\n\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await access(path);\n return true;\n } catch {\n return false;\n }\n}\n","/**\n * Coloured terminal output using ANSI escape codes directly.\n * No chalk, kleur, or any dependency.\n */\n\nconst ESC = \"\\x1b[\";\nconst RESET = `${ESC}0m`;\n\nconst codes = {\n red: `${ESC}31m`,\n green: `${ESC}32m`,\n yellow: `${ESC}33m`,\n blue: `${ESC}34m`,\n magenta: `${ESC}35m`,\n cyan: `${ESC}36m`,\n gray: `${ESC}90m`,\n bold: `${ESC}1m`,\n dim: `${ESC}2m`,\n} as const;\n\nfunction supportsColor(): boolean {\n if (typeof process === \"undefined\") return false;\n if (process.env.NO_COLOR !== undefined) return false;\n if (process.env.FORCE_COLOR !== undefined) return true;\n if (process.stdout && \"isTTY\" in process.stdout) return !!process.stdout.isTTY;\n return false;\n}\n\nconst useColor = supportsColor();\n\nfunction wrap(code: string, text: string): string {\n return useColor ? `${code}${text}${RESET}` : text;\n}\n\nexport const color = {\n red: (t: string) => wrap(codes.red, t),\n green: (t: string) => wrap(codes.green, t),\n yellow: (t: string) => wrap(codes.yellow, t),\n blue: (t: string) => wrap(codes.blue, t),\n magenta: (t: string) => wrap(codes.magenta, t),\n cyan: (t: string) => wrap(codes.cyan, t),\n gray: (t: string) => wrap(codes.gray, t),\n bold: (t: string) => wrap(codes.bold, t),\n dim: (t: string) => wrap(codes.dim, t),\n} as const;\n\nexport function success(msg: string): void {\n console.log(`${color.green(\"✓\")} ${msg}`);\n}\n\nexport function info(msg: string): void {\n console.log(`${color.blue(\"ℹ\")} ${msg}`);\n}\n\nexport function warn(msg: string): void {\n console.log(`${color.yellow(\"⚠\")} ${msg}`);\n}\n\nexport function error(msg: string): void {\n console.error(`${color.red(\"✗\")} ${msg}`);\n}\n\nexport function verbose(msg: string, isVerbose: boolean): void {\n if (isVerbose) console.log(`${color.dim(` ${msg}`)}`);\n}\n","import { resolve } from \"node:path\";\nimport {\n ConfigValidationError,\n createEngine,\n type ExperimentsConfig,\n explain,\n type VariantContext,\n validateConfig,\n} from \"@variantlab/core\";\nimport { fileExists, readTextFile } from \"../utils/file.js\";\nimport * as print from \"../utils/printer.js\";\nimport { color } from \"../utils/printer.js\";\n\nexport interface EvalOptions {\n readonly experiment?: string | undefined;\n readonly context?: string | undefined;\n readonly contextFile?: string | undefined;\n readonly verbose?: boolean | undefined;\n}\n\nexport async function evalCommand(\n configPathArg?: string,\n options: EvalOptions = {},\n): Promise<number> {\n const configPath = resolve(configPathArg ?? \"experiments.json\");\n\n if (!options.experiment) {\n print.error(\"Missing required flag: --experiment <id>\");\n return 4;\n }\n\n if (!(await fileExists(configPath))) {\n print.error(`Config not found: ${configPath}`);\n return 1;\n }\n\n // Load config\n let config: ExperimentsConfig;\n try {\n const raw = await readTextFile(configPath);\n const parsed = JSON.parse(raw);\n config = validateConfig(parsed);\n } catch (err) {\n if (err instanceof ConfigValidationError) {\n print.error(`Invalid config: ${err.issues.length} issue(s)`);\n return 2;\n }\n print.error(`Failed to load config: ${(err as Error).message}`);\n return err instanceof SyntaxError ? 2 : 3;\n }\n\n // Load context\n let context: VariantContext = {};\n if (options.contextFile) {\n try {\n const raw = await readTextFile(resolve(options.contextFile));\n context = JSON.parse(raw) as VariantContext;\n } catch (err) {\n print.error(`Failed to load context file: ${(err as Error).message}`);\n return 3;\n }\n } else if (options.context) {\n try {\n context = JSON.parse(options.context) as VariantContext;\n } catch (err) {\n print.error(`Invalid context JSON: ${(err as Error).message}`);\n return 4;\n }\n }\n\n // Find experiment\n const experiment = config.experiments.find((e) => e.id === options.experiment);\n if (!experiment) {\n print.error(`Experiment not found: ${options.experiment}`);\n const ids = config.experiments.map((e) => e.id);\n if (ids.length > 0) {\n print.info(`Available experiments: ${ids.join(\", \")}`);\n }\n return 4;\n }\n\n // Targeting trace\n console.log(color.bold(`Experiment: ${experiment.id}`));\n console.log(` Name: ${experiment.name}`);\n console.log(` Type: ${experiment.type ?? \"render\"}`);\n console.log(` Assignment: ${experiment.assignment ?? \"default\"}`);\n console.log(` Default: ${experiment.default}`);\n console.log(` Variants: ${experiment.variants.map((v) => v.id).join(\", \")}`);\n console.log(\"\");\n\n console.log(color.bold(\"Context:\"));\n if (Object.keys(context).length === 0) {\n console.log(\" (empty)\");\n } else {\n for (const [key, val] of Object.entries(context)) {\n console.log(` ${key}: ${JSON.stringify(val)}`);\n }\n }\n console.log(\"\");\n\n // Explain targeting\n console.log(color.bold(\"Targeting trace:\"));\n const result = explain(experiment, context);\n if (result.steps.length === 0) {\n console.log(\" (no targeting rules)\");\n } else {\n for (const step of result.steps) {\n const icon = step.matched ? color.green(\"PASS\") : color.red(\"FAIL\");\n const detail = step.detail ? ` — ${step.detail}` : \"\";\n console.log(` ${icon} ${step.field}${detail}`);\n }\n }\n console.log(\"\");\n\n // Resolve variant\n const engine = createEngine(config, { context });\n const variantId = engine.getVariant(experiment.id);\n const variant = experiment.variants.find((v) => v.id === variantId);\n\n console.log(color.bold(\"Result:\"));\n console.log(` Targeted: ${result.matched ? color.green(\"yes\") : color.red(\"no\")}`);\n console.log(` Variant: ${color.cyan(variantId)}`);\n if (variant?.value !== undefined) {\n console.log(` Value: ${JSON.stringify(variant.value)}`);\n }\n if (!result.matched && result.reason) {\n console.log(` Reason: failed on ${color.yellow(result.reason)}`);\n }\n console.log(\"\");\n\n engine.dispose();\n return 0;\n}\n","import { watch } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport { type Experiment, type ExperimentsConfig, validateConfig } from \"@variantlab/core\";\nimport { fileExists, readTextFile, writeTextFile } from \"../utils/file.js\";\nimport * as print from \"../utils/printer.js\";\n\nexport interface GenerateOptions {\n readonly config?: string | undefined;\n readonly out?: string | undefined;\n readonly watch?: boolean | undefined;\n readonly verbose?: boolean | undefined;\n}\n\nexport async function generate(options: GenerateOptions = {}): Promise<number> {\n const configPath = resolve(options.config ?? \"experiments.json\");\n const outPath = resolve(options.out ?? \"src/variantlab.generated.ts\");\n\n const code = await runGenerate(configPath, outPath, options.verbose ?? false);\n if (code !== 0) return code;\n\n if (options.watch) {\n print.info(`Watching ${configPath} for changes...`);\n let debounce: ReturnType<typeof setTimeout> | null = null;\n watch(configPath, () => {\n if (debounce) clearTimeout(debounce);\n debounce = setTimeout(async () => {\n print.info(\"Config changed, regenerating...\");\n await runGenerate(configPath, outPath, options.verbose ?? false);\n }, 100);\n });\n // Keep process alive in watch mode\n await new Promise(() => {});\n }\n\n return 0;\n}\n\nasync function runGenerate(\n configPath: string,\n outPath: string,\n isVerbose: boolean,\n): Promise<number> {\n if (!(await fileExists(configPath))) {\n print.error(`Config not found: ${configPath}`);\n return 1;\n }\n\n let raw: string;\n try {\n raw = await readTextFile(configPath);\n } catch (err) {\n print.error(`Failed to read config: ${(err as Error).message}`);\n return 3;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (err) {\n print.error(`Invalid JSON: ${(err as Error).message}`);\n return 2;\n }\n\n let config: ExperimentsConfig;\n try {\n config = validateConfig(parsed);\n } catch (err) {\n print.error(`Validation failed: ${(err as Error).message}`);\n return 2;\n }\n\n print.verbose(`Loaded ${config.experiments.length} experiment(s)`, isVerbose);\n\n const output = emitTypeScript(config);\n\n try {\n await writeTextFile(outPath, output);\n } catch (err) {\n print.error(`Failed to write output: ${(err as Error).message}`);\n return 3;\n }\n\n print.success(`Generated ${outPath}`);\n return 0;\n}\n\nfunction emitTypeScript(config: ExperimentsConfig): string {\n const lines: string[] = [\n \"// AUTO-GENERATED by @variantlab/cli\",\n `// Generated at ${new Date().toISOString()}`,\n \"// DO NOT EDIT BY HAND\",\n \"// Regenerate with: variantlab generate\",\n \"\",\n \"export interface VariantLabExperiments {\",\n ];\n\n for (const exp of config.experiments) {\n if (exp.name) {\n lines.push(` /** ${escapeComment(exp.name)} */`);\n }\n lines.push(` \"${exp.id}\": {`);\n lines.push(` type: \"${exp.type ?? \"render\"}\";`);\n lines.push(` variants: ${variantUnion(exp)};`);\n lines.push(` default: \"${exp.default}\";`);\n\n if (exp.type === \"value\") {\n lines.push(` value: ${valueUnion(exp)};`);\n }\n\n lines.push(\" };\");\n }\n\n lines.push(\"}\");\n lines.push(\"\");\n lines.push('declare module \"@variantlab/core\" {');\n lines.push(\" interface VariantLabRegistry extends VariantLabExperiments {}\");\n lines.push(\"}\");\n lines.push(\"\");\n lines.push(\"export type ExperimentId = keyof VariantLabExperiments;\");\n lines.push(\"\");\n lines.push(\"export type VariantId<T extends ExperimentId> =\");\n lines.push(' VariantLabExperiments[T][\"variants\"];');\n lines.push(\"\");\n lines.push(\"export type VariantValueType<T extends ExperimentId> =\");\n lines.push(\" VariantLabExperiments[T] extends { value: infer V } ? V : never;\");\n lines.push(\"\");\n\n return lines.join(\"\\n\");\n}\n\nfunction variantUnion(exp: Experiment): string {\n return exp.variants.map((v) => `\"${escapeStr(v.id)}\"`).join(\" | \");\n}\n\nfunction valueUnion(exp: Experiment): string {\n const values = exp.variants.map((v) => v.value).filter((v) => v !== undefined);\n\n if (values.length === 0) return \"never\";\n\n return values\n .map((v) => {\n if (typeof v === \"string\") return `\"${escapeStr(v)}\"`;\n if (typeof v === \"number\" || typeof v === \"boolean\") return String(v);\n return \"unknown\";\n })\n .join(\" | \");\n}\n\nfunction escapeStr(s: string): string {\n return s.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"');\n}\n\nfunction escapeComment(s: string): string {\n return s.replace(/\\*\\//g, \"* /\");\n}\n","import { resolve } from \"node:path\";\nimport { fileExists, readTextFile, writeTextFile } from \"../utils/file.js\";\nimport * as print from \"../utils/printer.js\";\n\nconst STARTER_CONFIG = `{\n \"$schema\": \"https://variantlab.dev/schemas/experiments.schema.json\",\n \"version\": 1,\n \"experiments\": [\n {\n \"id\": \"welcome-message\",\n \"name\": \"Welcome message copy\",\n \"type\": \"value\",\n \"default\": \"hello\",\n \"variants\": [\n { \"id\": \"hello\", \"label\": \"Hello\", \"value\": \"Hello, welcome!\" },\n { \"id\": \"hey\", \"label\": \"Hey\", \"value\": \"Hey there, glad you're here!\" }\n ]\n },\n {\n \"id\": \"hero-layout\",\n \"name\": \"Hero section layout\",\n \"type\": \"render\",\n \"default\": \"centered\",\n \"variants\": [\n { \"id\": \"centered\", \"label\": \"Centered\" },\n { \"id\": \"split\", \"label\": \"Split view\" }\n ]\n }\n ]\n}\n`;\n\nexport interface InitOptions {\n readonly force?: boolean | undefined;\n readonly verbose?: boolean | undefined;\n}\n\nexport async function init(options: InitOptions = {}): Promise<number> {\n const configPath = resolve(\"experiments.json\");\n\n if (!options.force && (await fileExists(configPath))) {\n print.error(\"experiments.json already exists. Use --force to overwrite.\");\n return 1;\n }\n\n try {\n await writeTextFile(configPath, STARTER_CONFIG);\n print.success(\"Created experiments.json\");\n } catch (err) {\n print.error(`Failed to write experiments.json: ${(err as Error).message}`);\n return 3;\n }\n\n // Try to add generate script to package.json if it exists\n const pkgPath = resolve(\"package.json\");\n if (await fileExists(pkgPath)) {\n try {\n const raw = await readTextFile(pkgPath);\n const pkg = JSON.parse(raw) as Record<string, unknown>;\n const scripts = (pkg.scripts ?? {}) as Record<string, string>;\n if (!scripts[\"variantlab:generate\"]) {\n scripts[\"variantlab:generate\"] = \"variantlab generate\";\n pkg.scripts = scripts;\n await writeTextFile(pkgPath, `${JSON.stringify(pkg, null, 2)}\\n`);\n print.success('Added \"variantlab:generate\" script to package.json');\n }\n } catch {\n print.verbose(\"Could not update package.json (non-fatal)\", true);\n }\n }\n\n console.log(\"\");\n print.info(\"Next steps:\");\n console.log(\" 1. Edit experiments.json to define your experiments\");\n console.log(\" 2. Run: variantlab generate\");\n console.log(\" 3. Import the generated types in your app\");\n console.log(\"\");\n\n return 0;\n}\n","import { resolve } from \"node:path\";\nimport { ConfigValidationError, validateConfig } from \"@variantlab/core\";\nimport { fileExists, readTextFile } from \"../utils/file.js\";\nimport * as print from \"../utils/printer.js\";\nimport { color } from \"../utils/printer.js\";\n\nexport interface ValidateOptions {\n readonly verbose?: boolean | undefined;\n}\n\nexport async function validate(\n configPathArg?: string,\n options: ValidateOptions = {},\n): Promise<number> {\n const configPath = resolve(configPathArg ?? \"experiments.json\");\n\n if (!(await fileExists(configPath))) {\n print.error(`Config not found: ${configPath}`);\n return 1;\n }\n\n let raw: string;\n try {\n raw = await readTextFile(configPath);\n } catch (err) {\n print.error(`Failed to read config: ${(err as Error).message}`);\n return 3;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (err) {\n print.error(`Invalid JSON in ${configPath}`);\n print.error((err as Error).message);\n return 2;\n }\n\n try {\n const config = validateConfig(parsed);\n print.success(`Valid config: ${config.experiments.length} experiment(s)`);\n\n if (options.verbose) {\n for (const exp of config.experiments) {\n console.log(\n ` ${color.cyan(exp.id)} — ${exp.variants.length} variants, ` +\n `assignment: ${exp.assignment ?? \"default\"}, ` +\n `status: ${exp.status ?? \"active\"}`,\n );\n }\n }\n\n return 0;\n } catch (err) {\n if (err instanceof ConfigValidationError) {\n print.error(`Validation failed with ${err.issues.length} issue(s):`);\n console.log(\"\");\n for (const issue of err.issues) {\n console.log(` ${color.red(issue.code)} at ${color.yellow(issue.path)}`);\n console.log(` ${issue.message}`);\n }\n console.log(\"\");\n return 2;\n }\n print.error(`Unexpected error: ${(err as Error).message}`);\n return 2;\n }\n}\n","/**\n * Minimal argument parser. Supports:\n * --flag → { flag: true }\n * --flag=value → { flag: \"value\" }\n * --flag value → { flag: \"value\" }\n * --no-flag → { flag: false }\n * positional → positionals[]\n *\n * Zero dependencies.\n */\n\nexport interface ParsedArgs {\n readonly flags: Readonly<Record<string, string | boolean>>;\n readonly positionals: readonly string[];\n readonly command: string | undefined;\n}\n\nexport function parseArgs(argv: readonly string[]): ParsedArgs {\n const flags: Record<string, string | boolean> = {};\n const positionals: string[] = [];\n let command: string | undefined;\n\n let i = 0;\n while (i < argv.length) {\n const arg = argv[i] as string;\n\n if (arg === \"--\") {\n positionals.push(...argv.slice(i + 1));\n break;\n }\n\n if (arg.startsWith(\"--\")) {\n const eqIdx = arg.indexOf(\"=\");\n if (eqIdx !== -1) {\n const key = camelCase(arg.slice(2, eqIdx));\n flags[key] = arg.slice(eqIdx + 1);\n } else {\n const raw = arg.slice(2);\n if (raw.startsWith(\"no-\")) {\n flags[camelCase(raw.slice(3))] = false;\n } else {\n const key = camelCase(raw);\n const next = argv[i + 1];\n if (next !== undefined && !next.startsWith(\"-\")) {\n flags[key] = next;\n i++;\n } else {\n flags[key] = true;\n }\n }\n }\n } else if (arg.startsWith(\"-\") && arg.length === 2) {\n const key = arg[1] as string;\n const next = argv[i + 1];\n if (next !== undefined && !next.startsWith(\"-\")) {\n flags[key] = next;\n i++;\n } else {\n flags[key] = true;\n }\n } else if (command === undefined && !arg.startsWith(\"-\")) {\n command = arg;\n } else {\n positionals.push(arg);\n }\n\n i++;\n }\n\n return { flags, positionals, command };\n}\n\nfunction camelCase(s: string): string {\n return s.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());\n}\n","export const VERSION = \"0.0.0\";\n\nexport { type EvalOptions, evalCommand } from \"./commands/eval.js\";\nexport { type GenerateOptions, generate } from \"./commands/generate.js\";\nexport { type InitOptions, init } from \"./commands/init.js\";\nexport { type ValidateOptions, validate } from \"./commands/validate.js\";\nexport { type ParsedArgs, parseArgs } from \"./utils/arg-parser.js\";\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
interface EvalOptions {
|
|
2
|
+
readonly experiment?: string | undefined;
|
|
3
|
+
readonly context?: string | undefined;
|
|
4
|
+
readonly contextFile?: string | undefined;
|
|
5
|
+
readonly verbose?: boolean | undefined;
|
|
6
|
+
}
|
|
7
|
+
declare function evalCommand(configPathArg?: string, options?: EvalOptions): Promise<number>;
|
|
8
|
+
|
|
9
|
+
interface GenerateOptions {
|
|
10
|
+
readonly config?: string | undefined;
|
|
11
|
+
readonly out?: string | undefined;
|
|
12
|
+
readonly watch?: boolean | undefined;
|
|
13
|
+
readonly verbose?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
declare function generate(options?: GenerateOptions): Promise<number>;
|
|
16
|
+
|
|
17
|
+
interface InitOptions {
|
|
18
|
+
readonly force?: boolean | undefined;
|
|
19
|
+
readonly verbose?: boolean | undefined;
|
|
20
|
+
}
|
|
21
|
+
declare function init(options?: InitOptions): Promise<number>;
|
|
22
|
+
|
|
23
|
+
interface ValidateOptions {
|
|
24
|
+
readonly verbose?: boolean | undefined;
|
|
25
|
+
}
|
|
26
|
+
declare function validate(configPathArg?: string, options?: ValidateOptions): Promise<number>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Minimal argument parser. Supports:
|
|
30
|
+
* --flag → { flag: true }
|
|
31
|
+
* --flag=value → { flag: "value" }
|
|
32
|
+
* --flag value → { flag: "value" }
|
|
33
|
+
* --no-flag → { flag: false }
|
|
34
|
+
* positional → positionals[]
|
|
35
|
+
*
|
|
36
|
+
* Zero dependencies.
|
|
37
|
+
*/
|
|
38
|
+
interface ParsedArgs {
|
|
39
|
+
readonly flags: Readonly<Record<string, string | boolean>>;
|
|
40
|
+
readonly positionals: readonly string[];
|
|
41
|
+
readonly command: string | undefined;
|
|
42
|
+
}
|
|
43
|
+
declare function parseArgs(argv: readonly string[]): ParsedArgs;
|
|
44
|
+
|
|
45
|
+
declare const VERSION = "0.0.0";
|
|
46
|
+
|
|
47
|
+
export { type EvalOptions, type GenerateOptions, type InitOptions, type ParsedArgs, VERSION, type ValidateOptions, evalCommand, generate, init, parseArgs, validate };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
interface EvalOptions {
|
|
2
|
+
readonly experiment?: string | undefined;
|
|
3
|
+
readonly context?: string | undefined;
|
|
4
|
+
readonly contextFile?: string | undefined;
|
|
5
|
+
readonly verbose?: boolean | undefined;
|
|
6
|
+
}
|
|
7
|
+
declare function evalCommand(configPathArg?: string, options?: EvalOptions): Promise<number>;
|
|
8
|
+
|
|
9
|
+
interface GenerateOptions {
|
|
10
|
+
readonly config?: string | undefined;
|
|
11
|
+
readonly out?: string | undefined;
|
|
12
|
+
readonly watch?: boolean | undefined;
|
|
13
|
+
readonly verbose?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
declare function generate(options?: GenerateOptions): Promise<number>;
|
|
16
|
+
|
|
17
|
+
interface InitOptions {
|
|
18
|
+
readonly force?: boolean | undefined;
|
|
19
|
+
readonly verbose?: boolean | undefined;
|
|
20
|
+
}
|
|
21
|
+
declare function init(options?: InitOptions): Promise<number>;
|
|
22
|
+
|
|
23
|
+
interface ValidateOptions {
|
|
24
|
+
readonly verbose?: boolean | undefined;
|
|
25
|
+
}
|
|
26
|
+
declare function validate(configPathArg?: string, options?: ValidateOptions): Promise<number>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Minimal argument parser. Supports:
|
|
30
|
+
* --flag → { flag: true }
|
|
31
|
+
* --flag=value → { flag: "value" }
|
|
32
|
+
* --flag value → { flag: "value" }
|
|
33
|
+
* --no-flag → { flag: false }
|
|
34
|
+
* positional → positionals[]
|
|
35
|
+
*
|
|
36
|
+
* Zero dependencies.
|
|
37
|
+
*/
|
|
38
|
+
interface ParsedArgs {
|
|
39
|
+
readonly flags: Readonly<Record<string, string | boolean>>;
|
|
40
|
+
readonly positionals: readonly string[];
|
|
41
|
+
readonly command: string | undefined;
|
|
42
|
+
}
|
|
43
|
+
declare function parseArgs(argv: readonly string[]): ParsedArgs;
|
|
44
|
+
|
|
45
|
+
declare const VERSION = "0.0.0";
|
|
46
|
+
|
|
47
|
+
export { type EvalOptions, type GenerateOptions, type InitOptions, type ParsedArgs, VERSION, type ValidateOptions, evalCommand, generate, init, parseArgs, validate };
|