|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- const { spawn, spawnSync } = require('child_process');
- const fs = require('fs')
-
- function nodeShellExec() {
-
- var args = Array.from(arguments);
- var opts = args[2] = args[2] || {}
- opts.title ? null : opts.title = `${args[0]} ${args[1] }`
- const child = spawn(...arguments);
-
- 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); });
- child.on('error', (chunk) => { success = false; messages.push(chunk); } );
- 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) => {
- if(+code !== 0 || opts.haserrors) success = false;
- if(opts.stdio !== 'ignore') {
- var logEntry = { result: `${opts.title} exited with code ${code}`, messages }
- logEntry.success = success;
-
- if(opts.runas){
-
- fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
- }
- else {
-
- process.stdout.write( messages.join('') )
- }
- }
- if(code !== 0 || opts.haserrors) return reject(logEntry)
- resolve(logEntry)
- });
- }
- else {
- child.unref()
- resolve(true);
- }
- });
- p.process = child;
- return p;
- }
-
- const readline = require("readline");
-
- var cli = {
- nodeShellExec
- , get prompt() {
- const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
- clii.ask = function(q){
- return new Promise((resolve, reject)=>{
- clii.question(q, (answer)=>{ resolve(answer) })
- })
- }
- return clii
- }
- }
-
- module.exports = cli
|