您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cliverse.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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({
  54. input: process.stdin,
  55. output: process.stdout
  56. });
  57. // clii.on("close", function() { });
  58. return clii
  59. }
  60. }
  61. module.exports = cli