padrone 1.4.0 → 1.6.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/CHANGELOG.md +115 -0
- package/README.md +108 -283
- package/dist/args-Cnq0nwSM.mjs +272 -0
- package/dist/args-Cnq0nwSM.mjs.map +1 -0
- package/dist/codegen/index.d.mts +28 -3
- package/dist/codegen/index.d.mts.map +1 -1
- package/dist/codegen/index.mjs +169 -19
- package/dist/codegen/index.mjs.map +1 -1
- package/dist/commands-B_gufyR9.mjs +514 -0
- package/dist/commands-B_gufyR9.mjs.map +1 -0
- package/dist/{completion.mjs → completion-BEuflbDO.mjs} +86 -108
- package/dist/completion-BEuflbDO.mjs.map +1 -0
- package/dist/docs/index.d.mts +22 -2
- package/dist/docs/index.d.mts.map +1 -1
- package/dist/docs/index.mjs +92 -7
- package/dist/docs/index.mjs.map +1 -1
- package/dist/errors-CL63UOzt.mjs +137 -0
- package/dist/errors-CL63UOzt.mjs.map +1 -0
- package/dist/{formatter-ClUK5hcQ.d.mts → formatter-DrvhDMrq.d.mts} +35 -6
- package/dist/formatter-DrvhDMrq.d.mts.map +1 -0
- package/dist/help-B5Kk83of.mjs +849 -0
- package/dist/help-B5Kk83of.mjs.map +1 -0
- package/dist/index-BaU3X6dY.d.mts +1178 -0
- package/dist/index-BaU3X6dY.d.mts.map +1 -0
- package/dist/index.d.mts +763 -36
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3608 -1534
- package/dist/index.mjs.map +1 -1
- package/dist/mcp-BM-d0nZi.mjs +377 -0
- package/dist/mcp-BM-d0nZi.mjs.map +1 -0
- package/dist/serve-Bk0JUlCj.mjs +402 -0
- package/dist/serve-Bk0JUlCj.mjs.map +1 -0
- package/dist/stream-DC4H8YTx.mjs +77 -0
- package/dist/stream-DC4H8YTx.mjs.map +1 -0
- package/dist/test.d.mts +5 -8
- package/dist/test.d.mts.map +1 -1
- package/dist/test.mjs +5 -27
- package/dist/test.mjs.map +1 -1
- package/dist/{update-check-EbNDkzyV.mjs → update-check-CZ2VqjnV.mjs} +16 -17
- package/dist/update-check-CZ2VqjnV.mjs.map +1 -0
- package/dist/zod.d.mts +32 -0
- package/dist/zod.d.mts.map +1 -0
- package/dist/zod.mjs +50 -0
- package/dist/zod.mjs.map +1 -0
- package/package.json +20 -9
- package/src/cli/completions.ts +14 -11
- package/src/cli/docs.ts +13 -16
- package/src/cli/doctor.ts +213 -24
- package/src/cli/index.ts +28 -82
- package/src/cli/init.ts +12 -10
- package/src/cli/link.ts +22 -18
- package/src/cli/wrap.ts +14 -11
- package/src/codegen/discovery.ts +80 -28
- package/src/codegen/index.ts +2 -1
- package/src/codegen/parsers/bash.ts +179 -0
- package/src/codegen/schema-to-code.ts +2 -1
- package/src/core/args.ts +296 -0
- package/src/core/commands.ts +373 -0
- package/src/core/create.ts +268 -0
- package/src/{runtime.ts → core/default-runtime.ts} +70 -135
- package/src/{errors.ts → core/errors.ts} +22 -0
- package/src/core/exec.ts +259 -0
- package/src/core/interceptors.ts +302 -0
- package/src/{parse.ts → core/parse.ts} +36 -89
- package/src/core/program-methods.ts +301 -0
- package/src/core/results.ts +229 -0
- package/src/core/runtime.ts +246 -0
- package/src/core/validate.ts +247 -0
- package/src/docs/index.ts +124 -11
- package/src/extension/auto-output.ts +95 -0
- package/src/extension/color.ts +38 -0
- package/src/extension/completion.ts +49 -0
- package/src/extension/config.ts +262 -0
- package/src/extension/env.ts +101 -0
- package/src/extension/help.ts +192 -0
- package/src/extension/index.ts +43 -0
- package/src/extension/ink.ts +93 -0
- package/src/extension/interactive.ts +106 -0
- package/src/extension/logger.ts +214 -0
- package/src/extension/man.ts +51 -0
- package/src/extension/mcp.ts +52 -0
- package/src/extension/progress-renderer.ts +338 -0
- package/src/extension/progress.ts +299 -0
- package/src/extension/repl.ts +94 -0
- package/src/extension/serve.ts +48 -0
- package/src/extension/signal.ts +87 -0
- package/src/extension/stdin.ts +62 -0
- package/src/extension/suggestions.ts +114 -0
- package/src/extension/timing.ts +81 -0
- package/src/extension/tracing.ts +175 -0
- package/src/extension/update-check.ts +77 -0
- package/src/extension/utils.ts +51 -0
- package/src/extension/version.ts +63 -0
- package/src/{completion.ts → feature/completion.ts} +130 -57
- package/src/{interactive.ts → feature/interactive.ts} +47 -6
- package/src/feature/mcp.ts +387 -0
- package/src/{repl-loop.ts → feature/repl-loop.ts} +26 -16
- package/src/feature/serve.ts +438 -0
- package/src/feature/test.ts +262 -0
- package/src/{update-check.ts → feature/update-check.ts} +16 -16
- package/src/{wrap.ts → feature/wrap.ts} +27 -27
- package/src/index.ts +120 -11
- package/src/output/colorizer.ts +154 -0
- package/src/{formatter.ts → output/formatter.ts} +281 -135
- package/src/{help.ts → output/help.ts} +62 -15
- package/src/{zod.d.ts → schema/zod.d.ts} +1 -1
- package/src/schema/zod.ts +50 -0
- package/src/test.ts +2 -285
- package/src/types/args-meta.ts +151 -0
- package/src/types/builder.ts +697 -0
- package/src/types/command.ts +157 -0
- package/src/types/index.ts +59 -0
- package/src/types/interceptor.ts +296 -0
- package/src/types/preferences.ts +83 -0
- package/src/types/result.ts +71 -0
- package/src/types/schema.ts +19 -0
- package/src/util/dotenv.ts +244 -0
- package/src/{shell-utils.ts → util/shell-utils.ts} +26 -9
- package/src/util/stream.ts +101 -0
- package/src/{type-helpers.ts → util/type-helpers.ts} +23 -16
- package/src/{type-utils.ts → util/type-utils.ts} +99 -37
- package/src/util/utils.ts +51 -0
- package/src/zod.ts +1 -0
- package/dist/args-CVDbyyzG.mjs +0 -199
- package/dist/args-CVDbyyzG.mjs.map +0 -1
- package/dist/chunk-y_GBKt04.mjs +0 -5
- package/dist/completion.d.mts +0 -64
- package/dist/completion.d.mts.map +0 -1
- package/dist/completion.mjs.map +0 -1
- package/dist/formatter-ClUK5hcQ.d.mts.map +0 -1
- package/dist/help-CcBe91bV.mjs +0 -1254
- package/dist/help-CcBe91bV.mjs.map +0 -1
- package/dist/types-DjIdJN5G.d.mts +0 -1059
- package/dist/types-DjIdJN5G.d.mts.map +0 -1
- package/dist/update-check-EbNDkzyV.mjs.map +0 -1
- package/src/args.ts +0 -461
- package/src/colorizer.ts +0 -41
- package/src/command-utils.ts +0 -532
- package/src/create.ts +0 -1477
- package/src/types.ts +0 -1109
- package/src/utils.ts +0 -140
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { resolveCommand, suggestSimilar } from '../core/commands.ts';
|
|
2
|
+
import { RoutingError } from '../core/errors.ts';
|
|
3
|
+
import { defineInterceptor } from '../core/interceptors.ts';
|
|
4
|
+
import { thenMaybe } from '../core/results.ts';
|
|
5
|
+
import { getKnownOptionNames } from '../core/validate.ts';
|
|
6
|
+
import type { AnyPadroneBuilder, AnyPadroneCommand, CommandTypesBase } from '../types/index.ts';
|
|
7
|
+
|
|
8
|
+
function formatSuggestions(names: string[], prefix = ''): string {
|
|
9
|
+
if (names.length === 0) return '';
|
|
10
|
+
const quoted = names.map((n) => `"${prefix}${n}"`);
|
|
11
|
+
if (quoted.length === 1) return `Did you mean ${quoted[0]}?`;
|
|
12
|
+
return `Did you mean ${quoted.slice(0, -1).join(', ')} or ${quoted.at(-1)}?`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function findSourceCommand(commandPath: string | undefined, root: AnyPadroneCommand): AnyPadroneCommand {
|
|
16
|
+
if (!commandPath || commandPath === root.name || commandPath === root.path) return root;
|
|
17
|
+
const parts = commandPath.split(' ');
|
|
18
|
+
let current = root;
|
|
19
|
+
for (const part of parts) {
|
|
20
|
+
const found = current.commands?.find((c) => {
|
|
21
|
+
resolveCommand(c);
|
|
22
|
+
return c.name === part || c.aliases?.includes(part);
|
|
23
|
+
});
|
|
24
|
+
if (found) current = found;
|
|
25
|
+
else break;
|
|
26
|
+
}
|
|
27
|
+
return current;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function enrichRoutingError(err: unknown, rootCommand: AnyPadroneCommand): unknown {
|
|
31
|
+
if (!(err instanceof RoutingError)) return err;
|
|
32
|
+
|
|
33
|
+
const unknownMatch = err.message.match(/^Unknown command: (\S+)/);
|
|
34
|
+
const unexpectedMatch = err.message.match(/^Unexpected arguments for '[^']+': (\S+)/);
|
|
35
|
+
const term = unknownMatch?.[1] ?? unexpectedMatch?.[1];
|
|
36
|
+
if (!term) return err;
|
|
37
|
+
|
|
38
|
+
const sourceCmd = findSourceCommand(err.command, rootCommand);
|
|
39
|
+
|
|
40
|
+
const candidateNames: string[] = [];
|
|
41
|
+
if (sourceCmd.commands) {
|
|
42
|
+
for (const cmd of sourceCmd.commands) {
|
|
43
|
+
resolveCommand(cmd);
|
|
44
|
+
if (!cmd.hidden) {
|
|
45
|
+
candidateNames.push(cmd.name);
|
|
46
|
+
if (cmd.aliases) candidateNames.push(...cmd.aliases);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const similar = suggestSimilar(term, candidateNames);
|
|
52
|
+
const suggestionText = formatSuggestions(similar);
|
|
53
|
+
if (!suggestionText) return err;
|
|
54
|
+
|
|
55
|
+
const suggestions = [suggestionText];
|
|
56
|
+
const enrichedMsg = `${err.message}\n\n ${suggestionText}`;
|
|
57
|
+
return new RoutingError(enrichedMsg, { suggestions, command: err.command });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function enrichIssuesWithSuggestions(
|
|
61
|
+
issues: readonly { path?: readonly unknown[]; message: string }[],
|
|
62
|
+
knownOptions: () => string[],
|
|
63
|
+
): typeof issues {
|
|
64
|
+
return issues.map((i: any) => {
|
|
65
|
+
// Handle direct unknown option detection (from checkUnknownArgs)
|
|
66
|
+
const unknownMatch = i.message?.match(/^Unknown option: "([^"]+)"$/);
|
|
67
|
+
if (unknownMatch) {
|
|
68
|
+
const similar = suggestSimilar(unknownMatch[1], knownOptions());
|
|
69
|
+
if (similar.length) {
|
|
70
|
+
const hint = formatSuggestions(similar, '--');
|
|
71
|
+
return { ...i, message: `${i.message} ${hint}` };
|
|
72
|
+
}
|
|
73
|
+
return i;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Handle Zod strict schema errors (Unrecognized key(s) in object: "foo")
|
|
77
|
+
const keys: string[] | undefined = i.keys ?? i.message?.match(/[Uu]nrecognized key(?:s)?[^"]*"([^"]+)"/)?.slice(1);
|
|
78
|
+
if (!keys?.length) return i;
|
|
79
|
+
const hints = keys.flatMap((k: string) => {
|
|
80
|
+
const similar = suggestSimilar(k, knownOptions());
|
|
81
|
+
return similar.length ? [formatSuggestions(similar, '--')] : [];
|
|
82
|
+
});
|
|
83
|
+
if (!hints.length) return i;
|
|
84
|
+
return { ...i, message: `${i.message} ${hints.join(' ')}` };
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const suggestionsInterceptor = defineInterceptor({ id: 'padrone:suggestions', name: 'padrone:suggestions', order: -500 }, () => ({
|
|
89
|
+
parse(ctx, next) {
|
|
90
|
+
try {
|
|
91
|
+
const result = next();
|
|
92
|
+
if (result instanceof Promise) {
|
|
93
|
+
return result.catch((err: unknown) => {
|
|
94
|
+
throw enrichRoutingError(err, ctx.command);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
} catch (err) {
|
|
99
|
+
throw enrichRoutingError(err, ctx.command);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
validate(ctx, next) {
|
|
103
|
+
const result = next();
|
|
104
|
+
return thenMaybe(result, (v) => {
|
|
105
|
+
if (!v.argsResult?.issues?.length) return v;
|
|
106
|
+
const enriched = enrichIssuesWithSuggestions(v.argsResult.issues, () => getKnownOptionNames(ctx.command));
|
|
107
|
+
return { ...v, argsResult: { ...v.argsResult, issues: enriched } } as typeof v;
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
}));
|
|
111
|
+
|
|
112
|
+
export function padroneSuggestions(): <T extends CommandTypesBase>(builder: T) => T {
|
|
113
|
+
return ((builder: AnyPadroneBuilder) => builder.intercept(suggestionsInterceptor)) as any;
|
|
114
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { defineInterceptor } from '../core/interceptors.ts';
|
|
2
|
+
import { thenMaybe } from '../core/results.ts';
|
|
3
|
+
import type { AnyPadroneBuilder, CommandTypesBase } from '../types/index.ts';
|
|
4
|
+
|
|
5
|
+
// ── Helpers ─────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
function formatDuration(ms: number): string {
|
|
8
|
+
if (ms < 1000) return `${ms.toFixed(0)}ms`;
|
|
9
|
+
if (ms < 60_000) return `${(ms / 1000).toFixed(2)}s`;
|
|
10
|
+
const mins = Math.floor(ms / 60_000);
|
|
11
|
+
const secs = ((ms % 60_000) / 1000).toFixed(2);
|
|
12
|
+
return `${mins}m ${secs}s`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ── Interceptor ─────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
const timingMeta = { id: 'padrone:timing', name: 'padrone:timing', order: -1002 } as const;
|
|
18
|
+
|
|
19
|
+
function createTimingInterceptor(enabledByDefault: boolean) {
|
|
20
|
+
return defineInterceptor(timingMeta, () => {
|
|
21
|
+
let enabled = enabledByDefault;
|
|
22
|
+
let startTime = 0;
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
parse(_ctx, next) {
|
|
26
|
+
return thenMaybe(next(), (res) => {
|
|
27
|
+
if ('timing' in res.rawArgs) {
|
|
28
|
+
enabled = res.rawArgs.timing !== false;
|
|
29
|
+
delete res.rawArgs.timing;
|
|
30
|
+
}
|
|
31
|
+
if ('time' in res.rawArgs) {
|
|
32
|
+
enabled = res.rawArgs.time !== false;
|
|
33
|
+
delete res.rawArgs.time;
|
|
34
|
+
}
|
|
35
|
+
return res;
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
start(_ctx, next) {
|
|
39
|
+
startTime = performance.now();
|
|
40
|
+
return next();
|
|
41
|
+
},
|
|
42
|
+
shutdown(ctx, next) {
|
|
43
|
+
return thenMaybe(next(), (res) => {
|
|
44
|
+
if (enabled) {
|
|
45
|
+
const elapsed = performance.now() - startTime;
|
|
46
|
+
ctx.runtime.error(`\nDone in ${formatDuration(elapsed)}`);
|
|
47
|
+
}
|
|
48
|
+
return res;
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ── Extension ───────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
export interface PadroneTimingOptions {
|
|
58
|
+
/** Enable timing by default without requiring `--time` flag. Default: `false`. */
|
|
59
|
+
enabled?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Extension that tracks command execution time.
|
|
64
|
+
*
|
|
65
|
+
* - `--time` / `--timing` → enables timing output
|
|
66
|
+
* - `--no-time` / `--no-timing` → disables timing output
|
|
67
|
+
*
|
|
68
|
+
* Pass `{ enabled: true }` to enable timing by default (can be disabled via `--no-time`).
|
|
69
|
+
*
|
|
70
|
+
* Usage:
|
|
71
|
+
* ```ts
|
|
72
|
+
* // Opt-in via flag
|
|
73
|
+
* createPadrone('my-cli').extend(padroneTiming())
|
|
74
|
+
*
|
|
75
|
+
* // Always on, opt-out via --no-time
|
|
76
|
+
* createPadrone('my-cli').extend(padroneTiming({ enabled: true }))
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export function padroneTiming(options?: PadroneTimingOptions): <T extends CommandTypesBase>(builder: T) => T {
|
|
80
|
+
return ((builder: AnyPadroneBuilder) => builder.intercept(createTimingInterceptor(options?.enabled ?? false))) as any;
|
|
81
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { defineInterceptor } from '#src/core/interceptors.ts';
|
|
2
|
+
import { thenMaybe } from '#src/core/results.ts';
|
|
3
|
+
import type { AnyPadroneBuilder, CommandTypesBase } from '#src/types/index.ts';
|
|
4
|
+
import type { WithInterceptor } from '#src/util/type-utils.ts';
|
|
5
|
+
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Types — minimal OTEL-compatible interfaces so we don't hard-depend on
|
|
8
|
+
// `@opentelemetry/api`. Users pass their real Tracer / Span instances.
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
/** Minimal subset of OTEL `SpanStatusCode`. */
|
|
12
|
+
type SpanStatusCode = 0 | 1 | 2;
|
|
13
|
+
|
|
14
|
+
/** Minimal subset of OTEL `SpanStatus`. */
|
|
15
|
+
type SpanStatus = { code: SpanStatusCode; message?: string };
|
|
16
|
+
|
|
17
|
+
/** Minimal subset of OTEL `Span`. */
|
|
18
|
+
export interface OtelSpan {
|
|
19
|
+
setAttribute(key: string, value: string | number | boolean): this;
|
|
20
|
+
addEvent(name: string, attributes?: Record<string, string | number | boolean>): this;
|
|
21
|
+
setStatus(status: SpanStatus): this;
|
|
22
|
+
recordException(error: unknown): this;
|
|
23
|
+
end(): void;
|
|
24
|
+
spanContext(): { traceId: string; spanId: string };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Minimal subset of OTEL `Tracer`. */
|
|
28
|
+
export interface OtelTracer {
|
|
29
|
+
startSpan(name: string): OtelSpan;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Minimal subset of OTEL `TracerProvider`. */
|
|
33
|
+
export interface OtelTracerProvider {
|
|
34
|
+
getTracer(name: string, version?: string): OtelTracer;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Tracing handle injected into the command context. */
|
|
38
|
+
export type PadroneTracer = {
|
|
39
|
+
/** The underlying OTEL tracer. */
|
|
40
|
+
tracer: OtelTracer;
|
|
41
|
+
/** Root span covering the full command execution. */
|
|
42
|
+
rootSpan: OtelSpan;
|
|
43
|
+
/** Run `fn` inside a child span that is automatically ended when `fn` returns (or rejects). */
|
|
44
|
+
span: <T>(name: string, fn: (span: OtelSpan) => T) => T;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Configuration for the tracing extension. */
|
|
48
|
+
export type PadroneTracingConfig = {
|
|
49
|
+
/** OTEL `TracerProvider`. Required — there is no global fallback. */
|
|
50
|
+
provider: OtelTracerProvider;
|
|
51
|
+
/** Service / tracer name. Defaults to the CLI program name. */
|
|
52
|
+
serviceName?: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Builder/program type after applying `padroneTracing()`. Adds `{ tracing: PadroneTracer }` to the command context. */
|
|
56
|
+
export type WithTracing<T> = WithInterceptor<T, { tracing: PadroneTracer }>;
|
|
57
|
+
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// Interceptor
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
const OTEL_ERROR: SpanStatusCode = 2;
|
|
63
|
+
|
|
64
|
+
type ResolvedTracingConfig = { provider: OtelTracerProvider; serviceName: string | undefined };
|
|
65
|
+
|
|
66
|
+
function tracingInterceptor(config: ResolvedTracingConfig) {
|
|
67
|
+
return defineInterceptor({ id: 'padrone:tracing', name: 'padrone:tracing', order: -1 }, () => {
|
|
68
|
+
let rootSpan: OtelSpan;
|
|
69
|
+
let tracer: OtelTracer;
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
start(ctx, next) {
|
|
73
|
+
tracer = config.provider.getTracer(config.serviceName ?? ctx.command.name);
|
|
74
|
+
return next();
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
execute(ctx, next) {
|
|
78
|
+
rootSpan = tracer.startSpan(`cli ${ctx.command.name}`);
|
|
79
|
+
|
|
80
|
+
const padroneTracer: PadroneTracer = {
|
|
81
|
+
tracer,
|
|
82
|
+
rootSpan,
|
|
83
|
+
span(name, fn) {
|
|
84
|
+
const child = tracer.startSpan(name);
|
|
85
|
+
try {
|
|
86
|
+
const result = fn(child);
|
|
87
|
+
if (result != null && typeof (result as any).then === 'function') {
|
|
88
|
+
return (result as any).then(
|
|
89
|
+
(v: any) => {
|
|
90
|
+
child.end();
|
|
91
|
+
return v;
|
|
92
|
+
},
|
|
93
|
+
(err: unknown) => {
|
|
94
|
+
child.recordException(err);
|
|
95
|
+
child.setStatus({ code: OTEL_ERROR });
|
|
96
|
+
child.end();
|
|
97
|
+
throw err;
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
child.end();
|
|
102
|
+
return result;
|
|
103
|
+
} catch (err) {
|
|
104
|
+
child.recordException(err);
|
|
105
|
+
child.setStatus({ code: OTEL_ERROR });
|
|
106
|
+
child.end();
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
return next({ context: { ...(ctx.context as any), tracing: padroneTracer } });
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
error(ctx, next) {
|
|
116
|
+
rootSpan?.recordException(ctx.error);
|
|
117
|
+
rootSpan?.setStatus({ code: OTEL_ERROR });
|
|
118
|
+
return next();
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
shutdown(_ctx, next) {
|
|
122
|
+
return thenMaybe(next(), (res) => {
|
|
123
|
+
rootSpan?.end();
|
|
124
|
+
return res;
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
// Extension
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Extension that adds OpenTelemetry tracing to command execution.
|
|
137
|
+
*
|
|
138
|
+
* Creates a root span for each command invocation and provides a `PadroneTracer`
|
|
139
|
+
* on the command context for creating child spans in action handlers.
|
|
140
|
+
*
|
|
141
|
+
* When used with `padroneLogger()`, the logger automatically emits span events
|
|
142
|
+
* for each log call — no extra configuration needed. The logger detects the
|
|
143
|
+
* tracing context and bridges log output to span events.
|
|
144
|
+
*
|
|
145
|
+
* Uses minimal OTEL-compatible interfaces — pass any `TracerProvider` that
|
|
146
|
+
* implements `getTracer()`. Works with `@opentelemetry/api` or compatible
|
|
147
|
+
* libraries.
|
|
148
|
+
*
|
|
149
|
+
* Provides `{ tracing: PadroneTracer }` on the command context.
|
|
150
|
+
* Access it in action handlers as `ctx.context.tracing`.
|
|
151
|
+
*
|
|
152
|
+
* Usage:
|
|
153
|
+
* ```ts
|
|
154
|
+
* import { trace } from '@opentelemetry/api';
|
|
155
|
+
*
|
|
156
|
+
* createPadrone('my-cli')
|
|
157
|
+
* .extend(padroneTracing({ provider: trace.getTracerProvider() }))
|
|
158
|
+
* .extend(padroneLogger())
|
|
159
|
+
* .command('deploy', (c) =>
|
|
160
|
+
* c.action((_args, ctx) => {
|
|
161
|
+
* ctx.context.logger.info('deploying'); // also emits a span event
|
|
162
|
+
* ctx.context.tracing.span('build', (span) => {
|
|
163
|
+
* span.setAttribute('target', 'production');
|
|
164
|
+
* });
|
|
165
|
+
* })
|
|
166
|
+
* )
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export function padroneTracing<T extends CommandTypesBase>(config: PadroneTracingConfig): (builder: T) => WithTracing<T> {
|
|
170
|
+
const resolved: ResolvedTracingConfig = {
|
|
171
|
+
provider: config.provider,
|
|
172
|
+
serviceName: config.serviceName,
|
|
173
|
+
};
|
|
174
|
+
return ((builder: AnyPadroneBuilder) => builder.intercept(tracingInterceptor(resolved))) as any;
|
|
175
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { thenMaybe } from '#src/core/results.ts';
|
|
2
|
+
import { defineInterceptor } from '../core/interceptors.ts';
|
|
3
|
+
import type { UpdateCheckConfig } from '../feature/update-check.ts';
|
|
4
|
+
import type { AnyPadroneBuilder, CommandTypesBase } from '../types/index.ts';
|
|
5
|
+
import { getVersion } from '../util/utils.ts';
|
|
6
|
+
|
|
7
|
+
// ── Interceptor ─────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
function createUpdateCheckInterceptor(config: UpdateCheckConfig) {
|
|
10
|
+
return defineInterceptor({ id: 'padrone:update-check', name: 'padrone:update-check', order: 1000 }, () => {
|
|
11
|
+
let checkPromise: Promise<(() => void) | undefined> | undefined;
|
|
12
|
+
let suppressed = false;
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
start(ctx, next) {
|
|
16
|
+
const rootCommand = ctx.command;
|
|
17
|
+
const runtime = ctx.runtime;
|
|
18
|
+
|
|
19
|
+
checkPromise = Promise.resolve(getVersion(rootCommand.version)).then((currentVersion) =>
|
|
20
|
+
import('../feature/update-check.ts').then(({ createUpdateChecker }) =>
|
|
21
|
+
createUpdateChecker(rootCommand.name, currentVersion, config, runtime),
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return next();
|
|
26
|
+
},
|
|
27
|
+
parse(_ctx, next) {
|
|
28
|
+
return thenMaybe(next(), (res) => {
|
|
29
|
+
if ('update-check' in res.rawArgs) {
|
|
30
|
+
if (res.rawArgs['update-check'] === false) suppressed = true;
|
|
31
|
+
delete res.rawArgs['update-check'];
|
|
32
|
+
}
|
|
33
|
+
return res;
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
shutdown(_ctx, next) {
|
|
37
|
+
const result = next();
|
|
38
|
+
if (suppressed || !checkPromise) return result;
|
|
39
|
+
|
|
40
|
+
// Try to show notification synchronously if the check already resolved
|
|
41
|
+
let resolved: (() => void) | undefined | null = null;
|
|
42
|
+
checkPromise.then(
|
|
43
|
+
(fn) => {
|
|
44
|
+
resolved = fn;
|
|
45
|
+
},
|
|
46
|
+
() => {
|
|
47
|
+
resolved = undefined;
|
|
48
|
+
},
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (resolved !== null) {
|
|
52
|
+
(resolved as (() => void) | undefined)?.();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return result;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ── Extension ────────────────────────────────────────────────────────────
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Extension that adds background update checking:
|
|
65
|
+
* - Checks for newer versions on npm (or custom registry) in the background
|
|
66
|
+
* - Shows an update notification after command execution
|
|
67
|
+
* - Respects `--no-update-check` flag to suppress
|
|
68
|
+
*
|
|
69
|
+
* Usage:
|
|
70
|
+
* ```ts
|
|
71
|
+
* createPadrone('my-cli')
|
|
72
|
+
* .extend(padroneUpdateCheck({ packageName: 'my-cli' }))
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function padroneUpdateCheck(config: UpdateCheckConfig = {}): <T extends CommandTypesBase>(builder: T) => T {
|
|
76
|
+
return ((builder: AnyPadroneBuilder) => builder.intercept(createUpdateCheckInterceptor(config))) as any;
|
|
77
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { AnyPadroneCommand, PadroneSchema } from '../types/index.ts';
|
|
2
|
+
|
|
3
|
+
type SchemaShape = Record<string, 'string' | 'string[]' | 'boolean'>;
|
|
4
|
+
|
|
5
|
+
type InferPassthroughSchema<T extends SchemaShape> = {
|
|
6
|
+
[K in keyof T]: T[K] extends 'string' ? string : T[K] extends 'string[]' ? string[] : T[K] extends 'boolean' ? boolean : never;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** Minimal Standard Schema that passes through known fields, ignoring unknown ones. */
|
|
10
|
+
export function passthroughSchema<TShape extends SchemaShape>(fields: TShape): PadroneSchema<InferPassthroughSchema<TShape>> {
|
|
11
|
+
return {
|
|
12
|
+
'~standard': {
|
|
13
|
+
version: 1 as const,
|
|
14
|
+
vendor: 'padrone' as const,
|
|
15
|
+
jsonSchema: {
|
|
16
|
+
input: () => ({}),
|
|
17
|
+
output: () => ({}),
|
|
18
|
+
},
|
|
19
|
+
validate: (value) => {
|
|
20
|
+
const input = value && typeof value === 'object' ? (value as Record<string, unknown>) : {};
|
|
21
|
+
const result: Record<string, unknown> = {};
|
|
22
|
+
for (const [name, type] of Object.entries(fields)) {
|
|
23
|
+
const v = input[name];
|
|
24
|
+
if (v === undefined) continue;
|
|
25
|
+
if (type === 'string[]') {
|
|
26
|
+
if (Array.isArray(v)) result[name] = v.map(String);
|
|
27
|
+
else if (typeof v === 'string') result[name] = [v];
|
|
28
|
+
} else if (type === 'string') {
|
|
29
|
+
if (typeof v === 'string') result[name] = v;
|
|
30
|
+
else if (Array.isArray(v) && v.length > 0) result[name] = String(v[0]);
|
|
31
|
+
} else if (type === 'boolean') {
|
|
32
|
+
result[name] = v === true || v === 'true';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return { value: result as InferPassthroughSchema<TShape> };
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Find a command by space-separated name in the command tree. */
|
|
42
|
+
export function findCommandInTree(name: string, rootCommand: AnyPadroneCommand): AnyPadroneCommand | undefined {
|
|
43
|
+
const parts = name.split(' ').filter(Boolean);
|
|
44
|
+
let current = rootCommand;
|
|
45
|
+
for (const part of parts) {
|
|
46
|
+
const found = current.commands?.find((c) => c.name === part || c.aliases?.includes(part));
|
|
47
|
+
if (!found) return undefined;
|
|
48
|
+
current = found;
|
|
49
|
+
}
|
|
50
|
+
return current;
|
|
51
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { thenMaybe } from '#src/core/results.ts';
|
|
2
|
+
import { resolveCommand } from '../core/commands.ts';
|
|
3
|
+
import { defineInterceptor } from '../core/interceptors.ts';
|
|
4
|
+
import type { AnyPadroneBuilder, CommandTypesBase, PadroneCommand } from '../types/index.ts';
|
|
5
|
+
import type { PadroneSchema } from '../types/schema.ts';
|
|
6
|
+
import type { WithCommand } from '../util/type-utils.ts';
|
|
7
|
+
import { getRootCommand, getVersion } from '../util/utils.ts';
|
|
8
|
+
|
|
9
|
+
// ── Types ────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
export type VersionCommand = PadroneCommand<'version', '', PadroneSchema<void>, string, [], [], false>;
|
|
12
|
+
|
|
13
|
+
export type WithVersion<T> = WithCommand<T, 'version', VersionCommand>;
|
|
14
|
+
|
|
15
|
+
// ── Interceptor ─────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
const versionInterceptor = defineInterceptor({ id: 'padrone:version', name: 'padrone:version', order: -1000 }, () => ({
|
|
18
|
+
parse(_ctx, next) {
|
|
19
|
+
return thenMaybe(next(), (res) => {
|
|
20
|
+
const hasVersionFlag = res.rawArgs.version || res.rawArgs.v || res.rawArgs.V;
|
|
21
|
+
|
|
22
|
+
// Only show version for root command (no subcommand matched)
|
|
23
|
+
if (hasVersionFlag && !res.command.parent) {
|
|
24
|
+
delete res.rawArgs.version;
|
|
25
|
+
delete res.rawArgs.v;
|
|
26
|
+
delete res.rawArgs.V;
|
|
27
|
+
|
|
28
|
+
// Route to the version command so its action handles the rest
|
|
29
|
+
const versionCmd = res.command.commands?.find((c) => c.name === 'version');
|
|
30
|
+
if (versionCmd) {
|
|
31
|
+
resolveCommand(versionCmd);
|
|
32
|
+
return { ...res, command: versionCmd, rawArgs: {}, positionalArgs: [] };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return res;
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
// ── Extension ────────────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Extension that adds version support:
|
|
45
|
+
* - `version` command
|
|
46
|
+
* - `--version` / `-v` / `-V` flags (root command only)
|
|
47
|
+
*
|
|
48
|
+
* Usage:
|
|
49
|
+
* ```ts
|
|
50
|
+
* createPadrone('my-cli').extend(padroneVersion())
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function padroneVersion(): <T extends CommandTypesBase>(builder: T) => WithVersion<T> {
|
|
54
|
+
return ((builder: AnyPadroneBuilder) =>
|
|
55
|
+
builder
|
|
56
|
+
.command('version', (c) =>
|
|
57
|
+
c.configure({ description: 'Display the version number', hidden: true }).action((_args, ctx) => {
|
|
58
|
+
const rootCommand = getRootCommand(ctx.command);
|
|
59
|
+
return getVersion(rootCommand.version);
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
.intercept(versionInterceptor)) as any;
|
|
63
|
+
}
|