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

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
3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. const child = spawn(...args);
  33. var p = new Promise(function(resolve, reject){
  34. // console.log(...args)
  35. if(!opts.detached) {
  36. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  37. var success = true;
  38. if(opts.stdio !== 'ignore') {
  39. child.stdout.setEncoding('utf8');
  40. child.stderr.setEncoding('utf8');
  41. child.stdout.on('data', (chunk) => {
  42. chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */
  43. process.stdout.write( chunk )
  44. });
  45. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */
  46. console.log('Error exit not handled.')
  47. } );
  48. child.stderr.on('data', (chunk) => {
  49. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  50. messages.push(chunk);
  51. // process.stdout.write( chunk )
  52. // console.error('stderr e: ' + chunk)
  53. });
  54. }
  55. child.on('close', (code) => {
  56. // console.log('Proper close was fired')
  57. var logEntry = { code, success }
  58. if(+code !== 0 || opts.haserrors) { success = false; logEntry = { messages, result: `${opts.title} exited with code ${code}`, success, code }
  59. if(opts.evaluateResult) logEntry = opts.evaluateResult(false, logEntry);
  60. };
  61. if(opts.stdio !== 'ignore') {
  62. logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
  63. logEntry.success = success;
  64. if(opts.evaluateResult) logEntry = opts.evaluateResult(success, logEntry);
  65. if(opts.runas){
  66. // success ? logEntry.success = true : null;
  67. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  68. }
  69. else {
  70. // console.log( messages.join('') )
  71. process.stdout.write( messages.join('') )
  72. }
  73. }
  74. // clearInterval(interval)
  75. if(code !== 0 || opts.haserrors) return reject(logEntry)
  76. resolve(logEntry)
  77. });
  78. }
  79. else {
  80. child.unref()
  81. // clearInterval(interval)
  82. resolve(true);
  83. }
  84. });
  85. p.process = child;
  86. return p;
  87. }
  88. var prompt = function(choices, label, defaultchoice){
  89. // prompt accepts either an array or an object as choices.
  90. var choices = choices || [];
  91. defaultchoice = defaultchoice || choices[0] || choices[Object.keys(choices)[0]]
  92. return this.prompter.ask(
  93. `${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]} ) : `
  94. ).then(choice => {
  95. // propName = promptable.interpret(propValue)
  96. if(!choice) return defaultchoice || choices[0];
  97. if(choice && isNaN(+choice)) return choice;
  98. return choices[(+choice) - 1];
  99. })
  100. }
  101. const readline = require("readline");
  102. var cli = {
  103. nodeShellExec
  104. , get prompter() {
  105. var prompt_interface = {
  106. ask : function(q){
  107. // Needs to be serialized. Parallel asks are not possible.
  108. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  109. return new Promise((resolve, reject)=>{
  110. clii.question(q, (answer)=>{
  111. try {
  112. clii.close();
  113. console.log("readline.createInterface closed");
  114. resolve(answer)
  115. }
  116. catch(e) {
  117. reject(e)
  118. }
  119. })
  120. })
  121. }
  122. }
  123. return prompt_interface
  124. }
  125. , prompt
  126. }
  127. module.exports = cli