555
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const { spawn, spawnSync } = require('child_process');
  2. const fs = require('fs')
  3. function nodeShellExec() {
  4. var args = Array.from(arguments);
  5. var opts = args[2] = args[2] || {}
  6. opts.title ? null : opts.title = `${args[0]} ${args[1] }`
  7. const child = spawn(...arguments);
  8. // // const spawn = require('child_process').spawn;
  9. // const s = spawn(
  10. // 'C:\\Program Files\\Git\\bin\\sh.exe'
  11. // , ['notepad', 'index'], { cwd: __dirname });
  12. var p = new Promise(function(resolve, reject){
  13. if(!opts.detached) {
  14. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  15. var success = true;
  16. if(opts.stdio !== 'ignore') {
  17. child.stdout.setEncoding('utf8');
  18. child.stderr.setEncoding('utf8');
  19. child.stdout.on('data', (chunk) => { chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */ });
  20. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */ } );
  21. child.stderr.on('data', (chunk) => {
  22. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  23. messages.push(chunk);
  24. // console.error('stderr e: ' + chunk)
  25. });
  26. }
  27. child.on('close', (code) => {
  28. if(+code !== 0 || opts.haserrors) success = false;
  29. if(opts.stdio !== 'ignore') {
  30. var logEntry = { result: `${opts.title} exited with code ${code}`, messages }
  31. logEntry.success = success;
  32. if(opts.runas){
  33. // success ? logEntry.success = true : null;
  34. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  35. }
  36. else {
  37. // console.log( messages.join('') )
  38. process.stdout.write( messages.join('') )
  39. }
  40. }
  41. if(code !== 0 || opts.haserrors) return reject(logEntry)
  42. resolve(logEntry)
  43. });
  44. }
  45. else {
  46. child.unref()
  47. resolve(true);
  48. }
  49. });
  50. p.process = child;
  51. return p;
  52. }
  53. const readline = require("readline");
  54. var cli = {
  55. nodeShellExec
  56. , get prompt() {
  57. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  58. clii.ask = function(q){
  59. return new Promise((resolve, reject)=>{
  60. clii.question(q, (answer)=>{ resolve(answer) })
  61. })
  62. }
  63. return clii
  64. }
  65. }
  66. module.exports = cli