-
Notifications
You must be signed in to change notification settings - Fork 35
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Describe the feature
I often implement batch processes in citty. And often I have to write logs to measure such execution times.
run: async ({ args }) => {
const start = process.hrtime()
// do something...
const end = process.hrtime(start)
const elapsed = (end[0] * 1000 + end[1] / 1e6) / 1000
logger.info(`duration: ${elapsed.toFixed(3)} s`)
However, there are some problems with this.
- Difficult to include setup and cleanup time
- I need to write the same process every time
So, could you support the --duration option in citty?
maybe...
export async function runMain<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
opts: RunMainOptions = {},
) {
const rawArgs = opts.rawArgs || process.argv.slice(2);
const showUsage = opts.showUsage || _showUsage;
try {
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
await showUsage(...(await resolveSubCommand(cmd, rawArgs)));
process.exit(0);
} else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
const meta =
typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
if (!meta?.version) {
throw new CLIError("No version specified", "E_NO_VERSION");
}
consola.log(meta.version);
} else {
// get start time
await runCommand(cmd, { rawArgs });
// get end time and calcurate procecc time
// if used --duration option, do console.log(`duration: ${dutationtime}s`)
}
} catch (error: any) {
const isCLIError = error instanceof CLIError;
if (isCLIError) {
await showUsage(...(await resolveSubCommand(cmd, rawArgs)));
consola.error(error.message);
} else {
consola.error(error, "\n");
}
process.exit(1);
}
}
and add option.
Additional information
- Would you be willing to help implement this feature?
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request