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

Code before:

function getProcessEnvs(val: string): any {
  if (!val) {
    return {};
  }
  return parseJson(val);
}

function checkInternet(): void {
  dns.lookup('npmjs.com', (err) => {
    if (err && err.code === 'ENOTFOUND') {
      log.warn(
        '\nNo Internet Connection\nTo offline build, download electron from https://github.com/electron/electron/releases\nand place in ~/AppData/Local/electron/Cache/ on Windows,\n~/.cache/electron on Linux or ~/Library/Caches/electron/ on Mac\nUse --electron-version to specify the version you downloaded.',
      );
    }
  });
}

if (require.main === module) {
  const sanitizedArgs = [];
  process.argv.forEach((arg) => {
    if (
      !arg.search(/^-[A-Z]{2,}/i) ||
      (!arg.search(/^--[A-Z]/i) && arg.length == 3)
    ) {
      log.error(`Invalid argument passed: ${arg} \nAborting`);
      process.exit(1);
    }

    if (sanitizedArgs.length > 0) {
      const previousArg = sanitizedArgs[sanitizedArgs.length - 1];

      // Work around commander.js not supporting default argument for options
      if (
        previousArg === '--tray' &&
        !['true', 'false', 'start-in-tray'].includes(arg)
      ) {
        sanitizedArgs.push('true');
      }
    }
    sanitizedArgs.push(arg);
  });

  const positionalOptions = {
    targetUrl: '',
    out: '',
  };
  const args = commander
    .name('nativefier')
    .version(packageJson.version, '-v, --version')
    .arguments('<targetUrl> [dest]')

Review comment (suggestion):
A bit more end-user friendliness would be neat
```suggestion
      log.error(`Invalid argument passed: ${arg} .\nNativefier supports short options (like "-n") and long options (like "--name"), all lowercase. Run "nativefier --help" for help.\nAborting`);
```

Code after:
}

function getProcessEnvs(val: string): any {
  if (!val) {
    return {};
  }
  return parseJson(val);
}

function checkInternet(): void {
  dns.lookup('npmjs.com', (err) => {
    if (err && err.code === 'ENOTFOUND') {
      log.warn(
        '\nNo Internet Connection\nTo offline build, download electron from https://github.com/electron/electron/releases\nand place in ~/AppData/Local/electron/Cache/ on Windows,\n~/.cache/electron on Linux or ~/Library/Caches/electron/ on Mac\nUse --electron-version to specify the version you downloaded.',
      );
    }
  });
}

if (require.main === module) {
  const sanitizedArgs = [];
  process.argv.forEach((arg) => {
    if (isArgFormatValid(arg) === false) {
      log.error(
        `Invalid argument passed: ${arg} .\nNativefier supports short options (like "-n") and long options (like "--name"), all lowercase. Run "nativefier --help" for help.\nAborting`,
      );
      process.exit(1);
    }
    if (sanitizedArgs.length > 0) {
      const previousArg = sanitizedArgs[sanitizedArgs.length - 1];

      // Work around commander.js not supporting default argument for options
      if (
        previousArg === '--tray' &&
        !['true', 'false', 'start-in-tray'].includes(arg)
      ) {
        sanitizedArgs.push('true');
      }
    }
    sanitizedArgs.push(arg);
  });

  const positionalOptions = {
    targetUrl: '',
    out: '',
  };
  const args = commander
    .name('nativefier')
    .version(packageJson.version, '-v, --version')
    .arguments('<targetUrl> [dest]')
    .action((url, outputDirectory) => {