initial commit
This commit is contained in:
commit
db4a1e24ca
10 changed files with 1977 additions and 0 deletions
115
src/index.ts
Normal file
115
src/index.ts
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import {
|
||||
buildApplication,
|
||||
buildCommand,
|
||||
buildRouteMap,
|
||||
numberParser,
|
||||
run,
|
||||
} from "@stricli/core";
|
||||
import { $, CommandBuilder } from "dax";
|
||||
|
||||
$.setPrintCommand(true);
|
||||
|
||||
interface EchoFlags {
|
||||
count: number;
|
||||
verbose: boolean;
|
||||
name: string | undefined;
|
||||
level: "info" | "warn" | "error" | undefined;
|
||||
timeout: number | undefined;
|
||||
}
|
||||
|
||||
const echo = buildCommand<EchoFlags, [string]>({
|
||||
docs: {
|
||||
brief: "Echo a message with optional flags",
|
||||
fullDescription:
|
||||
"This command echoes a message and demonstrates various flag types.",
|
||||
},
|
||||
parameters: {
|
||||
aliases: {
|
||||
c: "count",
|
||||
v: "verbose",
|
||||
n: "name",
|
||||
t: "timeout",
|
||||
},
|
||||
flags: {
|
||||
count: { kind: "counter", brief: "Number of times to repeat" },
|
||||
verbose: { kind: "boolean", brief: "Enable verbose output" },
|
||||
name: {
|
||||
kind: "parsed",
|
||||
parse: (s: string) => s,
|
||||
brief: "Name to include",
|
||||
optional: true,
|
||||
},
|
||||
level: {
|
||||
kind: "enum",
|
||||
values: ["info", "warn", "error"] as const,
|
||||
brief: "Log level",
|
||||
optional: true,
|
||||
},
|
||||
timeout: {
|
||||
kind: "parsed",
|
||||
parse: numberParser,
|
||||
brief: "Timeout in ms",
|
||||
optional: true,
|
||||
},
|
||||
},
|
||||
positional: {
|
||||
kind: "tuple",
|
||||
parameters: [
|
||||
{
|
||||
parse: (s: string) => s,
|
||||
placeholder: "MESSAGE",
|
||||
brief: "Message to echo",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
func: async (flags, message) => {
|
||||
$.logStep("Starting echo command");
|
||||
$.log("Flags:", flags);
|
||||
$.log("Message:", message);
|
||||
|
||||
$.logStep("Executing echo via $");
|
||||
const result = await $`echo ${message}`.stdout("piped").stderr("piped");
|
||||
$.logWarn("Stderr:", result.stderr);
|
||||
$.log("Stdout:", result.stdout);
|
||||
|
||||
if (flags.count > 0) {
|
||||
$.logStep(`Repeating ${flags.count} times via commandBuilder`);
|
||||
const cmd = new CommandBuilder()
|
||||
.command(["echo", message])
|
||||
.env({ MY_VAR: "test" })
|
||||
.timeout(flags.timeout ?? 30 * 1000);
|
||||
for (let i = 0; i < flags.count; i++) {
|
||||
await cmd;
|
||||
}
|
||||
}
|
||||
|
||||
if (flags.name) {
|
||||
$.log("Name flag was:", flags.name);
|
||||
}
|
||||
|
||||
if (flags.level) {
|
||||
$.logError(`Level set to: ${flags.level}`);
|
||||
}
|
||||
|
||||
$.logStep("Done!");
|
||||
},
|
||||
});
|
||||
|
||||
const root = buildRouteMap({
|
||||
docs: {
|
||||
brief: "My CLI tool",
|
||||
},
|
||||
routes: {
|
||||
echo,
|
||||
},
|
||||
});
|
||||
|
||||
const app = buildApplication(root, { name: "name-placeholder" });
|
||||
|
||||
await run(app, process.argv.slice(2), {
|
||||
process:
|
||||
process as /* biome-ignore lint/suspicious/noExplicitAny: Stricli types don't align with @types/node */ any,
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue