All files / api package.js

69.44% Statements 25/36
34.78% Branches 8/23
66.67% Functions 4/6
69.44% Lines 25/36
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                  1x                                   2x             2x 2x   2x 2x       2x   2x         2x     2x         2x         2x 2x 2x     2x   2x 2x   2x 2x 2x                         2x 2x         2x   2x   2x    
import 'colors';
import debug from 'debug';
import fs from 'fs-promise';
import glob from 'glob';
import path from 'path';
import pify from 'pify';
import packager from 'electron-packager';
 
import electronHostArch from '../util/electron-host-arch';
import getForgeConfig from '../util/forge-config';
import ora from '../util/ora';
import packagerCompileHook from '../util/compile-hook';
import readPackageJSON from '../util/read-package-json';
import rebuildHook from '../util/rebuild';
import requireSearch from '../util/require-search';
import resolveDir from '../util/resolve-dir';
 
const d = debug('electron-forge:packager');
 
/**
 * @typedef {Object} PackageOptions
 * @property {string} [dir=process.cwd()] The path to the app to package
 * @property {boolean} [interactive=false] Whether to use sensible defaults or prompt the user visually
 * @property {string} [arch=process.arch] The target arch
 * @property {string} [platform=process.platform] The target platform.  NOTE: This is limited to be the current platform at the moment
 */
 
/**
 * Package an Electron application into an platform dependent format.
 *
 * @param {PackageOptions} providedOptions - Options for the Package method
 * @return {Promise} Will resolve when the package process is complete
 */
export default async (providedOptions = {}) => {
  // eslint-disable-next-line prefer-const, no-unused-vars
  let { dir, interactive, arch, platform } = Object.assign({
    dir: process.cwd(),
    interactive: false,
    arch: electronHostArch(),
    platform: process.platform,
  }, providedOptions);
 
  let prepareSpinner = ora(`Preparing to Package Application for arch: ${(arch === 'all' ? 'ia32' : arch).cyan}`).start();
  let prepareCounter = 0;
 
  dir = await resolveDir(dir);
  Iif (!dir) {
    throw 'Failed to locate compilable Electron application';
  }
 
  const packageJSON = await readPackageJSON(dir);
 
  Iif (path.dirname(require.resolve(path.resolve(dir, packageJSON.main))) === dir) {
    console.error(`Entry point: ${packageJSON.main}`.red);
    throw 'The entry point to your application ("packageJSON.name") must be in a subfolder not in the top level directory';
  }
 
  const forgeConfig = await getForgeConfig(dir);
  let packagerSpinner;
 
  const packageOpts = Object.assign({
    asar: false,
    overwrite: true,
  }, forgeConfig.electronPackagerConfig, {
    afterCopy: [async (buildPath, electronVersion, pPlatform, pArch, done) => {
      Iif (packagerSpinner) {
        packagerSpinner.succeed();
        prepareCounter += 1;
        prepareSpinner = ora(`Preparing to Package Application for arch: ${(prepareCounter === 2 ? 'armv7l' : 'x64').cyan}`).start();
      }
      await fs.remove(path.resolve(buildPath, 'node_modules/electron-compile/test'));
      const bins = await pify(glob)(path.join(buildPath, '**/.bin/**/*'));
      for (const bin of bins) {
        await fs.remove(bin);
      }
      done();
    }, async (...args) => {
      prepareSpinner.succeed();
      await packagerCompileHook(dir, ...args);
    }, async (buildPath, electronVersion, pPlatform, pArch, done) => {
      await rebuildHook(buildPath, electronVersion, pPlatform, pArch);
      packagerSpinner = ora('Packaging Application').start();
      done();
    }].concat(forgeConfig.electronPackagerConfig.afterCopy ? forgeConfig.electronPackagerConfig.afterCopy.map(item =>
      (typeof item === 'string' ? requireSearch(dir, [item]) : item)
    ) : []),
    afterExtract: forgeConfig.electronPackagerConfig.afterExtract ? forgeConfig.electronPackagerConfig.afterExtract.map(item =>
      (typeof item === 'string' ? requireSearch(dir, [item]) : item)
    ) : [],
    dir,
    arch,
    platform,
    out: path.resolve(dir, 'out'),
    electronVersion: packageJSON.devDependencies['electron-prebuilt-compile'],
  });
  packageOpts.quiet = true;
  Iif (typeof packageOpts.asar === 'object' && packageOpts.asar.unpack) {
    packagerSpinner.fail();
    throw new Error('electron-compile does not support asar.unpack yet.  Please use asar.unpackDir');
  }
 
  d('packaging with options', packageOpts);
 
  await pify(packager)(packageOpts);
 
  packagerSpinner.succeed();
};