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

cliverse.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. var prompt = function(choices, label){
  50. return this.prompter.ask(
  51. `${label} \n ` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${choices[0]} ) : `
  52. ).then(choice => {
  53. if(!choice) return choices[0];
  54. if(choice && isNaN(+choice)) return choice;
  55. return choices[choice + 1];
  56. })
  57. }
  58. const readline = require("readline");
  59. var cli = {
  60. nodeShellExec
  61. , get prompter() {
  62. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  63. clii.ask = function(q){
  64. return new Promise((resolve, reject)=>{
  65. clii.question(q, (answer)=>{ resolve(answer) })
  66. })
  67. }
  68. return clii
  69. }
  70. , prompt
  71. }
  72. module.exports = cli