All files / create-package/src cli.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                  6x                 6x 28x 22x   22x 1x             5x              
import yargs from 'yargs';
 
import type { CommandModule } from './commands.ts';
 
/**
 * The entry point of `create-package`, a yargs application for creating new
 * monorepo packages. See the repository contributor documentation for more
 * information.
 *
 * @param argv - The unmodified `process.argv`.
 * @param commands - The yargs command modules.
 */
export default async function cli(
  argv: string[],
  // Parameterized for easier testing.
  commands: CommandModule[],
): Promise<void> {
  await yargs(argv.slice(2))
    .scriptName('create-package')
    // Disable --version. This is an internal tool and it doesn't have a version.
    .version(false)
    .usage('$0 [args]')
    .command(commands)
    .strict()
    .check((args) => {
      // Trim all strings and ensure they are not empty.
      for (const key in args) {
        if (typeof args[key] === 'string') {
          args[key] = args[key].trim();
 
          if (args[key] === '') {
            throw new Error(
              `The argument "${key}" was processed to an empty string. Please provide a value with non-whitespace characters.`,
            );
          }
        }
      }
 
      return true;
    }, true) // `true` indicating that this check should be enabled for all commands and sub-commands.
    .showHelpOnFail(false)
    .help()
    .alias('help', 'h')
    .parseAsync();
}