All files / install-all/src cli.js

100% Statements 21/21
100% Branches 6/6
100% Functions 4/4
100% Lines 20/20

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75    1x 1x     1x                                     11x 11x 34x 34x   11x       11x 11x         10x       1x 1x       2x 2x                   2x 7x 7x     2x     1x            
'use strict'
 
const { parseArgs } = require('node:util')
const path = require('node:path')
 
// Switch registry — add new flags here and they auto-appear in help output
const FLAG_REGISTRY = {
  version: {
    type: 'boolean',
    short: 'v',
    description: 'Print the version number and exit',
  },
  help: {
    type: 'boolean',
    short: 'h',
    description: 'Show this help message and exit',
  },
  clean: {
    type: 'boolean',
    short: 'c',
    description: 'Run clean install (npm ci, yarn/pnpm/bun install --frozen-lockfile)',
  },
}
 
function buildParseArgsOptions() {
  const options = {}
  for (const [name, config] of Object.entries(FLAG_REGISTRY)) {
    options[name] = { type: config.type }
    if (config.short) options[name].short = config.short
  }
  return options
}
 
function parseCliArgs(argv) {
  const args = argv || process.argv.slice(2)
  const { values } = parseArgs({
    args,
    options: buildParseArgsOptions(),
    strict: true,
  })
  return values
}
 
function printVersion() {
  const pkg = require(path.join(__dirname, '..', 'package.json'))
  console.log(pkg.version)
}
 
function printHelp() {
  const pkg = require(path.join(__dirname, '..', 'package.json'))
  const lines = [
    `${pkg.name} v${pkg.version}`,
    '',
    pkg.description,
    '',
    'Usage: install-all [options]',
    '',
    'Options:',
  ]
 
  for (const [name, config] of Object.entries(FLAG_REGISTRY)) {
    const shortFlag = config.short ? `-${config.short}, ` : '    '
    lines.push(`  ${shortFlag}--${name.padEnd(12)} ${config.description}`)
  }
 
  console.log(lines.join('\n'))
}
 
module.exports = {
  FLAG_REGISTRY,
  parseCliArgs,
  printVersion,
  printHelp,
}