Repository: jiahaog/nativefier
File: src/cli.js (JavaScript)

Code before:

const dns = require('dns');
const packageJson = require('./../package');

function collect(val, memo) {
  memo.push(val);
  return memo;
}

function parseJson(val) {
  if (!val) return {};
  return JSON.parse(val);
}

function getProcessEnvs(val) {
  if (!val) return {};
  const pEnv = {};
  pEnv.processEnvs = parseJson(val);
  return pEnv;
}

function checkInternet() {
  dns.lookup('npmjs.com', (err) => {
    if (err && err.code === 'ENOTFOUND') {
      console.log('\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.');
      process.exit(1);
    }
  });
}


if (require.main === module) {
  program
    .version(packageJson.version)
    .arguments('<targetUrl> [dest]')
    .action((targetUrl, appDir) => {
      program.targetUrl = targetUrl;
      program.out = appDir;
    })
    .option('-n, --name <value>', 'app name')
    .option('-p, --platform <value>', '\'osx\', \'linux\' or \'windows\'')
    .option('-a, --arch <value>', '\'ia32\' or \'x64\' or \'armv7l\'')
    .option('--app-version <value>', 'The release version of the application.  Maps to the `ProductVersion` metadata property on Windows, and `CFBundleShortVersionString` on OS X.')
    .option('--build-version <value>', 'The build version of the application. Maps to the `FileVersion` metadata property on Windows, and `CFBundleVersion` on OS X.')
    .option('--app-copyright <value>', 'The human-readable copyright line for the app. Maps to the `LegalCopyright` metadata property on Windows, and `NSHumanReadableCopyright` on OS X')
    .option('--win32metadata <json-string>', 'a JSON string of key/value pairs of application metadata (ProductName, InternalName, FileDescription) to embed into the executable (Windows only).', parseJson)
    .option('-e, --electron-version <value>', 'electron version to package, without the \'v\', see https://github.com/atom/electron/releases')
    .option('--no-overwrite', 'do not override output directory if it already exists, defaults to false')
    .option('-c, --conceal', 'packages the source code within your app into an archive, defaults to false, see http://electron.atom.io/docs/v0.36.0/tutorial/application-packaging/')
    .option('--counter', 'if the target app should use a persistant counter badge in the dock (OSX only), defaults to false')
    .option('-i, --icon <value>', 'the icon file to use as the icon for the app (should be a .icns file on OSX, .png for Windows and Linux)')

Review comment (bug):
@mdh34 but `process.exit()`-ing here defeats the purpose, as it would make building offline altogether impossible, with or without cached files 🙂. You should

* Either do your `checkInternet` work later, when you determined user is offline AND Electron cached files are absent. Only when *both* of these conditions are met, `exit(1)` is the thing to do.
* Or leave the check where it is now (meaning it will always be done, which doesn't seem bad), but do not `exit()`, meaning packaging offline will be attempted (and maybe fail), but at least we'll have warned the user.

Your choice.

Code after:

const dns = require('dns');
const packageJson = require('./../package');

function collect(val, memo) {
  memo.push(val);
  return memo;
}

function parseJson(val) {
  if (!val) return {};
  return JSON.parse(val);
}

function getProcessEnvs(val) {
  if (!val) return {};
  const pEnv = {};
  pEnv.processEnvs = parseJson(val);
  return pEnv;
}

function checkInternet() {
  dns.lookup('npmjs.com', (err) => {
    if (err && err.code === 'ENOTFOUND') {
      console.log('\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) {
  program
    .version(packageJson.version)
    .arguments('<targetUrl> [dest]')
    .action((targetUrl, appDir) => {
      program.targetUrl = targetUrl;
      program.out = appDir;
    })
    .option('-n, --name <value>', 'app name')
    .option('-p, --platform <value>', '\'osx\', \'linux\' or \'windows\'')
    .option('-a, --arch <value>', '\'ia32\' or \'x64\' or \'armv7l\'')
    .option('--app-version <value>', 'The release version of the application.  Maps to the `ProductVersion` metadata property on Windows, and `CFBundleShortVersionString` on OS X.')
    .option('--build-version <value>', 'The build version of the application. Maps to the `FileVersion` metadata property on Windows, and `CFBundleVersion` on OS X.')
    .option('--app-copyright <value>', 'The human-readable copyright line for the app. Maps to the `LegalCopyright` metadata property on Windows, and `NSHumanReadableCopyright` on OS X')
    .option('--win32metadata <json-string>', 'a JSON string of key/value pairs of application metadata (ProductName, InternalName, FileDescription) to embed into the executable (Windows only).', parseJson)
    .option('-e, --electron-version <value>', 'electron version to package, without the \'v\', see https://github.com/atom/electron/releases')
    .option('--no-overwrite', 'do not override output directory if it already exists, defaults to false')
    .option('-c, --conceal', 'packages the source code within your app into an archive, defaults to false, see http://electron.atom.io/docs/v0.36.0/tutorial/application-packaging/')
    .option('--counter', 'if the target app should use a persistant counter badge in the dock (OSX only), defaults to false')
    .option('-i, --icon <value>', 'the icon file to use as the icon for the app (should be a .icns file on OSX, .png for Windows and Linux)')
    .option('--width <value>', 'set window default width, defaults to 1280px', parseInt)