Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const main = command.main;
export const runConfigCommand = command.run;
export const meta = command.meta;

/** @param {{ values: { json?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json">, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runConfig({ values, positionals, context }) {
const [subcommand, extra] = positionals;
if (subcommand !== "explain" || extra) throw new CliError(usageText());
Expand Down
18 changes: 9 additions & 9 deletions commands/d1.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export const runD1Command = command.run;
export const meta = command.meta;

/**
* @typedef {object} D1Flags
* @property {string} [sql]
* @property {string} [file]
* @property {string} [mode]
* @property {string} [params]
* @property {string} [dir]
* @property {string} [env]
* @property {boolean} [yes]
* @property {boolean} [json]
* @typedef {import("../lib/command.js").PresetFlags<"ns" | "control" | "json"> & {
* sql?: string,
* file?: string,
* mode?: string,
* params?: string,
* dir?: string,
* env?: string,
* yes?: boolean,
* }} D1Flags
*/

/** @param {{ values: D1Flags, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
Expand Down
2 changes: 1 addition & 1 deletion commands/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const main = command.main;
export const runDeleteCommand = command.run;
export const meta = command.meta;

/** @param {{ values: { worker?: string, version?: string, "dry-run"?: boolean, yes?: boolean, json?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json"> & { worker?: string, version?: string, "dry-run"?: boolean, yes?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runDelete({ values, positionals, context }) {
const { stdout, stderr, stdin } = context;

Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const meta = command.meta;
* @typedef {import("../lib/command.js").CommandContext & { execFile: typeof execFileSync }} DeployContext
*/

/** @param {{ values: { env?: string, verbose?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control"> & { env?: string, verbose?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runDeploy({ values, positionals, context: baseContext }) {
const context = /** @type {DeployContext} */ (baseContext);
const { env, stdout, stderr, cwd, execFile } = context;
Expand Down
2 changes: 1 addition & 1 deletion commands/doctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const meta = command.meta;
* @typedef {import("../lib/command.js").CommandContext & { execFile: typeof execFileSync }} DoctorContext
*/

/** @param {{ values: { ns?: string, "control-url"?: string, token?: string, "no-token-store"?: boolean, json?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json">, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runDoctor({ values, positionals, context: baseContext }) {
if (positionals.length > 0) throw new CliError(usageText());

Expand Down
2 changes: 1 addition & 1 deletion commands/r2.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const meta = command.meta;

/**
* @param {{
* values: { prefix?: string, delimiter?: string, cursor?: string, limit?: string, out?: string, yes?: boolean, json?: boolean },
* values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json"> & { prefix?: string, delimiter?: string, cursor?: string, limit?: string, out?: string, yes?: boolean },
* positionals: string[],
* context: import("../lib/command.js").CommandContext,
* }} arg
Expand Down
2 changes: 1 addition & 1 deletion commands/secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const meta = command.meta;
* @property {PromoteWarning[]} [warnings]
*/

/** @param {{ values: { worker?: string, scope?: string, yes?: boolean, json?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json"> & { worker?: string, scope?: string, yes?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runSecret({ values, positionals, context }) {
const { stdout, stderr, stdin } = context;

Expand Down
2 changes: 1 addition & 1 deletion commands/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const meta = command.meta;
* }} TailContext
*/

/** @param {{ values: { raw?: boolean, since?: string, "max-reconnects"?: string, ns?: string, "control-url"?: string, token?: string, "no-token-store"?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control"> & { raw?: boolean, since?: string, "max-reconnects"?: string }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runTail({ values, positionals, context: baseContext }) {
const context = /** @type {TailContext} */ (baseContext);
const { stdout, stderr, transport, sleepFn, now } = context;
Expand Down
2 changes: 1 addition & 1 deletion commands/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const runTokenCommand = command.run;
export const meta = command.meta;

/**
* @typedef {{ label?: string, default?: boolean, ns?: string, "control-url"?: string, json?: boolean }} TokenValues
* @typedef {import("../lib/command.js").PresetFlags<"endpoint"> & { label?: string, default?: boolean, ns?: string, json?: boolean }} TokenValues
*/

