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.

nodeshell.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const fs = require('fs')
  2. const { spawn, spawnSync } = require('child_process');
  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. // console.dir(args[2])
  33. console.log('nodeshellexec ::: ')
  34. console.log(`${args[0]} ${args[1].join(' ') } ${args[args.length-1].cwd}`)
  35. const child = spawn(...args);
  36. var p = new Promise(function(resolve, reject){
  37. // console.log(...args)
  38. if(!opts.detached) {
  39. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  40. var success = true;
  41. if(opts.stdio !== 'ignore') {
  42. child.stdout.setEncoding('utf8');
  43. child.stderr.setEncoding('utf8');
  44. child.stdout.on('data', (chunk) => {
  45. chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */
  46. process.stdout.write( chunk )
  47. });
  48. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */
  49. console.error('Error exit not handled.')
  50. });
  51. child.stderr.on('data', (chunk) => {
  52. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  53. messages.push(chunk);
  54. // process.stdout.write( chunk )
  55. // console.error('stderr e: ' + chunk)
  56. });
  57. }
  58. child.on('close', (code) => {
  59. // console.log('Proper close was fired')
  60. var logEntry = { code, success }
  61. if(+code !== 0 || opts.haserrors) {
  62. success = false; logEntry = { messages, result: `${opts.title} exited with code ${code}`, success, code }
  63. };
  64. if(opts.stdio !== 'ignore') {
  65. logEntry = { result: `${opts.title} exited with code ${code}`, messages, code }
  66. logEntry.success = success;
  67. if(opts.evaluateResult) logEntry = opts.evaluateResult(success, logEntry);
  68. if(opts.runas){
  69. // success ? logEntry.success = true : null;
  70. // fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  71. }
  72. else {
  73. // console.log( messages.join('') )
  74. // process.stdout.write( JSON.stringify(logEntry) )
  75. }
  76. }
  77. else if(opts.evaluateResult) {
  78. try { logEntry = opts.evaluateResult(false, logEntry); }
  79. catch(e){ reject(e) }
  80. }
  81. // clearInterval(interval)
  82. if(code !== 0 || opts.haserrors) {
  83. args[2].benign || args[2].ignorefailures ? (logEntry.benign = true, logEntry.ignorefailures = true) : null
  84. return reject(logEntry)
  85. }
  86. resolve(logEntry)
  87. });
  88. }
  89. else {
  90. child.unref()
  91. // clearInterval(interval)
  92. resolve(true);
  93. }
  94. });
  95. p.process = child;
  96. return p;
  97. }
  98. module.exports = {
  99. nodeShellExec
  100. , createTask(task, str){
  101. var tasks = {
  102. getTaskCheckExists : (str)=>{
  103. return (command, options) => {
  104. options = options || {}
  105. return () => {
  106. var p = nodeShellExec.apply(null, [str, [command]])
  107. if (options.ignorefailures) {
  108. return p.then(() => { return true }).catch(e => { // Ignore. Not a major error.
  109. return false;
  110. })
  111. }
  112. else return p.then(() => { return true });
  113. }
  114. }
  115. }
  116. }
  117. return tasks[task](str)
  118. }
  119. }