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 年之前
2 年之前
2 年之前
2 年之前
3 年之前
1 年之前
2 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
2 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. var opts = {
  11. title : `${args[0]}`
  12. // , inherit: true, shell: true
  13. }
  14. }
  15. opts.stdin = 'pipe';
  16. // // const spawn = require('child_process').spawn;
  17. // const s = spawn(
  18. // 'C:\\Program Files\\Git\\bin\\sh.exe'
  19. // , ['notepad', 'index'], { cwd: __dirname });
  20. // var interval = null;
  21. // var t = setTimeout(function(){
  22. // interval = setInterval(function(){
  23. // console.log('Awaiting close : ' + child.spawnargs)
  24. // }, 1500)
  25. // // console.log('Awaiting close : ' + child.spawnargs)
  26. // }, 0)
  27. // child.on('close', (code) => {
  28. // console.error('Prematurely closed even before promise...')
  29. // })
  30. // 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
  31. // 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
  32. // console.dir(args[2])
  33. console.log('nodeshellexec ::: ')
  34. console.log(`${args[0]} ${args[1].join(' ') }`)
  35. const child = spawn(...args);
  36. var p = new Promise(function(resolve, reject){
  37. // console.log(...args)
  38. if(!opts.detached) {
  39. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  40. var success = true;
  41. if(opts.stdio !== 'ignore') {
  42. child.stdout.setEncoding('utf8');
  43. child.stderr.setEncoding('utf8');
  44. child.stdout.on('data', (chunk) => {
  45. chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */
  46. process.stdout.write( chunk )
  47. });
  48. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */
  49. console.log('Error exit not handled.')
  50. } );
  51. child.stderr.on('data', (chunk) => {
  52. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  53. messages.push(chunk);
  54. // process.stdout.write( chunk )
  55. // console.error('stderr e: ' + chunk)
  56. });
  57. }
  58. child.on('close', (code) => {
  59. // console.log('Proper close was fired')
  60. var logEntry = { code, success }
  61. if(+code !== 0 || opts.haserrors) {
  62. success = false; logEntry = { messages, result: `${opts.title} exited with code ${code}`, success, code }
  63. };
  64. if(opts.stdio !== 'ignore') {
  65. logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
  66. logEntry.success = success;
  67. if(opts.evaluateResult) logEntry = opts.evaluateResult(success, logEntry);
  68. if(opts.runas){
  69. // success ? logEntry.success = true : null;
  70. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  71. }
  72. else {
  73. // console.log( messages.join('') )
  74. // process.stdout.write( JSON.stringify(logEntry) )
  75. }
  76. }
  77. else if(opts.evaluateResult) {
  78. try { logEntry = opts.evaluateResult(false, logEntry); }
  79. catch(e){ reject(e) }
  80. }
  81. // clearInterval(interval)
  82. if(code !== 0 || opts.haserrors) {
  83. args[2].benign || args[2].ignorefailures ? (logEntry.benign = true, logEntry.ignorefailures = true) : null
  84. return reject(logEntry)
  85. }
  86. resolve(logEntry)
  87. });
  88. }
  89. else {
  90. child.unref()
  91. // clearInterval(interval)
  92. resolve(true);
  93. }
  94. });
  95. p.process = child;
  96. return p;
  97. }
  98. var prompt = function(choices, label, defaultchoice, selectedchoice){
  99. // prompt accepts either an array or an object as choices.
  100. var choices = choices || [];
  101. defaultchoice = defaultchoice || choices[0] || selectedchoice || choices[Object.keys(choices)[0]]
  102. var ci = 0;
  103. return this.prompter.ask(
  104. `${label} \n` + Object.keys(choices).map(
  105. choice => {
  106. ++ci; var choice_label = isNaN(+choice) ? `${ci}) (${choice})` : ci + ')';
  107. return ` ${choice_label} ${choices[choice]} `
  108. }).join('\n')
  109. + `\n d) default ( <= ${ defaultchoice || choices[0]} ) : `
  110. + `\n selected ( <= ${ selectedchoice || defaultchoice || choices[0]} ) : `
  111. ).then(choice => {
  112. // propName = promptable.interpret(propValue)
  113. if(!choice && selectedchoice) return selectedchoice;
  114. if(!choice) return defaultchoice || choices[0];
  115. if(choice && isNaN(+choice)) {
  116. if(choice === 'd') return defaultchoice || choices[0];
  117. try {
  118. return JSON.parse(choice);
  119. }
  120. catch(e) {
  121. // console.error(e)
  122. return choice // Treat it as a string if it is not parsable
  123. }
  124. }
  125. return choices[(+choice) - 1];
  126. })
  127. }
  128. const readline = require("readline");
  129. var cli = {
  130. nodeShellExec
  131. , get prompter() {
  132. var prompt_interface = {
  133. ask : function(q){
  134. // Needs to be serialized. Parallel asks are not possible.
  135. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  136. return new Promise((resolve, reject)=>{
  137. clii.question(q, (answer)=>{
  138. console.log(answer)
  139. try {
  140. clii.close();
  141. console.log("readline.createInterface closed");
  142. resolve(answer)
  143. }
  144. catch(e) {
  145. reject(e)
  146. }
  147. })
  148. })
  149. }
  150. }
  151. return prompt_interface
  152. }
  153. , prompt
  154. , createTask(task, str){
  155. var tasks = {
  156. getTaskCheckExists : (str)=>{
  157. return (command, options) => {
  158. options = options || {}
  159. return () => {
  160. var p = nodeShellExec.apply(null, [str, [command]])
  161. if (options.ignorefailures) {
  162. return p.then(() => { return true }).catch(e => { // Ignore. Not a major error.
  163. return false;
  164. })
  165. }
  166. else return p.then(() => { return true });
  167. }
  168. }
  169. }
  170. }
  171. return tasks[task](str)
  172. }
  173. }
  174. module.exports = cli