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.0KB

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