Skip to content

Commit

Permalink
refactor(generic): refactor confirm prompts into a helper for interac…
Browse files Browse the repository at this point in the history
…tive mode
  • Loading branch information
MarshallOfSound authored and malept committed Jan 4, 2017
1 parent 0923ac1 commit b495012
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -52,7 +52,8 @@
"gulp-babel": "^6.1.2",
"mocha": "^3.2.0",
"nodemon": "^1.11.0",
"nyc": "^10.0.0"
"nyc": "^10.0.0",
"sinon": "^1.17.7"
},
"babel": {
"env": {
Expand Down
30 changes: 10 additions & 20 deletions src/api/import.js
Expand Up @@ -10,6 +10,7 @@ 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';
import confirmIfInteractive from '../util/confirm-if-interactive';

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

Expand Down Expand Up @@ -42,11 +43,9 @@ export default async (providedOptions = {}) => {
process.exit(1);
}

const confirm = !interactive || (await inquirer.createPromptModule()({
type: 'confirm',
name: 'confirm',
message: `WARNING: We will now attempt to import: "${dir}". This will involve modifying some files, are you sure you want to continue?`,
})).confirm;
// eslint-disable-next-line max-len
const confirm = await confirmIfInteractive(interactive, `WARNING: We will now attempt to import: "${dir}". This will involve modifying some files, are you sure you want to continue?`);

if (!confirm) {
process.exit(1);
}
Expand All @@ -56,21 +55,15 @@ export default async (providedOptions = {}) => {
let packageJSON = await readPackageJSON(dir);
if (packageJSON.config && packageJSON.config.forge) {
console.warn('It looks like this project is already configured for "electron-forge"'.green);
const shouldContinue = !interactive || (await inquirer.createPromptModule()({
type: 'confirm',
name: 'shouldContinue',
message: 'Are you sure you want to continue?',
})).shouldContinue;
const shouldContinue = await confirmIfInteractive(interactive, 'Are you sure you want to continue?');

if (!shouldContinue) {
process.exit(0);
}
}

const shouldChangeMain = interactive ? (await inquirer.createPromptModule()({
type: 'confirm',
name: 'shouldChangeMain',
message: 'Do you want us to change the "main" attribute of your package.json? If you are currently using babel and pointing to a "build" directory say yes.', // eslint-disable-line
})).shouldChangeMain : false;
// eslint-disable-next-line max-len
const shouldChangeMain = await confirmIfInteractive(interactive, 'Do you want us to change the "main" attribute of your package.json? If you are currently using babel and pointing to a "build" directory say yes.', false);
if (shouldChangeMain) {
const { newMain } = await inquirer.createPromptModule()({
type: 'input',
Expand Down Expand Up @@ -105,11 +98,8 @@ export default async (providedOptions = {}) => {
electronName = key;
} else if (buildToolPackages[key]) {
const explanation = buildToolPackages[key];
const shouldRemoveDependency = !interactive || (await inquirer.createPromptModule()({
type: 'confirm',
name: 'shouldRemoveDependency',
message: `Do you want us to remove the "${key}" dependency in package.json? Electron Forge ${explanation}.`,
})).shouldRemoveDependency;
// eslint-disable-next-line max-len
const shouldRemoveDependency = await confirmIfInteractive(interactive, `Do you want us to remove the "${key}" dependency in package.json? Electron Forge ${explanation}.`);

if (shouldRemoveDependency) {
delete packageJSON.dependencies[key];
Expand Down
2 changes: 1 addition & 1 deletion src/api/init.js
Expand Up @@ -45,7 +45,7 @@ export default async (providedOptions = {}) => {
}
}

await initDirectory(dir);
await initDirectory(dir, interactive);
await initGit(dir);
await initStarter(dir, template ? undefined : lintstyle);
await initNPM(dir, template ? undefined : lintstyle);
Expand Down
10 changes: 3 additions & 7 deletions src/init/init-directory.js
@@ -1,13 +1,13 @@
import debug from 'debug';
import fs from 'fs-promise';
import inquirer from 'inquirer';
import logSymbols from 'log-symbols';

import asyncOra from '../util/ora-handler';
import confirmIfInteractive from '../util/confirm-if-interactive';

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

export default async (dir) => {
export default async (dir, interactive) => {
await asyncOra('Initializing Project Directory', async (initSpinner) => {
d('creating directory:', dir);
await fs.mkdirs(dir);
Expand All @@ -16,11 +16,7 @@ export default async (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',
name: 'confirm',
message: `WARNING: The specified path: "${dir}" is not empty, do you wish to continue?`,
});
const confirm = await confirmIfInteractive(interactive, `WARNING: The specified path: "${dir}" is not empty, do you wish to continue?`);
if (!confirm) {
throw 'Cancelled by user'; // eslint-disable-line
}
Expand Down
12 changes: 12 additions & 0 deletions src/util/confirm-if-interactive.js
@@ -0,0 +1,12 @@
import inquirer from 'inquirer';

export default async (interactive, message, defaultValue = true) => {
if (interactive) {
return (await inquirer.createPromptModule()({
type: 'confirm',
name: 'confirm',
message,
})).confirm;
}
return defaultValue;
};
36 changes: 36 additions & 0 deletions test/fast/confirm-if-interactive_spec.js
@@ -0,0 +1,36 @@
import { expect } from 'chai';
import inquirer from 'inquirer';
import sinon from 'sinon';

import confirmIfInteractive from '../../src/util/confirm-if-interactive';

describe('confirm if interactive', () => {
describe('if interactive=true', () => {
let createPromptModuleSpy;

beforeEach(() => {
createPromptModuleSpy = sinon.stub(inquirer, 'createPromptModule');
createPromptModuleSpy.returns(() => Promise.resolve({ confirm: 'resolved' }));
});

it('should call inquirer prompt', async () => {
const val = await confirmIfInteractive(true, 'Please say yes?');
expect(createPromptModuleSpy.callCount).to.equal(1);
expect(val).to.equal('resolved');
});

afterEach(() => {
createPromptModuleSpy.restore();
});
});

describe('if interactive=false', () => {
it('should return true', async () => {
expect(await confirmIfInteractive(false, 'Yolo!')).to.equal(true);
});

it('should return the defaultValue if provided', async () => {
expect(await confirmIfInteractive(false, 'Yolo!', 'default_value')).to.equal('default_value');
});
});
});

0 comments on commit b495012

Please sign in to comment.