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.

cliverse.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) => {
  20. chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */
  21. process.stdout.write( chunk )
  22. });
  23. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */ } );
  24. child.stderr.on('data', (chunk) => {
  25. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  26. messages.push(chunk);
  27. process.stdout.write( chunk )
  28. // console.error('stderr e: ' + chunk)
  29. });
  30. }
  31. child.on('close', (code) => {
  32. var logEntry = { code, success }
  33. if(+code !== 0 || opts.haserrors) { success = false; logEntry = { result: `${opts.title} exited with code ${code}`, success, code }};
  34. if(opts.stdio !== 'ignore') {
  35. logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
  36. logEntry.success = success;
  37. if(opts.runas){
  38. // success ? logEntry.success = true : null;
  39. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  40. }
  41. else {
  42. // console.log( messages.join('') )
  43. process.stdout.write( messages.join('') )
  44. }
  45. }
  46. if(code !== 0 || opts.haserrors) return reject(logEntry)
  47. resolve(logEntry)
  48. });
  49. }
  50. else {
  51. child.unref()
  52. resolve(true);
  53. }
  54. });
  55. p.process = child;
  56. return p;
  57. }
  58. var prompt = function(choices, label){
  59. return this.prompter.ask(
  60. `${label} \n ` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${choices[0]} ) : `
  61. ).then(choice => {
  62. if(!choice) return choices[0];
  63. if(choice && isNaN(+choice)) return choice;
  64. return choices[choice + 1];
  65. })
  66. }
  67. const readline = require("readline");
  68. var cli = {
  69. nodeShellExec
  70. , get prompter() {
  71. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  72. clii.ask = function(q){
  73. return new Promise((resolve, reject)=>{
  74. clii.question(q, (answer)=>{ resolve(answer) })
  75. })
  76. }
  77. return clii
  78. }
  79. , prompt
  80. }
  81. module.exports = cli