/** @param {{ values: TokenValues, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
Expand Down
2 changes: 1 addition & 1 deletion commands/whoami.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const main = command.main;
export const runWhoamiCommand = command.run;
export const meta = command.meta;

/** @param {{ values: { json?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json">, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runWhoami({ values, positionals, context }) {
if (positionals.length > 0) throw new CliError(usageText());

Expand Down
15 changes: 9 additions & 6 deletions commands/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ export const meta = command.meta;
// Re-exported for the existing test import surface; logic lives in lib/.
export { formatWorkersList };

/** @param {{ positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runWorkers({ positionals, context }) {
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json">, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runWorkers({ values, positionals, context }) {
const ns = context.resolveNamespace();
if (positionals.length > 0) throw new CliError(usageText());
if (!ns) throw new CliError(usageText());
await printWorkersList(context);
await printWorkersList(context, values.json === true);
}

/** @param {import("../lib/command.js").CommandContext} context */
async function printWorkersList(context) {
/**
* @param {import("../lib/command.js").CommandContext} context
* @param {boolean} json
*/
async function printWorkersList(context, json) {
const { headers } = context.resolveControl();
const body = /** @type {{ workers?: import("../lib/workers-format.js").WorkerSummary[] }} */ (
await context.fetchJson(context.nsUrl("workers"), { headers }, "list workers")
);
writeResult(context.values.json === true, body, () => formatWorkersList(body), context.stdout);
writeResult(json, body, () => formatWorkersList(body), context.stdout);
}

function usageText() {
Expand Down
2 changes: 1 addition & 1 deletion commands/workflows.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const main = command.main;
export const runWorkflowsCommand = command.run;
export const meta = command.meta;

/** @param {{ values: { limit?: string, cursor?: string, "include-steps"?: boolean, "step-limit"?: string, yes?: boolean, json?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
/** @param {{ values: import("../lib/command.js").PresetFlags<"ns" | "control" | "json"> & { limit?: string, cursor?: string, "include-steps"?: boolean, "step-limit"?: string, yes?: boolean }, positionals: string[], context: import("../lib/command.js").CommandContext }} arg */
async function runWorkflows({ values, positionals, context }) {
const { stdout, stderr, stdin } = context;

Expand Down
32 changes: 31 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,44 @@ import { resolveControlContext, resolveNamespace, warnIfInsecureControlUrl } fro
* Flag-preset names accepted in a command's `options` list; each expands to
* the matching shared option specs:
* "ns" -> --ns
* "control" -> --control-url, --token
* "control" -> --control-url, --token, --no-token-store
* "endpoint"-> --control-url
* "env" -> --env
* "json" -> --json
* "yes" -> --yes
* "help" -> -h, --help
* @typedef {string} OptionPreset
*/

/**
* @template U
* @typedef {(U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never} UnionToIntersection
*/

/**
* The parsed `values` each option preset contributes, keyed by preset name.
* Keep in sync with CLI_OPTION_PRESETS in common.js: a command names its presets
* via PresetFlags<...> instead of re-typing their flags, so the schema and the
* handler's `values` type can't drift. A preset's value keys need not match its
* name — "control" contributes "control-url", "token", and "no-token-store".
* @typedef {object} PresetValueMap
* @property {{ ns?: string }} ns
* @property {{ env?: string }} env
* @property {{ "control-url"?: string, token?: string, "no-token-store"?: boolean }} control
* @property {{ "control-url"?: string }} endpoint
* @property {{ json?: boolean }} json
* @property {{ yes?: boolean }} yes
* @property {{ help?: boolean }} help
*/

/**
* The `values` contributed by a set of option presets, e.g.
* `PresetFlags<"ns" | "control" | "json">`. Intersect with a command's own
* flags: `PresetFlags<"ns"> & { raw?: boolean }`.
* @template {keyof PresetValueMap} P
* @typedef {UnionToIntersection<PresetValueMap[P]>} PresetFlags
*/

/**
* The object handed to every command's run body. Framework members are typed,
* so a misspelled access (e.g. `context.fetchJson` typed as `fetchJsonn`) is a
Expand Down