555
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cliverse.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, selectedchoice){
  89. // prompt accepts either an array or an object as choices.
  90. var choices = choices || [];
  91. defaultchoice = defaultchoice || choices[0] || selectedchoice || choices[Object.keys(choices)[0]]
  92. var ci = 0;
  93. return this.prompter.ask(
  94. `${label} \n` + Object.keys(choices).map(
  95. choice => {
  96. ++ci; var choice_label = isNaN(+choice) ? `${ci}) (${choice})` : ci + ')';
  97. return ` ${choice_label} ${choices[choice]} `
  98. }).join('\n')
  99. + `\n default ( <= ${ defaultchoice || choices[0]} ) : `
  100. + `\n selected ( <= ${ selectedchoice || defaultchoice || choices[0]} ) : `
  101. ).then(choice => {
  102. // propName = promptable.interpret(propValue)
  103. if(!choice) return selectedchoice;
  104. if(!choice) return defaultchoice || choices[0];
  105. if(choice && isNaN(+choice)) return choice;
  106. return choices[(+choice) - 1];
  107. })
  108. }
  109. const readline = require("readline");
  110. var cli = {
  111. nodeShellExec
  112. , get prompter() {
  113. var prompt_interface = {
  114. ask : function(q){
  115. // Needs to be serialized. Parallel asks are not possible.
  116. const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
  117. return new Promise((resolve, reject)=>{
  118. clii.question(q, (answer)=>{
  119. try {
  120. clii.close();
  121. console.log("readline.createInterface closed");
  122. resolve(answer)
  123. }
  124. catch(e) {
  125. reject(e)
  126. }
  127. })
  128. })
  129. }
  130. }
  131. return prompt_interface
  132. }
  133. , prompt
  134. , createTask(task, str){
  135. var tasks = {
  136. getTaskCheckExists : (str)=>{
  137. return (command, options) => {
  138. options = options || {}
  139. return () => {
  140. var p = nodeShellExec.apply(null, [str, [command]])
  141. if (options.ignorefailures) {
  142. return p.then(() => { return true }).catch(e => { // Ignore. Not a major error.
  143. return false;
  144. })
  145. }
  146. else return p.then(() => { return true });
  147. }
  148. }
  149. }
  150. }
  151. return tasks[task](str)
  152. }
  153. }
  154. module.exports = cli