Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-ask

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-ask - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

LICENSE.md

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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc