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')

Review comment (suggestion):
Four things:

1. In the first condition (long option), you need to accept literal `-` too, which exists in long options like `--electron-version`. So, `[a-z-]`.
2. Your code works, but is a bit weird: each condition will be true when we have a match, so a hit at position `0`, and `!0` is true. Using [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) will avoid this weird negation, making things more readable.
4. You explicitly test there's nothing before a match, with `^`, and that's good. Let's test too there's nothing *after*, with `$`
3. Given the point above, testing for `&& arg.length == 3` in the second condition becomes unnecessary. `^--[a-z]$` means "dash dash followed by any ***single*** character in the A-Z range", so here you already asserted that '--x' is what you're looking for, but not '--xyz'

All four together means this should work:

```suggestion
      arg.match(/^-[a-z-]{2,}$/i) || arg.match(/^--[a-z]$/i)
```

Code after:
    throw err;
  }
}

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')