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

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. return this.prompter.ask(
  78. `${label} \n ` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${ defaultchoice || choices[0]} ) : `
  79. ).then(choice => {
  80. if(!choice) return defaultchoice || choices[0];
  81. if(choice && isNaN(+choice)) return choice;
  82. return choices[(+choice) - 1];
  83. })
  84. }
  85. const readline = require("readline");
  86. var cli = {
  87. nodeShellExec
  88. , get prompter() {
  89. var prompt_interface = {
  90. ask : function(q){
  91. // Needs to be serialized. Parallel asks are not possible.
  92. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  93. return new Promise((resolve, reject)=>{
  94. clii.question(q, (answer)=>{
  95. clii.close();
  96. console.log("resolve is being called");
  97. resolve(answer)
  98. })
  99. })
  100. }
  101. }
  102. return prompt_interface
  103. }
  104. , prompt
  105. }
  106. module.exports = cli