All files / init init-npm.js

100% Statements 45/45
100% Branches 6/6
100% Functions 4/4
100% Lines 44/44
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                  1x   1x 1x 1x 1x 1x       6x 6x 6x 6x 6x 6x   6x   1x 1x   2x 2x   3x 3x   6x 6x     6x 6x 6x   6x 6x   6x 6x 6x     6x   1x 1x 1x   2x 2x 2x   3x 3x         6x 6x     6x 12x 12x     6x      
import debug from 'debug';
import fs from 'fs-promise';
import path from 'path';
import username from 'username';
 
import installDepList from '../util/install-dependencies';
import readPackageJSON from '../util/read-package-json';
import asyncOra from '../util/ora-handler';
 
const d = debug('electron-forge:init:npm');
 
export const deps = ['electron-compile'];
export const devDeps = ['babel-preset-env', 'babel-preset-react', 'babel-plugin-transform-async-to-generator'];
export const exactDevDeps = ['electron-prebuilt-compile'];
export const standardDeps = ['standard'];
export const airbnDeps = ['eslint', 'eslint-config-airbnb', 'eslint-plugin-import',
  'eslint-plugin-jsx-a11y', 'eslint-plugin-react'];
 
export default async (dir, lintStyle) => {
  await asyncOra('Initializing NPM Module', async () => {
    const packageJSON = await readPackageJSON(path.resolve(__dirname, '../../tmpl'));
    packageJSON.productName = packageJSON.name = path.basename(dir).toLowerCase();
    packageJSON.config.forge.electronWinstallerConfig.name = packageJSON.name.replace(/-/g, '_');
    packageJSON.config.forge.windowsStoreConfig.name = packageJSON.productName.replace(/-/g, '');
    packageJSON.author = await username();
 
    switch (lintStyle) {
      case 'standard':
        packageJSON.scripts.lint = 'standard';
        break;
      case 'airbnb':
        packageJSON.scripts.lint = 'eslint src';
        break;
      default:
        packageJSON.scripts.lint = 'echo "No linting configured"';
        break;
    }
    d('writing package.json to:', dir);
    await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON, null, 4));
  });
 
  await asyncOra('Installing NPM Dependencies', async () => {
    d('installing dependencies');
    await installDepList(dir, deps);
 
    d('installing devDependencies');
    await installDepList(dir, devDeps, true);
 
    d('installing exact dependencies');
    for (const packageName of exactDevDeps) {
      await installDepList(dir, [packageName], true, true);
    }
 
    switch (lintStyle) {
      case 'standard':
        d('installing standard linting dependencies');
        await installDepList(dir, standardDeps, true);
        break;
      case 'airbnb':
        d('installing airbnb linting dependencies');
        await installDepList(dir, airbnDeps, true);
        break;
      default:
        d('not installing linting deps');
        break;
    }
 
    // NB: For babel-preset-env to work correctly, it needs to know the
    // actual version of Electron that we installed
    const content = JSON.parse(await fs.readFile(path.join(dir, '.compilerc'), 'utf8'));
    const electronPrebuilt = require(
      path.join(dir, 'node_modules', 'electron-prebuilt-compile', 'package.json'));
 
    for (const profile of ['development', 'production']) {
      const envTarget = content.env[profile]['application/javascript'].presets.find(x => x[0] === 'env');
      envTarget[1].targets.electron = electronPrebuilt.version;
    }
 
    await fs.writeFile(path.join(dir, '.compilerc'), JSON.stringify(content, null, 2), 'utf8');
  });
};