You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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