Skip to content
Permalink
Browse files

feat(initializer): allow custom initialzers

ISSUES CLOSED: #35
  • Loading branch information
MarshallOfSound committed Dec 30, 2016
1 parent 3429c9f commit 9e6ddfa0a79360a097c1b83f6e37068b5cbee000
Showing with 88 additions and 11 deletions.
  1. +1 −0 package.json
  2. +18 −9 src/electron-forge-init.js
  3. +65 −0 src/init/init-custom.js
  4. +3 −1 src/init/init-npm.js
  5. +1 −1 src/init/init-starter-files.js
@@ -74,6 +74,7 @@
"node-gyp": "^3.4.0",
"ora": "^0.3.0",
"pify": "^2.3.0",
"resolve-package": "^1.0.1",
"semver": "^5.3.0",
"username": "^2.2.2",
"yarn-or-npm": "^2.0.2",
@@ -2,6 +2,7 @@ import debug from 'debug';
import path from 'path';
import program from 'commander';

import initCustom from './init/init-custom';
import initDirectory from './init/init-directory';
import initGit from './init/init-git';
import initNPM from './init/init-npm';
@@ -17,7 +18,8 @@ const main = async () => {
program
.version(require('../package.json').version)
.arguments('[name]')
.option('-l, --lintstyle [style]', 'Linting standard to follow. Can be "airbnb" or "standard"', 'airbnb')
.option('-t, --template [name]', 'Name of the forge template to use')
.option('-l, --lintstyle [style]', 'Linting standard to follow. For the default template it can be "airbnb" or "standard"', 'airbnb')
.action((name) => {
if (!name) return;
if (path.isAbsolute(name)) {
@@ -29,18 +31,25 @@ const main = async () => {
.parse(process.argv);

d(`Initializing in: ${dir}`);
program.lintstyle = program.lintstyle.toLowerCase();
if (!['airbnb', 'standard'].includes(program.lintstyle)) {
d(`Unrecognized lintstyle argument: '${program.lintstyle}' -- defaulting to 'airbnb'`);
program.lintstyle = 'airbnb';

if (!program.template) {
program.lintstyle = program.lintstyle.toLowerCase();
if (!['airbnb', 'standard'].includes(program.lintstyle)) {
d(`Unrecognized lintstyle argument: '${program.lintstyle}' -- defaulting to 'airbnb'`);
program.lintstyle = 'airbnb';
}
}

await initDirectory(dir);
await initGit(dir);
await initNPM(dir, program.lintstyle);
await initStarter(dir, program.lintstyle);
if (program.lintstyle === 'standard') {
await initStandardFix(dir);
await initNPM(dir, program.template ? undefined : program.lintstyle);
await initStarter(dir, program.template ? undefined : program.lintstyle);
if (!program.template) {
if (program.lintstyle === 'standard') {
await initStandardFix(dir);
}
} else {
await initCustom(dir, program.template, program.lintstyle);
}
};

@@ -0,0 +1,65 @@
import debug from 'debug';
import fs from 'fs-promise';
import glob from 'glob';
import resolvePackage from 'resolve-package';
import ora from 'ora';
import path from 'path';

import { copy } from './init-starter-files';
import installDepList from '../util/install-dependencies';

const d = debug('electron-forge:init:custom');

export default async (dir, template, lintStyle) => {
const resolveSpinner = ora.ora(`Locating custom template: "${template}"`).start();
let templateModulePath;
try {
templateModulePath = await resolvePackage(`electron-forge-template-${template}`);
} catch (err) {
resolveSpinner.fail();
throw new Error(`Failed to locate custom template: "${template}"\n\nTry \`npm install -g electron-forge-template-${template}\``);
}
resolveSpinner.succeed();

let templateModule = require(templateModulePath);

templateModule = templateModule.default || templateModule;

const installSpinner = ora.ora('Installing Template Dependencies').start();

try {
d('installing dependencies');
await installDepList(dir, templateModule.dependencies || []);
d('installing devDependencies');
await installDepList(dir, templateModule.devDependencies || [], true);
} catch (err) {
installSpinner.fail();
throw err;
}

installSpinner.succeed();

const copySpinner = ora.ora('Copying Template Files').start();
const templateDirectory = templateModule.templateDirectory;
if (templateDirectory) {
const tmplPath = templateDirectory;
if (!path.isAbsolute(templateDirectory)) {
copySpinner.fail();
throw new Error(`Custom template path needs to be absolute, this is an issue with "electron-forge-template-${template}"`);
}

const files = glob.sync(path.resolve(tmplPath, '**/*'));

for (const file of files) {
if ((await fs.stat(file)).isFile()) {
await copy(file, path.resolve(dir, path.relative(tmplPath, file).replace(/^_/, '.')));
}
}
}

copySpinner.succeed();

if (typeof templateModule.postCopy === 'function') {
await Promise.resolve(templateModule.postCopy(dir, ora.ora, lintStyle));
}
};
@@ -54,10 +54,12 @@ export default async (dir, lintStyle) => {
await installDepList(dir, standardDeps, true);
break;
case 'airbnb':
default:
d('installing airbnb linting dependencies');
await installDepList(dir, airbnDeps, true);
break;
default:
d('not installing linting deps');
break;
}
} catch (err) {
installSpinner.fail();
@@ -5,7 +5,7 @@ import path from 'path';

const d = debug('electron-forge:init:starter-files');

const copy = (source, target) =>
export const copy = (source, target) =>
new Promise((resolve, reject) => {
d(`copying "${source}" --> "${target}"`);
let rd;

0 comments on commit 9e6ddfa

Please sign in to comment.
You can’t perform that action at this time.