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

3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. var choices = choices || [];
  81. return this.prompter.ask(
  82. `${label} \n` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${ defaultchoice || choices[0]} ) : `
  83. ).then(choice => {
  84. // propName = promptable.interpret(propValue)
  85. if(!choice) return defaultchoice || choices[0];
  86. if(choice && isNaN(+choice)) return choice;
  87. return choices[(+choice) - 1];
  88. })
  89. }
  90. const readline = require("readline");
  91. var cli = {
  92. nodeShellExec
  93. , get prompter() {
  94. var prompt_interface = {
  95. ask : function(q){
  96. // Needs to be serialized. Parallel asks are not possible.
  97. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  98. return new Promise((resolve, reject)=>{
  99. clii.question(q, (answer)=>{
  100. try {
  101. clii.close();
  102. console.log("readline.createInterface closed");
  103. resolve(answer)
  104. }
  105. catch(e) {
  106. reject(e)
  107. }
  108. })
  109. })
  110. }
  111. }
  112. return prompt_interface
  113. }
  114. , prompt
  115. }
  116. module.exports = cli