Skip to content

Can you support --duration option? #197

@ysknsid25

Description

@ysknsid25

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

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions