interactive-cli
Advanced tools
Comparing version 1.1.4 to 1.1.5
131
index.js
@@ -1,2 +0,1 @@ | ||
const _ = require('lodash') | ||
const prompt = require('prompt'); | ||
@@ -58,4 +57,4 @@ const ALPHABET = 'abcdefghijklmnopqrstuvwxyz' | ||
// First two args are irrelevant as they are path to node process and the script file itself | ||
// However, store all other possible arguments before --arg is found | ||
// First two args are irrelevant as they are path to node process and the script file itself | ||
// However, store all other possible arguments before --arg is found | ||
} else if (i >= 2) { | ||
@@ -69,3 +68,3 @@ if (!args['root']) { | ||
const argsStringified = _.map(args, (params, arg) => { | ||
const argsStringified = Object.entries(args).map(([arg, params]) => { | ||
let addition = '' | ||
@@ -91,3 +90,3 @@ if (params.length) { | ||
*/ | ||
function promptOptions (text, optionMap, defaultOptionText = '') { | ||
function promptOptions(text, optionMap, defaultOptionText = '') { | ||
let options = optionMap | ||
@@ -113,27 +112,37 @@ let defaultOption = defaultOptionText | ||
return promptFields() | ||
.then(rawRes => { | ||
const res = rawRes && rawRes.toLowerCase() | ||
.then(rawRes => { | ||
const res = rawRes && rawRes.toLowerCase() | ||
let selectedOption | ||
const i = lettersToNumber(res) | ||
let selectedOption | ||
const i = lettersToNumber(res) | ||
// Only return null if defaultOptionText was specified | ||
if (!res && defaultOption) { | ||
selectedOption = null | ||
} else if (i !== -1 && i < optionKeys.length) { | ||
// As options can be array of keys or an object, return the key of the selections | ||
selectedOption = options instanceof Array ? options[i] : optionKeys[i] | ||
} else if (res === 'q') { | ||
throw new DontContinue() | ||
} else { | ||
console.log('Please select one of the options!') | ||
return promptOptions(text, optionMap, defaultOptionText) | ||
} | ||
// Only return null if defaultOptionText was specified | ||
if (!res && defaultOption) { | ||
selectedOption = null | ||
} else if (i !== -1 && i < optionKeys.length) { | ||
// As options can be array of keys or an object, return the key of the selections | ||
selectedOption = options instanceof Array ? options[i] : optionKeys[i] | ||
} else if (res === 'q') { | ||
throw new DontContinue() | ||
} else { | ||
console.log('Please select one of the options!') | ||
return promptOptions(text, optionMap, defaultOptionText) | ||
} | ||
return selectedOption | ||
}) | ||
return selectedOption | ||
}) | ||
} | ||
const once = func => (() => { | ||
let hasRun = false | ||
return () => { | ||
if (!hasRun) { | ||
hasRun = true | ||
func() | ||
} | ||
} | ||
})() | ||
// make a simple deferred/promise out of the prompt function | ||
const initPrompter = _.once(() => { | ||
const initPrompter = once(() => { | ||
prompt.start(); | ||
@@ -145,33 +154,33 @@ prompt.message = ""; | ||
return promptOptions(initialQuestion, initialOptions) | ||
.catch(err => { | ||
if (err instanceof DontContinue) { | ||
throw new ExitScript() | ||
} | ||
throw err | ||
}) | ||
.then(initialHandler) | ||
.then(() => {console.log("\n" + 'All DONE!')}) | ||
.catch(err => { | ||
if (err.message === 'canceled') { | ||
throw new ExitScript() | ||
} | ||
if (err instanceof ExitScript) { | ||
if (err.message) { | ||
console.log("\n" + err.message) | ||
.catch(err => { | ||
if (err instanceof DontContinue) { | ||
throw new ExitScript() | ||
} | ||
// exit if requested | ||
throw err | ||
}) | ||
.then(initialHandler) | ||
.then(() => { console.log("\n" + 'All DONE!') }) | ||
.catch(err => { | ||
if (err.message === 'canceled') { | ||
throw new ExitScript() | ||
} | ||
} else if (err instanceof DontContinue) { | ||
if (err.message) { | ||
console.log("\n" + err.message) | ||
if (err instanceof ExitScript) { | ||
if (err.message) { | ||
console.log("\n" + err.message) | ||
} | ||
// exit if requested | ||
throw err | ||
} else if (err instanceof DontContinue) { | ||
if (err.message) { | ||
console.log("\n" + err.message) | ||
} | ||
} else { | ||
// report error and restart from beginning | ||
console.error('ERROR:', err.message, err.code, err.stack); | ||
} | ||
} else { | ||
// report error and restart from beginning | ||
console.error('ERROR:', err.message, err.code, err.stack); | ||
} | ||
}) | ||
.then(() => startWith(initialQuestion, initialOptions, initialHandler)) | ||
}) | ||
.then(() => startWith(initialQuestion, initialOptions, initialHandler)) | ||
} | ||
@@ -183,3 +192,3 @@ | ||
} | ||
console.log('THIS SHOULD NEVER APPEAR', err.code, err.message, err) | ||
console.log('UNCAUGHT ERROR BEFORE EXIT', err.code, err.message, err) | ||
} | ||
@@ -196,3 +205,3 @@ | ||
*/ | ||
const promptFields = function(textArg, fieldsArg) { | ||
const promptFields = function (textArg, fieldsArg) { | ||
initPrompter() | ||
@@ -211,6 +220,6 @@ let text | ||
} | ||
return new Promise(function(resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
if (_.isArray(fields)) { | ||
prompt.get(fields, function(err, value) { | ||
if (fields instanceof Array) { | ||
prompt.get(fields, function (err, value) { | ||
if (err) { | ||
@@ -221,4 +230,4 @@ return reject(err) | ||
}); | ||
} else if (_.isString(fields)) { | ||
prompt.get([fields], function(err, value) { | ||
} else if (typeof fields === 'string') { | ||
prompt.get([fields], function (err, value) { | ||
if (err) { | ||
@@ -239,4 +248,4 @@ return reject(err) | ||
const promptToContinue = function(obj) { | ||
return promptFields(['continue? (y/n)']).then(function(res) { | ||
const promptToContinue = function (obj) { | ||
return promptFields(['continue? (y/n)']).then(function (res) { | ||
if (res['continue? (y/n)'].toLowerCase() == 'y') { | ||
@@ -250,3 +259,3 @@ return obj; | ||
const promptPassword = function() { | ||
const promptPassword = function () { | ||
return new Promise((resolve, reject) => { | ||
@@ -253,0 +262,0 @@ prompt.get([{ |
{ | ||
"name": "interactive-cli", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Making it easy to create interactive command line scripts in Node.js", | ||
@@ -20,5 +20,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "^4.17.15", | ||
"prompt": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^2.0.5" | ||
} | ||
} |
13653
1
5
325
1
- Removedlodash@^4.17.15