Skip to content

Commit

Permalink
fix: handle gracefulshutdown during prompting for project dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored and sgtcoolguy committed Oct 20, 2020
1 parent 19fca83 commit 719def7
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions hooks/sdk-fork-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
'use strict';

const fields = require('fields');
const path = require('path');

exports.cliVersion = '>=3.2';
Expand Down Expand Up @@ -52,7 +53,10 @@ exports.init = function (logger, config, cli, _appc) {
return true;
};

let origPrompt = pd.prompt;
const origValidate = pd.validate;
let gracefulShutdown = false;

pd.validate = function (projectDir, callback) {
return origValidate(projectDir, function (err, projectDir) {
if (!err) {
Expand All @@ -62,6 +66,10 @@ exports.init = function (logger, config, cli, _appc) {
projectDir = pd.callback(projectDir);
}

// force overwrite when validate() was called during prompting, otherwise
// the value should be the same
cli.argv['project-dir'] = projectDir;

// now validate the sdk
if (!realValidateCorrectSDK(logger, config, cli, commandName)) {
throw new cli.GracefulShutdown();
Expand All @@ -70,6 +78,51 @@ exports.init = function (logger, config, cli, _appc) {
callback(err, projectDir);
});
};

// SDKs prior to 9.3.0 did not set a prompt correctly on the --project-dir argument and let
// the default prompting occur, if we detect that instance then just redirect
if (commandName === 'clean' && origPrompt === undefined) {
origPrompt = function prompt (callback) {
callback(fields.file({
promptLabel: 'Where is the __project directory__?',
complete: true,
showHidden: true,
ignoreDirs: new RegExp(config.get('cli.ignoreDirs')), // eslint-disable-line security/detect-non-literal-regexp
ignoreFiles: /.*/,
validate: pd.validate
}));
};
}

pd.prompt = callback => {
origPrompt(field => {
field.validate = (projectDir, cb) => {
try {
pd.validate(projectDir, cb);
} catch (err) {
if (err instanceof cli.GracefulShutdown) {
gracefulShutdown = true;
return true;
} else {
cb(err);
}
}
};
const origFieldPrompt = field.prompt.bind(field);
field.prompt = cb => {
origFieldPrompt((err, value) => {
if (err) {
cb(err);
} else if (gracefulShutdown) {
// do nothing!
} else {
cb(null, value);
}
});
};
callback(field);
});
};
}

cli.on('build.config', {
Expand Down

0 comments on commit 719def7

Please sign in to comment.