555
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.

cliverse.js 6.6KB

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