Skip to content

Commit

Permalink
feat(generic): use an ora/promise helper instead of a global uncaught…
Browse files Browse the repository at this point in the history
…Rejection handler (#50)

ISSUES CLOSED: #38
  • Loading branch information
MarshallOfSound authored and malept committed Dec 31, 2016
1 parent 838d70e commit 1b6b727
Show file tree
Hide file tree
Showing 20 changed files with 470 additions and 463 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"lodash.template": "^4.4.0",
"log-symbols": "^1.0.2",
"node-gyp": "^3.4.0",
"ora": "^0.3.0",
"ora": "^0.4.0",
"pify": "^2.3.0",
"resolve-package": "^1.0.1",
"semver": "^5.3.0",
Expand Down
85 changes: 42 additions & 43 deletions src/electron-forge-import.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import debug from 'debug';
import fs from 'fs-promise';
import inquirer from 'inquirer';
import ora from 'ora';
import path from 'path';
import program from 'commander';
import { spawn as yarnOrNPMSpawn, hasYarn } from 'yarn-or-npm';

import initGit from './init/init-git';
import { deps, devDeps } from './init/init-npm';

import asyncOra from './util/ora-handler';
import installDepList from './util/install-dependencies';
import readPackageJSON from './util/read-package-json';

Expand Down Expand Up @@ -114,9 +114,9 @@ const main = async () => {
}

const writeChanges = async () => {
const writeSpinner = ora.ora('Writing modified package.json file').start();
await fs.writeFile(path.resolve(dir, 'package.json'), `${JSON.stringify(packageJSON, null, 2)}\n`);
writeSpinner.succeed();
await asyncOra('Writing modified package.json file', async () => {
await fs.writeFile(path.resolve(dir, 'package.json'), `${JSON.stringify(packageJSON, null, 2)}\n`);
});
};

let electronVersion;
Expand All @@ -129,30 +129,30 @@ const main = async () => {
await writeChanges();

if (electronName) {
const pruneSpinner = ora.ora('Pruning deleted modules').start();
await new Promise((resolve) => {
d('attempting to prune node_modules in:', dir);
const child = yarnOrNPMSpawn(hasYarn() ? [] : ['prune'], {
cwd: dir,
stdio: 'ignore',
await asyncOra('Pruning deleted modules', async () => {
await new Promise((resolve) => {
d('attempting to prune node_modules in:', dir);
const child = yarnOrNPMSpawn(hasYarn() ? [] : ['prune'], {
cwd: dir,
stdio: 'ignore',
});
child.on('exit', () => resolve());
});
child.on('exit', () => resolve());
});
pruneSpinner.succeed();

const installSpinner = ora.ora('Installing dependencies').start();

await fs.remove(path.resolve(dir, 'node_modules/.bin/electron'));
await fs.remove(path.resolve(dir, 'node_modules/.bin/electron.cmd'));
await fs.remove(path.resolve(dir, 'node_modules', electronName));

d('installing dependencies');
await installDepList(dir, deps);
d('installing devDependencies');
await installDepList(dir, devDeps, true);
d('installing electron-prebuilt-compile');
await installDepList(dir, [`electron-prebuilt-compile@${electronVersion}`], false, true);
installSpinner.succeed();
await asyncOra('Installing dependencies', async () => {
d('deleting old dependencies forcefully');
await fs.remove(path.resolve(dir, 'node_modules/.bin/electron'));
await fs.remove(path.resolve(dir, 'node_modules/.bin/electron.cmd'));
await fs.remove(path.resolve(dir, 'node_modules', electronName));

d('installing dependencies');
await installDepList(dir, deps);
d('installing devDependencies');
await installDepList(dir, devDeps, true);
d('installing electron-prebuilt-compile');
await installDepList(dir, [`electron-prebuilt-compile@${electronVersion}`], false, true);
});
}

packageJSON = await readPackageJSON(dir);
Expand All @@ -163,14 +163,14 @@ const main = async () => {

await writeChanges();

const gitignoreSpinner = ora.ora('Fixing .gitignore').start();
if (await fs.exists(path.resolve(dir, '.gitignore'))) {
const gitignore = await fs.readFile(path.resolve(dir, '.gitignore'));
if (!gitignore.includes('out')) {
await fs.writeFile(path.resolve(dir, '.gitignore'), `${gitignore}\nout/`);
await asyncOra('Fixing .gitignore', async () => {
if (await fs.exists(path.resolve(dir, '.gitignore'))) {
const gitignore = await fs.readFile(path.resolve(dir, '.gitignore'));
if (!gitignore.includes('out')) {
await fs.writeFile(path.resolve(dir, '.gitignore'), `${gitignore}\nout/`);
}
}
}
gitignoreSpinner.succeed();
});

let babelConfig = packageJSON.babel;
const babelPath = path.resolve(dir, '.babelrc');
Expand All @@ -179,19 +179,18 @@ const main = async () => {
}

if (babelConfig) {
const babelSpinner = ora.ora('Porting original babel config').start();

let compileConfig = {};
const compilePath = path.resolve(dir, '.compilerc');
if (await fs.exists(compilePath)) {
compileConfig = JSON.parse(await fs.readFile(compilePath, 'utf8'));
}
await asyncOra('Porting original babel config', async () => {
let compileConfig = {};
const compilePath = path.resolve(dir, '.compilerc');
if (await fs.exists(compilePath)) {
compileConfig = JSON.parse(await fs.readFile(compilePath, 'utf8'));
}

await fs.writeFile(compilePath, JSON.stringify(Object.assign(compileConfig, {
'application/javascript': babelConfig,
}), null, 2));
await fs.writeFile(compilePath, JSON.stringify(Object.assign(compileConfig, {
'application/javascript': babelConfig,
}), null, 2));
});

babelSpinner.succeed();
console.info('NOTE: You might be able to remove your `.compilerc` file completely if you are only using the `es2015` and `react` presets'.yellow);
}

Expand Down
48 changes: 23 additions & 25 deletions src/electron-forge-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import debug from 'debug';
import fs from 'fs-promise';
import path from 'path';
import program from 'commander';
import ora from 'ora';
import { spawn as yarnOrNPMSpawn } from 'yarn-or-npm';

import './util/terminate';
import asyncOra from './util/ora-handler';
import resolveDir from './util/resolve-dir';

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

const main = async () => {
const lintSpinner = ora.ora('Linting Application').start();
let dir = process.cwd();
program
.version(require('../package.json').version)
Expand All @@ -27,31 +26,30 @@ const main = async () => {
})
.parse(process.argv);

dir = await resolveDir(dir);
if (!dir) {
lintSpinner.fail();
console.error('Failed to locate lintable Electron application'.red);
if (global._resolveError) global._resolveError();
process.exit(1);
}
await asyncOra('Linting Application', async (lintSpinner) => {
dir = await resolveDir(dir);
if (!dir) {
// eslint-disable-next-line no-throw-literal
throw 'Failed to locate lintable Electron application';
}

d('executing "run lint -- --color" in dir:', dir);
const child = yarnOrNPMSpawn(['run', 'lint', '--', '--color'], {
stdio: process.platform === 'win32' ? 'inherit' : 'pipe',
cwd: dir,
});
const output = [];
if (process.platform !== 'win32') {
child.stdout.on('data', data => output.push(data.toString()));
child.stderr.on('data', data => output.push(data.toString().red));
}
child.on('exit', (code) => {
if (code === 0) lintSpinner.succeed();
if (code !== 0) {
lintSpinner.fail();
output.forEach(data => process.stdout.write(data));
process.exit(code);
d('executing "run lint -- --color" in dir:', dir);
const child = yarnOrNPMSpawn(['run', 'lint', '--', '--color'], {
stdio: process.platform === 'win32' ? 'inherit' : 'pipe',
cwd: dir,
});
const output = [];
if (process.platform !== 'win32') {
child.stdout.on('data', data => output.push(data.toString()));
child.stderr.on('data', data => output.push(data.toString().red));
}
child.on('exit', (code) => {
if (code !== 0) {
lintSpinner.fail();
output.forEach(data => process.stdout.write(data));
process.exit(code);
}
});
});
};

Expand Down
64 changes: 35 additions & 29 deletions src/electron-forge-make.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'colors';
import fs from 'fs-promise';
import path from 'path';
import program from 'commander';
import ora from 'ora';

import './util/terminate';
import asyncOra from './util/ora-handler';
import electronHostArch from './util/electron-host-arch';
import getForgeConfig from './util/forge-config';
import packager from './electron-forge-package';
Expand All @@ -13,7 +13,6 @@ import requireSearch from './util/require-search';
import resolveDir from './util/resolve-dir';

const main = async () => {
const resolveSpinner = ora.ora('Resolving Forge Config').start();
let dir = process.cwd();
program
.version(require('../package.json').version)
Expand All @@ -33,15 +32,16 @@ const main = async () => {
})
.parse(process.argv);

dir = await resolveDir(dir);
if (!dir) {
resolveSpinner.fail();
console.error('Failed to locate makeable Electron application'.red);
if (global._resolveError) global._resolveError();
process.exit(1);
}
let forgeConfig;
await asyncOra('Resolving Forge Config', async () => {
dir = await resolveDir(dir);
if (!dir) {
// eslint-disable-next-line no-throw-literal
throw 'Failed to locate makeable Electron application';
}

resolveSpinner.succeed();
forgeConfig = await getForgeConfig(dir);
});

if (program.platform && program.platform !== process.platform && !(process.platform === 'darwin' && program.platform === 'mas')) {
console.error('You can not "make" for a platform other than your systems platform'.red);
Expand All @@ -58,7 +58,6 @@ const main = async () => {
const declaredArch = program.arch || electronHostArch();
const declaredPlatform = program.platform || process.platform;

const forgeConfig = await getForgeConfig(dir);
let targets = forgeConfig.make_targets[declaredPlatform];
if (program.targets) {
targets = program.targets.split(',');
Expand Down Expand Up @@ -93,24 +92,31 @@ const main = async () => {
}

for (const target of targets) {
const makeSpinner = ora.ora(`Making for target: ${target.cyan} - On platform: ${declaredPlatform.cyan} - For arch: ${targetArch.cyan}`).start();
const maker = requireSearch(__dirname, [
`./makers/${process.platform}/${target}.js`,
`./makers/generic/${target}.js`,
`electron-forge-maker-${target}`,
]);
if (!maker) {
makeSpinner.fail();
throw new Error(`Could not find a build target with the name: ${target} for the platform: ${declaredPlatform}`);
}
try {
outputs.push(await (maker.default || maker)(packageDir, appName, targetArch, forgeConfig, packageJSON));
} catch (err) {
makeSpinner.fail();
if (err) throw err;
throw new Error(`An error occurred while making for target: ${target}`);
}
makeSpinner.succeed();
// eslint-disable-next-line no-loop-func
await asyncOra(`Making for target: ${target.cyan} - On platform: ${declaredPlatform.cyan} - For arch: ${targetArch.cyan}`, async () => {
const maker = requireSearch(__dirname, [
`./makers/${process.platform}/${target}.js`,
`./makers/generic/${target}.js`,
`electron-forge-maker-${target}`,
]);
if (!maker) {
// eslint-disable-next-line no-throw-literal
throw `Could not find a build target with the name: ${target} for the platform: ${declaredPlatform}`;
}
try {
outputs.push(await (maker.default || maker)(packageDir, appName, targetArch, forgeConfig, packageJSON));
} catch (err) {
if (err) {
// eslint-disable-next-line no-throw-literal
throw {
message: `An error occured while making for target: ${target}`,
stack: `${err.message}\n${err.stack}`,
};
} else {
throw new Error(`An unknown error occured while making for target: ${target}`);
}
}
});
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/electron-forge-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ const main = async () => {

dir = await resolveDir(dir);
if (!dir) {
prepareSpinner.fail();
console.error('Failed to locate compilable Electron application'.red);
if (global._resolveError) global._resolveError();
process.exit(1);
// eslint-disable-next-line no-throw-literal
throw 'Failed to locate compilable Electron application';
}

const packageJSON = await readPackageJSON(dir);
Expand Down
28 changes: 13 additions & 15 deletions src/electron-forge-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'colors';
import fs from 'fs-promise';
import path from 'path';
import program from 'commander';
import ora from 'ora';

import './util/terminate';
import asyncOra from './util/ora-handler';
import getForgeConfig from './util/forge-config';
import readPackageJSON from './util/read-package-json';
import requireSearch from './util/require-search';
Expand Down Expand Up @@ -35,9 +35,8 @@ const main = async () => {

dir = await resolveDir(dir);
if (!dir) {
console.error('Failed to locate publishable Electron application'.red);
if (global._resolveError) global._resolveError();
process.exit(1);
// eslint-disable-next-line no-throw-literal
throw 'Failed to locate publishable Electron application';
}

const artifacts = makeResults.reduce((accum, arr) => {
Expand All @@ -51,17 +50,16 @@ const main = async () => {

if (!program.target) program.target = 'github';

const targetSpinner = ora.ora(`Resolving publish target: ${`${program.target}`.cyan}`).start();

const publisher = requireSearch(__dirname, [
`./publishers/${program.target}.js`,
`electron-forge-publisher-${program.target}`,
]);
if (!publisher) {
targetSpinner.fail();
throw new Error(`Could not find a publish target with the name: ${program.target}`);
}
targetSpinner.succeed();
let publisher;
await asyncOra(`Resolving publish target: ${`${program.target}`.cyan}`, async () => {
publisher = requireSearch(__dirname, [
`./publishers/${program.target}.js`,
`electron-forge-publisher-${program.target}`,
]);
if (!publisher) {
throw `Could not find a publish target with the name: ${program.target}`; // eslint-disable-line
}
});

await publisher(artifacts, packageJSON, forgeConfig, program.authToken, program.tag);
};
Expand Down
Loading

0 comments on commit 1b6b727

Please sign in to comment.