Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cliverse.js 2.3KB

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