555
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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const { spawn, spawnSync } = require('child_process');
  2. const fs = require('fs')
  3. function nodeShellExec() {
  4. var args = Array.from(arguments);
  5. if(args.length > 1){
  6. var opts = args[2] = args[2] || {}
  7. opts.title ? null : opts.title = `${args[0]} ${args[1] }`
  8. }
  9. else {
  10. opts = {
  11. title : `${args[0]}`
  12. // , inherit: true, shell: true
  13. }
  14. }
  15. // // const spawn = require('child_process').spawn;
  16. // const s = spawn(
  17. // 'C:\\Program Files\\Git\\bin\\sh.exe'
  18. // , ['notepad', 'index'], { cwd: __dirname });
  19. // var interval = null;
  20. // var t = setTimeout(function(){
  21. // interval = setInterval(function(){
  22. // console.log('Awaiting close : ' + child.spawnargs)
  23. // }, 1500)
  24. // // console.log('Awaiting close : ' + child.spawnargs)
  25. // }, 0)
  26. // child.on('close', (code) => {
  27. // console.error('Prematurely closed even before promise...')
  28. // })
  29. // D:\chess\instances\elixir_01\elxr/.elxr/run-1630002739610/download.bat https://github.com/git-for-windows/git/releases/download/v2.33.0.windows.2/Git-2.33.0.2-64-bit.exe D:\\chess\\instances\\elixir_01\\elxr/Downloads/Git-2.33.0.2-64-bit.exe
  30. // D:\\chess\\instances\\elixir_01\\elxr/.elxr/run-1630002923584/download.bat https://github.com/git-for-windows/git/releases/download/v2.33.0.windows.2/Git-2.33.0.2-64-bit.exe D:\\chess\\instances\\elixir_01\\elxr/Downloads/Git-2.33.0.2-64-bit.exe
  31. var p = new Promise(function(resolve, reject){
  32. // console.log(...args)
  33. const child = spawn(...args);
  34. if(!opts.detached) {
  35. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  36. var success = true;
  37. if(opts.stdio !== 'ignore') {
  38. child.stdout.setEncoding('utf8');
  39. child.stderr.setEncoding('utf8');
  40. child.stdout.on('data', (chunk) => {
  41. chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */
  42. process.stdout.write( chunk )
  43. });
  44. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */
  45. console.log('Error exit not handled.')
  46. } );
  47. child.stderr.on('data', (chunk) => {
  48. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  49. messages.push(chunk);
  50. // process.stdout.write( chunk )
  51. // console.error('stderr e: ' + chunk)
  52. });
  53. }
  54. child.on('close', (code) => {
  55. // console.log('Proper close was fired')
  56. var logEntry = { code, success }
  57. if(+code !== 0 || opts.haserrors) { success = false; logEntry = { messages, result: `${opts.title} exited with code ${code}`, success, code }
  58. if(opts.evaluateResult) logEntry = opts.evaluateResult(false, logEntry);
  59. };
  60. if(opts.stdio !== 'ignore') {
  61. logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
  62. logEntry.success = success;
  63. if(opts.evaluateResult) logEntry = opts.evaluateResult(success, logEntry);
  64. if(opts.runas){
  65. // success ? logEntry.success = true : null;
  66. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  67. }
  68. else {
  69. // console.log( messages.join('') )
  70. process.stdout.write( messages.join('') )
  71. }
  72. }
  73. // clearInterval(interval)
  74. if(code !== 0 || opts.haserrors) return reject(logEntry)
  75. resolve(logEntry)
  76. });
  77. }
  78. else {
  79. child.unref()
  80. // clearInterval(interval)
  81. resolve(true);
  82. }
  83. });
  84. // p.process = child;
  85. return p;
  86. }
  87. var prompt = function(choices, label, defaultchoice){
  88. // prompt accepts either an array or an object as choices.
  89. var choices = choices || [];
  90. defaultchoice = defaultchoice || choices[0] || choices[Object.keys(choices)[0]]
  91. return this.prompter.ask(
  92. `${label} \n` + Object.keys(choices).map(choice => { var choice_label = isNaN(+choice) ? choice : ((+choice) + 1); return ` ${choice_label}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${ defaultchoice || choices[0]} ) : `
  93. ).then(choice => {
  94. // propName = promptable.interpret(propValue)
  95. if(!choice) return defaultchoice || choices[0];
  96. if(choice && isNaN(+choice)) return choice;
  97. return choices[(+choice) - 1];
  98. })
  99. }
  100. const readline = require("readline");
  101. var cli = {
  102. nodeShellExec
  103. , get prompter() {
  104. var prompt_interface = {
  105. ask : function(q){
  106. // Needs to be serialized. Parallel asks are not possible.
  107. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  108. return new Promise((resolve, reject)=>{
  109. clii.question(q, (answer)=>{
  110. try {
  111. clii.close();
  112. console.log("readline.createInterface closed");
  113. resolve(answer)
  114. }
  115. catch(e) {
  116. reject(e)
  117. }
  118. })
  119. })
  120. }
  121. }
  122. return prompt_interface
  123. }
  124. , prompt
  125. }
  126. module.exports = cli