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 7.4KB

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