Skip to content
Permalink
Browse files

refactor(generic): refactor confirm prompts into a helper for interac…

…tive mode
  • Loading branch information
MarshallOfSound authored and malept committed Jan 3, 2017
1 parent 0923ac1 commit b495012e7ea14f92470398a993e3c01c717f67ba
Showing with 64 additions and 29 deletions.
  1. +2 −1 package.json
  2. +10 −20 src/api/import.js
  3. +1 −1 src/api/init.js
  4. +3 −7 src/init/init-directory.js
  5. +12 −0 src/util/confirm-if-interactive.js
  6. +36 −0 test/fast/confirm-if-interactive_spec.js
@@ -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": {
@@ -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');

@@ -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);
}
@@ -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',
@@ -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];
@@ -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);
@@ -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);
@@ -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
}
@@ -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;
};
@@ -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.
You can’t perform that action at this time.