import type { ChalkInstance } from "chalk";
import type { Logger } from "ts-log";
import { Output, Program } from "@libreworks/cli";
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
async function makeLogEntries(logger: Logger, colors: ChalkInstance) {
logger.info("Time to make the entries!");
for (let i = 0; i < 10; i++) {
logger.trace(`Testing ${colors.green(i)}`);
await sleep(500);
}
}
const output = Output.create({});
const program = new Program("@libreworks/cli:example-handling-output")
.description("A test of @libreworks/cli")
.version("0.0.0");
program.action(async function (options, cmd) {
// Send data as-is to stdout.
output.write("You called the handler!");
// Use the logger, which writes to stderr.
output.log.info("Handler invoked", { options });
output.log.info(`Testing ${output.colors.red("123")}`);
await Promise.all([
// Send logs to stderr at regular intervals.
makeLogEntries(output.logger, output.colors),
// Show a loading indicator for 5 seconds.
output.spinDuring(sleep(5000), "Reticulating Splines"),
]);
output.log.warn("This is a warning!");
try {
throw new Error("A Big Problem");
} catch (e) {
// Stop the spinner and display the error.
output.fail(e);
}
});
await program.parseAsync();