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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = { result: `${opts.title} exited with code ${code}`, success, code }};
  50. if(opts.stdio !== 'ignore') {
  51. logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
  52. logEntry.success = success;
  53. if(opts.runas){
  54. // success ? logEntry.success = true : null;
  55. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  56. }
  57. else {
  58. // console.log( messages.join('') )
  59. process.stdout.write( messages.join('') )
  60. }
  61. }
  62. // clearInterval(interval)
  63. if(code !== 0 || opts.haserrors) return reject(logEntry)
  64. resolve(logEntry)
  65. });
  66. }
  67. else {
  68. child.unref()
  69. // clearInterval(interval)
  70. resolve(true);
  71. }
  72. });
  73. // p.process = child;
  74. return p;
  75. }
  76. var prompt = function(choices, label, defaultchoice){
  77. var choices = choices || [];
  78. return this.prompter.ask(
  79. `${label} \n` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${ defaultchoice || choices[0]} ) : `
  80. ).then(choice => {
  81. // propName = promptable.interpret(propValue)
  82. if(!choice) return defaultchoice || choices[0];
  83. if(choice && isNaN(+choice)) return choice;
  84. return choices[(+choice) - 1];
  85. })
  86. }
  87. const readline = require("readline");
  88. var cli = {
  89. nodeShellExec
  90. , get prompter() {
  91. var prompt_interface = {
  92. ask : function(q){
  93. // Needs to be serialized. Parallel asks are not possible.
  94. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  95. return new Promise((resolve, reject)=>{
  96. clii.question(q, (answer)=>{
  97. try {
  98. clii.close();
  99. console.log("readline.createInterface closed");
  100. resolve(answer)
  101. }
  102. catch(e) {
  103. reject(e)
  104. }
  105. })
  106. })
  107. }
  108. }
  109. return prompt_interface
  110. }
  111. , prompt
  112. }
  113. module.exports = cli