Skip to content

Commit

Permalink
refactor(generic): add the 'debug' module for standard debug logging
Browse files Browse the repository at this point in the history
add debug logging throughout the utils and init files
  • Loading branch information
MarshallOfSound committed Dec 4, 2016
1 parent 4b7a22e commit 9f4c0b4
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"bluebird": "^3.4.6",
"colors": "^1.1.2",
"commander": "^2.9.0",
"debug": "^2.3.3",
"electron-packager": "^8.3.0",
"electron-winstaller": "^2.5.0",
"fs-promise": "^0.5.0",
Expand Down
9 changes: 8 additions & 1 deletion src/electron-forge-init.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import debug from 'debug';
import path from 'path';
import program from 'commander';

Expand All @@ -9,6 +10,8 @@ import initStarter from './init/init-starter-files';

import './util/terminate';

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

const main = async () => {
let dir = process.cwd();
program
Expand All @@ -25,8 +28,12 @@ const main = async () => {
})
.parse(process.argv);

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

await initDirectory(dir);
await initGit(dir);
Expand Down
4 changes: 2 additions & 2 deletions src/electron-forge-make.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import packager from './electron-forge-package';
import resolveDir from './util/resolve-dir';

const main = async () => {
const resolveSpinner = ora('Resolving Forge Config').start();
const resolveSpinner = ora.ora('Resolving Forge Config').start();
let dir = process.cwd();
program
.version(require('../package.json').version)
Expand Down Expand Up @@ -55,7 +55,7 @@ const main = async () => {
}

for (const target of targets) {
const makeSpinner = ora(`Making for target: ${target.cyan} - On platform: ${process.platform.cyan}`).start();
const makeSpinner = ora.ora(`Making for target: ${target.cyan} - On platform: ${process.platform.cyan}`).start();
let maker;
try {
maker = require(`./makers/${process.platform}/${target}.js`); //eslint-disable-line
Expand Down
4 changes: 2 additions & 2 deletions src/electron-forge-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import getForgeConfig from './util/forge-config';
import resolveDir from './util/resolve-dir';

const main = async () => {
const packagerSpinner = ora('Packaging Application').start();
const packagerSpinner = ora.ora('Packaging Application').start();
let dir = process.cwd();

program
Expand Down Expand Up @@ -71,7 +71,7 @@ const main = async () => {

packagerSpinner.succeed();

const compileSpinner = ora('Compiling Application').start();
const compileSpinner = ora.ora('Compiling Application').start();

const compileCLI = require(path.resolve(dir, 'node_modules/electron-compile/lib/cli.js')); // eslint-disable-line
const { runAsarArchive } = require(path.resolve(dir, 'node_modules/electron-compile/lib/packager-cli.js')); // eslint-disable-line
Expand Down
2 changes: 1 addition & 1 deletion src/electron-forge-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './util/terminate';
import resolveDir from './util/resolve-dir';

const main = async () => {
const startSpinner = ora('Launching Application').start();
const startSpinner = ora.ora('Launching Application').start();
let dir = process.cwd();
program
.version(require('../package.json').version)
Expand Down
10 changes: 8 additions & 2 deletions src/init/init-directory.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import debug from 'debug';
import fs from 'fs-promise';
import inquirer from 'inquirer';
import logSymbols from 'log-symbols';
import mkdirp from 'mkdirp';
import ora from 'ora';
import pify from 'pify';

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

export default async (dir) => {
const initSpinner = ora('Initializing Project Directory').start();
const initSpinner = ora.ora('Initializing Project Directory').start();

mkdirp.sync(dir);
d('creating directory:', dir);
await pify(mkdirp)(dir);

const files = await fs.readdir(dir);
if (files.length !== 0) {
d('found', files.length, 'files in the directory. warning the user');
initSpinner.stop(logSymbols.warning);
const { confirm } = await inquirer.createPromptModule()({
type: 'confirm',
Expand Down
6 changes: 5 additions & 1 deletion src/init/init-git.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { exec } from 'child_process';
import debug from 'debug';
import ora from 'ora';

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

export default async dir =>
new Promise((resolve, reject) => {
const spinner = ora('Initializing Git Repository').start();
const spinner = ora.ora('Initializing Git Repository').start();
d('executing "git init" in directory:', dir);
exec('git init', {
cwd: dir,
}, (err) => {
Expand Down
13 changes: 11 additions & 2 deletions src/init/init-npm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import debug from 'debug';
import fs from 'fs-promise';
import ora from 'ora';
import path from 'path';
import username from 'username';

import installDepList from '../util/install-dependencies';

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

const deps = [];
const devDeps = ['babel-preset-stage-0'];
const exactDevDeps = ['electron-prebuilt-compile'];
Expand All @@ -13,7 +16,7 @@ const airbnDeps = ['eslint', 'eslint-config-airbnb', 'eslint-plugin-import',
'eslint-plugin-jsx-a11y@^2.2.3', 'eslint-plugin-react'];

export default async (dir, lintStyle) => {
const initSpinner = ora('Initializing NPM Module').start();
const initSpinner = ora.ora('Initializing NPM Module').start();
const packageJSON = JSON.parse(await fs.readFile(path.resolve(__dirname, '../../tmpl/package.json'), 'utf8'));
packageJSON.productName = packageJSON.name = path.basename(dir).toLowerCase();
packageJSON.author = await username();
Expand All @@ -26,23 +29,29 @@ export default async (dir, lintStyle) => {
packageJSON.scripts.lint = 'eslint src';
break;
}
d('writing package.json to:', dir);
await fs.writeFile(path.resolve(dir, 'package.json'), JSON.stringify(packageJSON, null, 4));
initSpinner.succeed();

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

try {
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':
default:
d('installing airbnb linting dependencies');
await installDepList(dir, airbnDeps, true);
break;
}
Expand Down
6 changes: 5 additions & 1 deletion src/init/init-standard-fix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import debug from 'debug';
import ora from 'ora';
import { spawn as yarnOrNPMSpawn } from 'yarn-or-npm';

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

const run = dir =>
new Promise((resolve, reject) => {
const child = yarnOrNPMSpawn(['run', 'lint', '--', '--fix'], {
Expand All @@ -14,8 +17,9 @@ const run = dir =>
});

export default async (dir) => {
const initSpinner = ora('Applying Standard Style to JS').start();
const initSpinner = ora.ora('Applying Standard Style to JS').start();
try {
d('executing "standard --fix" in:', dir);
await run(dir);
} catch (err) {
initSpinner.fail();
Expand Down
10 changes: 8 additions & 2 deletions src/init/init-starter-files.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import debug from 'debug';
import fs from 'fs-promise';
import mkdirp from 'mkdirp';
import ora from 'ora';
import path from 'path';
import pify from 'pify';

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

const copy = (source, target) =>
new Promise((resolve, reject) => {
d(`copying "${source}" --> "${target}"`);
let rd;
let wr;
const rejectCleanup = (err) => {
Expand All @@ -21,10 +26,11 @@ const copy = (source, target) =>
});

export default async (dir, lintStyle) => {
const initSpinner = ora('Copying Starter Files').start();
const initSpinner = ora.ora('Copying Starter Files').start();
const tmplPath = path.resolve(__dirname, '../../tmpl');

mkdirp.sync(path.resolve(dir, 'src'));
d('creating directory:', path.resolve(dir, 'src'));
await pify(mkdirp)(path.resolve(dir, 'src'));
const rootFiles = ['_gitignore'];
if (lintStyle === 'airbnb') rootFiles.push('_eslintrc');
const srcFiles = ['index.js', 'index.html'];
Expand Down
4 changes: 4 additions & 0 deletions src/util/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import debug from 'debug';
import fs from 'fs';
import mkdirp from 'mkdirp';
import os from 'os';
import path from 'path';

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

/*
* Let's be real sharing config accross spawned process's must be easier than
Expand All @@ -18,12 +20,14 @@ class BasicConfigStore {

get(key) {
this._load();
d('fetching key', key);
return this._store[key];
}

set(key, value) {
this._load();
this._store[key] = value;
d('setting key:', key, 'to value:', value);
fs.writeFileSync(this._path, JSON.stringify(this._store));
}

Expand Down
10 changes: 9 additions & 1 deletion src/util/install-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import debug from 'debug';
import { spawn as yarnOrNPMSpawn, hasYarn } from 'yarn-or-npm';

import config from './config';

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

export default (dir, deps, areDev = false, exact = false) => {
if (deps.length === 0) return Promise.resolve();
d('installing', JSON.stringify(deps), 'in:', dir, `dev=${areDev},exact=${exact},withYarn=${hasYarn()}`);
if (deps.length === 0) {
d('nothing to install, stopping immediately');
return Promise.resolve();
}
let cmd = ['install'].concat(deps);
if (hasYarn()) {
cmd = ['add'].concat(deps);
Expand All @@ -15,6 +22,7 @@ export default (dir, deps, areDev = false, exact = false) => {
if (!areDev) cmd.push('--save');
}
return new Promise((resolve, reject) => {
d('executing', JSON.stringify(cmd), 'in:', dir);
const child = yarnOrNPMSpawn(cmd, {
cwd: dir,
stdio: config.get('verbose') ? 'inherit' : 'ignore',
Expand Down
11 changes: 10 additions & 1 deletion src/util/resolve-dir.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import debug from 'debug';
import fs from 'fs-promise';
import path from 'path';

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

export default async (dir) => {
let mDir = dir;
let prevDir;
while (prevDir !== mDir) {
prevDir = mDir;
const testPath = path.resolve(mDir, 'package.json');
d('searching for project in', testPath);
if (await fs.exists(testPath)) {
const packageJSON = JSON.parse(await fs.readFile(testPath, 'utf8'));

if (packageJSON.devDependencies && packageJSON.devDependencies['electron-prebuilt-compile']
&& !/[0-9]/.test(packageJSON.devDependencies['electron-prebuilt-compile'][0])) {
global._resolveError = () => console.error('You must depend on an EXACT version of "electron-prebuilt-compile" not a range'.red);
return null;
}
if (packageJSON.config && packageJSON.config.forge) return mDir;

if (packageJSON.config && packageJSON.config.forge) {
d('electron-forge compatible package.json found in', testPath);
return mDir;
}
}
mDir = path.dirname(mDir);
}
Expand Down
31 changes: 31 additions & 0 deletions src/util/terminate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import colors from 'colors';
import debug from 'debug';
import ora from 'ora';

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

process.on('unhandledRejection', (err) => {
process.stdout.write('\n\nAn unhandled rejection has occurred inside Forge:\n');
Expand All @@ -11,3 +15,30 @@ process.on('uncaughtException', (err) => {
console.error(colors.red(err.stack));
process.exit(1);
});

if (process.env.DEBUG && process.env.DEBUG.includes('electron-forge')) {
console.warn('WARNING: DEBUG environment variable detected. Progress indicators will be sent over electron-forge:lifecycle'.red);
ora.ora = (name) => {
const fake = {
start: () => {
d('Process Started:', name);
return fake;
},
fail: () => {
d(`Process Failed: ${name}`.red);
return fake;
},
succeed: () => {
d('Process Succeeded:', name);
return fake;
},
stop: () => {
d('Process Stopped:', name);
return fake;
},
};
return fake;
};
} else {
ora.ora = ora;
}

0 comments on commit 9f4c0b4

Please sign in to comment.