Skip to content

Commit

Permalink
Prompt user for a yesno before file ops (#29)
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
sthzg committed Aug 6, 2016
1 parent 59dc04e commit f2d1689
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
67 changes: 61 additions & 6 deletions utils/tasks.js
Expand Up @@ -66,8 +66,12 @@ function injectDefaultConstructor(generator) {

generator.option('host', {
desc: 'Name of the host generator',
type: 'String',
required: true
type: 'String'
});

generator.option('yes', {
desc: 'Confirm all yesnos with yes',
type: 'Boolean'
});

}
Expand Down Expand Up @@ -116,8 +120,34 @@ function makeSubgenAware(generator) {
*/
function activateSubgen(generator) {
if (!fs.existsSync(generator.subgenDest)) {
fs.symlinkSync(generator.subgenSrc, generator.subgenDest);
generator.genData.activation = utils.buildSuccess({});
var done = generator.async();

const run = (shouldProceed) => {

if (!shouldProceed) {
generator.env.error('Operation cancelled by user.');
done();
}

fs.symlinkSync(generator.subgenSrc, generator.subgenDest);
generator.genData.activation = utils.buildSuccess({});

done();
};

if (utils.shouldAutoConfirmYesnos(generator.options.yes)) {
run(true);
} else {
generator.prompt({
type: 'confirm',
name: 'proceed',
message: `I will create a symlink\nfrom =>\t${generator.subgenSrc}\nin =>\t${generator.subgenDest}\nConfirm to proceed:`
})
.then(
answers => run(answers.proceed)
);
}

} else {
const err = 'Subgen with name ' + generator.subgenName + ' is already activated. If you want to update it, make ' +
'sure to deactivate it first. This is a NOOP.';
Expand All @@ -132,8 +162,33 @@ function activateSubgen(generator) {
*/
function deactivateSubgen(generator) {
if (fs.existsSync(generator.subgenDest)) {
fs.unlinkSync(generator.subgenDest);
generator.genData.deactivation = utils.buildSuccess({});
var done = generator.async();

const run = (shouldProceed) => {

if (!shouldProceed) {
generator.env.error('Operation cancelled by user.');
done();
}

fs.unlinkSync(generator.subgenDest);
generator.genData.deactivation = utils.buildSuccess({});

done();
};

if (utils.shouldAutoConfirmYesnos(generator.options.yes)) {
run(true);
} else {
generator.prompt({
type: 'confirm',
name: 'proceed',
message: `I will remove this symlink\n=> ${generator.subgenDest}\nConfirm to proceed:`
})
.then(
answers => run(answers.proceed)
);
}
} else {
const err = `Subgen with name "${generator.subgenName}" doesn't seem to be activated. This is a NOOP.`;
generator.genData.deactivation = utils.buildError(err);
Expand Down
6 changes: 6 additions & 0 deletions utils/utils.js
Expand Up @@ -206,6 +206,11 @@ function findExternalSubgens(prefixes, host, installed) {
}


function shouldAutoConfirmYesnos(bool) {
return bool || false;
}


/**
* Returns package.json from a package's install path
* @param dir base path of the installed package
Expand Down Expand Up @@ -266,5 +271,6 @@ module.exports = {
getSubgenBaseName,
findExternalSubgens,
populatePkgStoreFromPaths,
shouldAutoConfirmYesnos,
sortCharArrByLength
};

0 comments on commit f2d1689

Please sign in to comment.