// cliverse.js function isWin(){ return /^win/.test(process.platform) } try { if(isWin()) { var win_verse = require('./win_verse') var shell_verse = win_verse; } else { var lin_verse = require('./lin_verse') var shell_verse = lin_verse; } } catch(e) { console.dir(e); console.error('cli environment detection failed') } const BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]'; shell_verse.getVersion = function getVersion() { return BUILD_VERSION; } console.log('----------------------') // console.dir(c) console.log('----------------------') // process.exit() const fs = require('fs') var {nodeShellExec, createTask} = require('./nodeshell') var prompt = function(choices, label, defaultchoice, selectedchoice){ // prompt accepts either an array or an object as choices. var choices = choices || []; defaultchoice = defaultchoice || choices[0] || selectedchoice || choices[Object.keys(choices)[0]] var ci = 0; return this.prompter.ask( `${label} \n` + Object.keys(choices).map( choice => { ++ci; var choice_label = isNaN(+choice) ? `${ci}) (${choice})` : ci + ')'; return ` ${choice_label} ${choices[choice]} ` }).join('\n') + `\n d) default ( <= ${ defaultchoice || choices[0]} ) : ` + `\n selected ( <= ${ selectedchoice || defaultchoice || choices[0]} ) : ` ).then(choice => { // propName = promptable.interpret(propValue) if(!choice && selectedchoice) return selectedchoice; if(!choice) return defaultchoice || choices[0]; if(choice && isNaN(+choice)) { if(choice === 'd') return defaultchoice || choices[0]; try { return JSON.parse(choice); } catch(e) { // console.error(e) return choice // Treat it as a string if it is not parsable } } return choices[(+choice) - 1]; }) } const readline = require("readline"); var cli = { nodeShellExec , get prompter() { var prompt_interface = { ask : function(q){ // Needs to be serialized. Parallel asks are not possible. const clii = readline.createInterface({ input: process.stdin, output: process.stdout }); return new Promise((resolve, reject)=>{ clii.question(q, (answer)=>{ console.log(answer) try { clii.close(); console.log("readline.createInterface closed"); resolve(answer) } catch(e) { reject(e) } }) }) } } return prompt_interface } , prompt , createTask , shell_verse : shell_verse } module.exports = cli