Repository: jiahaog/nativefier
File: src/cli.ts (TypeScript)

Code before:

    if (targetAndUpgrade) {
      throw new Error(
        'ERROR: Nativefier must be called with either a targetUrl or the --upgrade option, not both.\n',
      );
    }
  }

  if (!parsed.targetUrl && !parsed.upgrade) {
    throw new Error(
      'ERROR: Nativefier must be called with either a targetUrl or the --upgrade option.\n',
    );
  }

  parsed.noOverwrite = parsed['no-overwrite'] = !parsed.overwrite;

  // Since coerce in yargs seems to have broken since
  // https://github.com/yargs/yargs/pull/1978
  for (const arg of [
    'win32metadata',
    'browserwindow-options',
    'file-download-options',
  ]) {
    if (parsed[arg] && typeof parsed[arg] === 'string') {
      parsed[arg] = parseJson(parsed[arg] as string);
      // also set camel-cased option keys
      parsed[camelCased(arg)] = parsed[arg];
    }
  }
  if (parsed['process-envs'] && typeof parsed['process-envs'] === 'string') {
    parsed['process-envs'] = getProcessEnvs(parsed['process-envs']);
  }

  return parsed;
}

function sanitizeArgs(argv: string[]): string[] {
  const sanitizedArgs: string[] = [];
  argv.forEach((arg) => {
    if (isArgFormatInvalid(arg)) {
      throw new Error(
        `Invalid argument passed: ${arg} .\nNativefier supports short options (like "-n") and long options (like "--name"), all lowercase. Run "nativefier --help" for help.\nAborting`,
      );
    }
    const isLastArg = sanitizedArgs.length + 1 === argv.length;
    if (sanitizedArgs.length > 0) {
      const previousArg = sanitizedArgs[sanitizedArgs.length - 1];

      log.debug({ arg, previousArg, isLastArg });

      // Work around commander.js not supporting default argument for options

Review comment (suggestion):
Pls replace with a comment that says _why_, not just _what_. The _what_ is the code, and in such a case the code is sufficiently simple to not need a translation to English, but a _why_ giving context will be appreciated by our future selves or other maintainers wondering why we do this 🙂.

Code after:

    if (targetAndUpgrade) {
      throw new Error(
        'ERROR: Nativefier must be called with either a targetUrl or the --upgrade option, not both.\n',
      );
    }
  }

  if (!parsed.targetUrl && !parsed.upgrade) {
    throw new Error(
      'ERROR: Nativefier must be called with either a targetUrl or the --upgrade option.\n',
    );
  }

  parsed.noOverwrite = parsed['no-overwrite'] = !parsed.overwrite;

  // Since coerce in yargs seems to have broken since
  // https://github.com/yargs/yargs/pull/1978
  for (const arg of [
    'win32metadata',
    'browserwindow-options',
    'file-download-options',
  ]) {
    if (parsed[arg] && typeof parsed[arg] === 'string') {
      parsed[arg] = parseJson(parsed[arg] as string);
      // sets fileDownloadOptions and browserWindowOptions
      // as parsed object as they were still strings in `nativefier.json`
      // because only their snake-cased variants were being parsed above
      parsed[camelCased(arg)] = parsed[arg];
    }
  }
  if (parsed['process-envs'] && typeof parsed['process-envs'] === 'string') {
    parsed['process-envs'] = getProcessEnvs(parsed['process-envs']);
  }

  return parsed;
}

function sanitizeArgs(argv: string[]): string[] {
  const sanitizedArgs: string[] = [];
  argv.forEach((arg) => {
    if (isArgFormatInvalid(arg)) {
      throw new Error(
        `Invalid argument passed: ${arg} .\nNativefier supports short options (like "-n") and long options (like "--name"), all lowercase. Run "nativefier --help" for help.\nAborting`,
      );
    }
    const isLastArg = sanitizedArgs.length + 1 === argv.length;
    if (sanitizedArgs.length > 0) {
      const previousArg = sanitizedArgs[sanitizedArgs.length - 1];

      log.debug({ arg, previousArg, isLastArg });