All files / init init-starter-files.js

88.46% Statements 23/26
100% Branches 2/2
85.71% Functions 6/7
88% Lines 22/25
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            1x   1x 40x 40x     40x         40x 40x 40x 40x 40x 40x       6x 6x   6x 6x 6x 6x 6x   6x 14x   6x 12x        
import debug from 'debug';
import fs from 'fs-promise';
import path from 'path';
 
import asyncOra from '../util/ora-handler';
 
const d = debug('electron-forge:init:starter-files');
 
export const copy = (source, target) =>
  new Promise((resolve, reject) => {
    d(`copying "${source}" --> "${target}"`);
    let rd;
    let wr;
    const rejectCleanup = (err) => {
      rd.destroy();
      wr.end();
      reject(err);
    };
    rd = fs.createReadStream(source);
    rd.on('error', rejectCleanup);
    wr = fs.createWriteStream(target);
    wr.on('error', rejectCleanup);
    wr.on('finish', resolve);
    rd.pipe(wr);
  });
 
export default async (dir, lintStyle) => {
  await asyncOra('Copying Starter Files', async () => {
    const tmplPath = path.resolve(__dirname, '../../tmpl');
 
    d('creating directory:', path.resolve(dir, 'src'));
    await fs.mkdirs(path.resolve(dir, 'src'));
    const rootFiles = ['_gitignore', '_compilerc'];
    if (lintStyle === 'airbnb') rootFiles.push('_eslintrc');
    const srcFiles = ['index.js', 'index.html'];
 
    rootFiles.forEach(async (file) => {
      await copy(path.resolve(tmplPath, file), path.resolve(dir, file.replace(/^_/, '.')));
    });
    srcFiles.forEach(async (file) => {
      await copy(path.resolve(tmplPath, file), path.resolve(dir, 'src', file));
    });
  });
};