選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cliverse.js 2.9KB

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