|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- const { spawn, spawnSync } = require('child_process');
- const fs = require('fs')
-
- function nodeShellExec() {
-
- var args = Array.from(arguments);
- if(args.length > 1){
- var opts = args[2] = args[2] || {}
- opts.title ? null : opts.title = `${args[0]} ${args[1] }`
-
- }
- else {
- var opts = {
- title : `${args[0]}`
-
- }
- }
- opts.stdin = 'pipe';
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- console.log('nodeshellexec ::: ')
- console.log(`${args[0]} ${args[1].join(' ') } ${args[args.length-1].cwd}`)
-
- const child = spawn(...args);
- var p = new Promise(function(resolve, reject){
-
- if(!opts.detached) {
- var messages = [];
- var success = true;
-
- if(opts.stdio !== 'ignore') {
- child.stdout.setEncoding('utf8');
- child.stderr.setEncoding('utf8');
- child.stdout.on('data', (chunk) => {
- chunk.trim() === '' ? null : messages.push(chunk);
- process.stdout.write( chunk )
- });
- child.on('error', (chunk) => { success = false; messages.push(chunk);
- console.error('Error exit not handled.')
- });
- child.stderr.on('data', (chunk) => {
- if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
- messages.push(chunk);
-
-
- });
- }
- child.on('close', (code) => {
-
- var logEntry = { code, success }
- if(+code !== 0 || opts.haserrors) {
- success = false; logEntry = { messages, result: `${opts.title} exited with code ${code}`, success, code }
- };
- if(opts.stdio !== 'ignore') {
- logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
- logEntry.success = success;
- if(opts.evaluateResult) logEntry = opts.evaluateResult(success, logEntry);
- if(opts.runas){
-
- fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
- }
- else {
-
-
- }
- }
- else if(opts.evaluateResult) {
- try { logEntry = opts.evaluateResult(false, logEntry); }
- catch(e){ reject(e) }
- }
-
- if(code !== 0 || opts.haserrors) {
- args[2].benign || args[2].ignorefailures ? (logEntry.benign = true, logEntry.ignorefailures = true) : null
- return reject(logEntry)
- }
- resolve(logEntry)
- });
- }
- else {
- child.unref()
-
- resolve(true);
- }
- });
- p.process = child;
- return p;
- }
-
- var prompt = function(choices, label, defaultchoice, selectedchoice){
-
- 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 => {
-
- 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) {
-
- return choice
- }
- }
- return choices[(+choice) - 1];
- })
- }
-
- const readline = require("readline");
- var cli = {
- nodeShellExec
- , get prompter() {
- var prompt_interface = {
- ask : function(q){
-
- 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(task, str){
-
- var tasks = {
- getTaskCheckExists : (str)=>{
- return (command, options) => {
- options = options || {}
- return () => {
- var p = nodeShellExec.apply(null, [str, [command]])
- if (options.ignorefailures) {
- return p.then(() => { return true }).catch(e => {
- return false;
- })
- }
- else return p.then(() => { return true });
- }
- }
- }
- }
- return tasks[task](str)
- }
- }
-
- module.exports = cli
|