Comparing version 1.0.0 to 1.0.1
219
index.js
/* jslint esversion: 6 */ | ||
const cin = process.stdin; | ||
const cout = process.stdout; | ||
const fnList = { | ||
prompt: pprompt, | ||
confirm: pconfirm, | ||
multiline: pmultiline | ||
}; | ||
(function() { | ||
// Public functions | ||
function ask(questions) { | ||
"use strict"; | ||
const fnList = { | ||
prompt: pprompt, | ||
confirm: pconfirm, | ||
multiline: pmultiline | ||
}; | ||
let answers = {}; | ||
let i = 0; | ||
function prompt(msg, multiLine){ | ||
return new Promise(function(resolve) { | ||
cout.write(msg); | ||
cin.setEncoding('utf8'); | ||
if (multiLine) { | ||
let buf = []; | ||
cout.write('\n'); | ||
cin.on('data', | ||
function(val){ | ||
if ('\n' == val || '\r\n' == val) { | ||
cin.pause(); | ||
cin.removeAllListeners('data'); | ||
resolve(buf.join('\n')); | ||
buf = null; | ||
} else { | ||
buf.push(val.slice(0,-1)); | ||
} | ||
} | ||
).resume(); | ||
return new Promise(function(resolve, reject) { | ||
function askQuestion(item) { | ||
let key = item.key; | ||
let msg = item.msg; | ||
let fn = fnList[item.fn]; | ||
if (!key) { | ||
throw new Error('A value for `key` must be defined for question '+i); | ||
} | ||
else { | ||
cin.once('data', | ||
function(val){ | ||
cin.pause(); | ||
resolve(val.slice(0,-1)); | ||
} | ||
).resume(); | ||
if (!msg) { | ||
throw new Error('A value for `msg` must be defined for question '+i); | ||
} | ||
}); | ||
} | ||
if (!fn) { | ||
throw new Error('A value for `fn` must be "prompt", "confirm", or "multiline" for question '+i); | ||
} | ||
function confirm(msg) { | ||
return prompt(msg, false).then( | ||
function(val) { | ||
return(/^y|yes|ok|true$/i.test(val)); | ||
if (fn) { | ||
fn(msg, key, answers).then( | ||
function() { | ||
next(); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
} | ||
function multiline(msg) { | ||
return prompt(msg, true); | ||
} | ||
function promiseFn(msg, key, answers, fn) { | ||
answers = answers || {}; | ||
return fn(msg).then( | ||
function(resp) { | ||
answers[key] = resp; | ||
return answers; | ||
function next() { | ||
if (i < questions.length) { | ||
var question = questions[i]; | ||
i++; | ||
askQuestion(question); | ||
} | ||
); | ||
} | ||
else { | ||
resolve(answers); | ||
} | ||
} | ||
function pprompt(msg, key, answers) { | ||
return promiseFn(msg, key, answers, prompt); | ||
} | ||
next(); | ||
}); | ||
} | ||
function pconfirm(msg, key, answers) { | ||
return promiseFn(msg, key, answers, confirm); | ||
} | ||
function prompt(msg, multiLine){ | ||
"use strict"; | ||
return new Promise(function(resolve) { | ||
cout.write(msg); | ||
cin.setEncoding('utf8'); | ||
if (multiLine) { | ||
let buf = []; | ||
cout.write('\n'); | ||
cin.on('data', | ||
function(val){ | ||
if ('\n' == val || '\r\n' == val) { | ||
cin.pause(); | ||
cin.removeAllListeners('data'); | ||
resolve(buf.join('\n')); | ||
buf = null; | ||
} else { | ||
buf.push(val.slice(0,-1)); | ||
} | ||
} | ||
).resume(); | ||
} | ||
else { | ||
cin.once('data', | ||
function(val){ | ||
cin.pause(); | ||
resolve(val.slice(0,-1)); | ||
} | ||
).resume(); | ||
} | ||
}); | ||
} | ||
function pmultiline(msg, key, answers) { | ||
return promiseFn(msg, key, answers, multiline); | ||
} | ||
function confirm(msg) { | ||
"use strict"; | ||
return prompt(msg, false).then( | ||
function(val) { | ||
return(/^y|yes|ok|true$/i.test(val)); | ||
} | ||
); | ||
} | ||
function ask(questions) { | ||
let answers = {}; | ||
let i = 0; | ||
function multiline(msg) { | ||
"use strict"; | ||
return prompt(msg, true); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
function askQuestion(item) { | ||
let key = item.key; | ||
let msg = item.msg; | ||
let fn = fnList[item.fn]; | ||
if (!key) { | ||
throw new Error('A value for `key` must be defined for question '+i); | ||
} | ||
if (!msg) { | ||
throw new Error('A value for `msg` must be defined for question '+i); | ||
} | ||
if (!fn) { | ||
throw new Error('A value for `fn` must be "prompt", "confirm", or "multiline" for question '+i); | ||
} | ||
// Private functions | ||
function promiseFn(msg, key, answers, fn) { | ||
"use strict"; | ||
answers = answers || {}; | ||
return fn(msg).then( | ||
function(resp) { | ||
answers[key] = resp; | ||
return answers; | ||
} | ||
); | ||
} | ||
if (fn) { | ||
fn(msg, key, answers).then( | ||
function() { | ||
next(); | ||
} | ||
); | ||
} | ||
} | ||
function pprompt(msg, key, answers) { | ||
return promiseFn(msg, key, answers, prompt); | ||
} | ||
function next() { | ||
if (i < questions.length) { | ||
var question = questions[i]; | ||
i++; | ||
askQuestion(question); | ||
} | ||
else { | ||
resolve(answers); | ||
} | ||
} | ||
function pconfirm(msg, key, answers) { | ||
return promiseFn(msg, key, answers, confirm); | ||
} | ||
next(); | ||
}); | ||
} | ||
function pmultiline(msg, key, answers) { | ||
return promiseFn(msg, key, answers, multiline); | ||
} | ||
module.exports = { | ||
prompt, | ||
confirm, | ||
multiline, | ||
pprompt, | ||
pconfirm, | ||
pmultiline, | ||
ask | ||
}; | ||
})(); | ||
module.exports = { | ||
ask, | ||
confirm, | ||
multiline, | ||
prompt | ||
}; |
{ | ||
"name": "node-ask", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Command line keyboard entry using promises", | ||
@@ -18,3 +18,7 @@ "main": "index.js", | ||
"author": "Michael G Collins <intervalia@gmail.com> (http://www.intervalia.com/)", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/intervalia/node-ask.git" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8045
4
117
0
100
0