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.

index.js 21KB

6 vuotta sitten
4 vuotta sitten
5 vuotta sitten
6 vuotta sitten
4 vuotta sitten
4 vuotta sitten
6 vuotta sitten
5 vuotta sitten
6 vuotta sitten
5 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
6 vuotta sitten
5 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
5 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 vuotta sitten
5 vuotta sitten
6 vuotta sitten
5 vuotta sitten
5 vuotta sitten
6 vuotta sitten
5 vuotta sitten
6 vuotta sitten
6 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. 
  2. // --------------
  3. // elxr
  4. // A cli tool for elixr.
  5. const { existsSync } = require('fs');
  6. const fs = require('fs')
  7. const { spawn, spawnSync } = require('child_process');
  8. const cliargs = require('../elxr/cliargs'); // Use minimist...
  9. const processedArgs = cliargs(process.argv.slice(2));
  10. console.dir(processedArgs)
  11. var path = require('path')
  12. // Serialize a set of functions that will execute to return a promises one after the other.
  13. // Will stop when any one fails.
  14. function any(iterable, continueOnFailure) {
  15. return iterable.reduce(
  16. (p, fn, i ,a) => {
  17. // console.log('accumulator :');
  18. // console.log(p);
  19. if(Promise.resolve(p) === p ) {
  20. return p.then((pVal) => {
  21. if(Promise.resolve(pVal) === pVal) {
  22. // Passed in function retured a promise. We still need to wait for it.
  23. pVal.then((pVal)=>{
  24. // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p);
  25. if(!pVal && !continueOnFailure) {
  26. console.error("E : pVal :" + pVal);
  27. // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]);
  28. // a.map((i)=>console.error(i.toString()));
  29. console.error('debugData 3 -------------------');
  30. // console.error(debugData);
  31. console.error('debugData 3 -------------------');
  32. console.log("Cancelling remaining...");
  33. throw 'Failed in reduce 1 '
  34. return false;
  35. }
  36. // console.log('calling fn : '+ fn);
  37. return (Promise.resolve(fn) === fn ) ? fn : fn() ;
  38. })
  39. }
  40. else {
  41. // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p);
  42. if(!pVal && !continueOnFailure) {
  43. console.error("E : pVal :" + pVal);
  44. // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]);
  45. // a.map((i)=>console.error(i.toString()));
  46. console.error('debugData 3 -------------------');
  47. // console.error(debugData);
  48. console.error('debugData 3 -------------------');
  49. console.log("Cancelling remaining...");
  50. throw 'Failed in reduce 1 '
  51. return false;
  52. }
  53. // console.log('calling fn : '+ fn);
  54. return (Promise.resolve(fn) === fn ) ? fn : fn() ;
  55. }
  56. }).catch((error) => {
  57. console.error("E : " + error);
  58. fn ? console.error("Fn : " + fn.toString()) : null;
  59. // i==0 ? null : console.error("E :2 a[i-1] " + a[i-1]);
  60. // a.map((i)=>console.error(i.toString()));
  61. console.error('debugData 4-------------------------');
  62. // console.error(debugData);
  63. console.error('debugData 4-------------------------');
  64. throw 'Failed in reduce 2 '
  65. return false;
  66. })
  67. }
  68. else if(!p) {
  69. console.log("Bypass on failure");
  70. return false;
  71. }
  72. }
  73. , Promise.resolve(true)
  74. );
  75. }
  76. var isRunningElevated = ()=>{
  77. return nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
  78. inherit : true, shell: true
  79. , env: process.env
  80. , title : `check privileged execution mode using fsutil dirty query C:`
  81. }).then((exitcode)=>{
  82. console.log('Elevated')
  83. return true;
  84. }).catch(()=>{
  85. console.log('Not Elevated');
  86. throw false;
  87. });
  88. }
  89. var cli = 'elxr';
  90. var ver = '#unversioned';
  91. var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
  92. // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
  93. // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
  94. var dbForLabel = function(label){
  95. var dbsForLabel = {
  96. devmysql : 'mysql'
  97. , development : 'mssql'
  98. , production : 'mssql'
  99. }
  100. return dbsForLabel[label] || 'mysql'
  101. }
  102. // Relevant git repos
  103. var gitRepos = [
  104. 'ember-masonry-grid'
  105. , 'bbhverse'
  106. , 'clientverse'
  107. , 'serververse'
  108. , 'client'
  109. // , 'client/server'
  110. , 'elxr'
  111. , 'elixir-client'
  112. , 'elixir-client-unlinked'
  113. , 'ember-searchable-select'
  114. , 'loopback-component-jsonapi'
  115. , 'elixir-config'
  116. , 'cihsr-config'
  117. , 'cihsr-data'
  118. , 'elixir-data'
  119. , 'loopback-connector-ds'
  120. , 'chess-server-lib'
  121. , 'setup'
  122. ]
  123. // Repositiories that have symlinks that required elevated priviletes in windows to create symlinks
  124. //
  125. var elevatedRunasRepos = [
  126. 'elixir-server'
  127. , 'cihsr-server'
  128. , 'chess-server'
  129. ]
  130. var env = Object.assign({}, process.env); // Shallow clone it.
  131. var __runcmd = function(label){
  132. var op = {
  133. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  134. , 'upgrade' : ()=>{
  135. console.log('upgrade.......')
  136. var tasks = [
  137. ()=>{
  138. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  139. inherit : true, shell: true
  140. , env: process.env
  141. }).catch((e)=>{ console.error(e) })
  142. p.position = 1;
  143. console.log('One')
  144. return p;
  145. }
  146. , ()=>{
  147. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  148. inherit : true, shell: true
  149. , env: process.env
  150. }).catch((e)=>{ console.error(e) })
  151. p.position = 2;
  152. console.log('Two')
  153. return p;
  154. }
  155. , ()=>{
  156. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  157. inherit : true, shell: true
  158. , env: process.env
  159. }).catch((e)=>{ console.error(e) })
  160. p.position = 3;
  161. console.log('Three')
  162. return p;
  163. }
  164. , ()=>{
  165. var p = nodeShellExec('n', ['latest'], {
  166. inherit : true, shell: true
  167. , env: process.env
  168. }).catch((e)=>{ console.error(e) })
  169. p.position = 4;
  170. console.log('Four')
  171. return p;
  172. }
  173. ]
  174. any(tasks)
  175. console.log('.......done')
  176. console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
  177. console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
  178. console.info('Uninstalling existing ember globally') ;
  179. var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
  180. stdio: ['pipe', process.stdout, process.stderr],
  181. inherit : true,
  182. shell: true,
  183. cwd : path.dirname(__dirname),
  184. env: env
  185. })
  186. step1.on('close', ()=>{
  187. console.info('Installing ember globally') ;
  188. var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
  189. stdio: ['pipe', process.stdout, process.stderr],
  190. inherit : true,
  191. shell: true,
  192. cwd : path.dirname(__dirname),
  193. env: env
  194. })
  195. step2.on('close', ()=>{
  196. nodeShellExec('cmd', ['/c', 'ember', '--version'], {
  197. stdio: ['pipe', process.stdout, process.stderr],
  198. inherit : true,
  199. shell: true,
  200. cwd : path.dirname(__dirname),
  201. env: env
  202. })
  203. })
  204. })
  205. }
  206. , 'runas' : ()=>{
  207. return isRunningElevated().then(
  208. () => op[ processedArgs.label || processedArgs._[0] || 'h']()
  209. )
  210. .catch(()=>{
  211. console.log('Not Elevated');
  212. console.log('Requesting Elevated Privileges')
  213. nodeShellExec('MSHTA', [`javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr ${processedArgs._[0]} ${processedArgs._[1]} ${processedArgs._[2]} --runas=true', '', 'runas', 1);close();`]
  214. , {
  215. // inherit : true
  216. // , shell: true
  217. env: process.env
  218. , title : `runas`
  219. }
  220. );
  221. })
  222. }
  223. , 'push' : ()=>{
  224. if(!processedArgs._[1]) { console.error('push all not supported. Specify repo name'); return }
  225. // init remote bare from local
  226. // pushandinitremotebare
  227. // https://www.jeffgeerling.com/blogs/jeff-geerling/push-your-git-repositories
  228. // connect to repo server -- net use 172.16.0.27
  229. // cd 172.16.0.27/repos/
  230. // mkdir repo.git
  231. // cd repo.git
  232. // git init --bare
  233. // cd localrepo
  234. // git remote rename origin githubclone
  235. // git remote add origin //172.16.0.27/repos/repo.git
  236. // git push origin master
  237. var repo = processedArgs._[1];
  238. var defaultRepoServer = '//172.16.0.27/repos'
  239. var sequentialTaskShellCommands = [];
  240. if(!existsSync(`Z:/${repo}.git`)){
  241. sequentialTaskShellCommands = [
  242. // ['net', ['use', 'Z:', defaultRepoServer.replace('/','\\')], {
  243. // inherit : true, shell: true
  244. // , env: process.env
  245. // }]
  246. ['pwd', { cwd : 'Z:', inherit : true}]
  247. , ['mkdir', [`${repo}.git`], {
  248. cwd : `Z:`
  249. , inherit : true, shell: true
  250. , env: process.env
  251. }]
  252. , ['pwd', { cwd : `Z:/${repo}.git`, inherit : true}]
  253. , ['git', ['init', '--bare'], { cwd : `Z:/${repo}.git`
  254. , inherit : true, shell: true
  255. , env: process.env
  256. }]
  257. , ['git', ['remote', 'rename', 'origin', 'githubclone'], { cwd : `${repo}`}, (err)=>{
  258. console.log('Ignoring origin rename error : ' + err); return true; //return true to continue.
  259. } ] // PB ; Todo -- new repositories created locally will not have origin. Handle this failure.
  260. , ['git', ['remote', 'add', 'origin', `${defaultRepoServer}/${repo}.git`], { cwd : `${repo}`}]
  261. ]
  262. if(!existsSync(`Z:`)){
  263. sequentialTaskShellCommands.splice(0,0, ['net', ['use', 'Z:', defaultRepoServer.replace(/\//gm,'\\')], {
  264. inherit : true, shell: true
  265. , env: process.env
  266. }])
  267. console.warn('Adding network drive z: for repo server. ' + sequentialTaskShellCommands[0])
  268. // throw 'done'
  269. }
  270. }
  271. sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd : `${repo}`}])
  272. // console.dir(sequentialTaskShellCommands);
  273. var tasks = [];
  274. sequentialTaskShellCommands.forEach(shellcmd => {
  275. // console.log(shellcmd)
  276. tasks.push(()=>{
  277. var p = nodeShellExec.apply(null, shellcmd.slice(0,3)).catch((e)=>{ if(shellcmd[3]) { return shellcmd[3]() } else { console.error(e);} })
  278. return p;
  279. })
  280. })
  281. any(tasks);
  282. }
  283. , 'pull' : (label) => {
  284. var env = Object.assign({}, process.env); // Shallow clone it.
  285. // console.dir(env)
  286. console.log('Running exlr pull : ' + path.dirname(__dirname))
  287. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  288. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  289. // stdio: ['pipe', process.stdout, process.stderr],
  290. // inherit : true,
  291. // shell: true,
  292. // cwd : path.dirname(__dirname),
  293. // env: env
  294. // })
  295. var performPull = repo => {
  296. if(existsSync(repo)) {
  297. console.log('pulling ' + repo)
  298. nodeShellExec('git', ['pull'], {
  299. inherit : true, shell: true,
  300. cwd : repo
  301. // , env: process.env
  302. , title : `git pull ${repo}`
  303. }).catch((e)=>{ console.error(e) })
  304. }
  305. else {
  306. console.log('cloning ' + repo)
  307. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + repo + '.git'],
  308. {
  309. inherit : true, shell: true,
  310. env: process.env
  311. , title : `git clone ${'//172.16.0.27/repos/' + repo + '.git'}`
  312. }).catch((e)=>{ console.error(e) })
  313. }
  314. }
  315. if(!processedArgs.runas) gitRepos.forEach(performPull)
  316. return isRunningElevated().then(
  317. ()=> elevatedRunasRepos.forEach(performPull))
  318. .catch(
  319. () => op['runas']()
  320. )
  321. }
  322. , 'npmi' : ()=>{
  323. var tasks = [];
  324. gitRepos.forEach(repo => {
  325. console.log('npm i for ' + repo)
  326. // nodeShellExec('pwd', [], {
  327. // // inherit : true, shell: true
  328. // cwd : repo
  329. // // , env: process.env
  330. // , title : `pwd for ${repo}`
  331. // }).catch((e)=>{ console.error(e) })
  332. nodeShellExec('rm', ['package-lock.json'], {
  333. inherit : true, shell: true
  334. , cwd : repo
  335. , env: process.env
  336. , title : `rm 'package-lock.json' for ${repo}`
  337. }).catch((e)=>{ console.error(e) })
  338. tasks.push(()=>{
  339. var p = nodeShellExec('npm', ['i'], {
  340. inherit : true, shell: true
  341. , cwd : repo
  342. , env: process.env
  343. , title : `npm i for ${repo}`
  344. }).catch((e)=>{ console.error(e) })
  345. return p;
  346. })
  347. })
  348. any(tasks);
  349. }
  350. , 'start' : (label)=>{
  351. console.log('Starting Elixir Server.');
  352. var env = Object.assign({}, process.env); // Shallow clone it.
  353. // console.dir(env)
  354. env.NODE_ENV = label || 'development';
  355. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  356. var cmd = env.NODE_ENV === 'development' ? 'nodemon' : 'node';
  357. // cmd = 'node'
  358. var childPromise = nodeShellExec(cmd, ['--inspect=9228', 'elixir/server.js'], {
  359. // inherit : true,
  360. shell: true,
  361. detached: true,
  362. stdio: 'ignore',
  363. cwd : 'elixir-server'
  364. , env: env
  365. })
  366. var child = childPromise.process;
  367. if (typeof child.pid !== 'undefined') {
  368. fs.writeFileSync('.elixir-server.elixir.server.pid', child.pid, {
  369. encoding: 'utf8'
  370. })
  371. }
  372. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  373. // inherit : true,
  374. // shell: true, detached: true,
  375. // cwd : 'qms/server',
  376. // env: env,
  377. // shell : true
  378. // })
  379. // nodeShellExec('ember', ['s'], {
  380. // // inherit : true,
  381. // shell: true, detached: true,
  382. // cwd : 'client/',
  383. // env: env
  384. // })
  385. var childPromise = nodeShellExec('ember', ['s'], {
  386. // var childPromise = nodeShellExec('node', ['--inspect=9227', './node_modules/.bin/ember', 's'], {
  387. // PB : TODO -- ember debugging.
  388. // inherit : true,
  389. shell: true,
  390. detached: true,
  391. stdio: 'ignore',
  392. cwd : 'client'
  393. , env: env
  394. })
  395. // .catch(e=>console.error(e))
  396. child = childPromise.process;
  397. if (typeof child.pid !== 'undefined') {
  398. fs.writeFileSync('.client.server.pid', child.pid, {
  399. encoding: 'utf8'
  400. })
  401. }
  402. }
  403. , 'stop' : (label)=>{
  404. const kill = require('tree-kill');
  405. var serverPid = fs.readFileSync('.elixir-server.elixir.server.pid', {
  406. encoding: 'utf8'
  407. })
  408. fs.unlinkSync('.elixir-server.elixir.server.pid')
  409. console.log(serverPid)
  410. kill(serverPid)
  411. serverPid = fs.readFileSync('.client.server.pid', {
  412. encoding: 'utf8'
  413. })
  414. fs.unlinkSync('.client.server.pid')
  415. console.log(serverPid)
  416. kill(serverPid)
  417. }
  418. , 'use' : ()=>{
  419. var runconfig = { NODE_ENV : process.env.NODE_ENV }
  420. try {
  421. runconfig = Object.assign(runconfig, require('../run.js'))
  422. } catch(e) { }
  423. if(runconfig.NODE_ENV === process.env.NODE_ENV && processedArgs._[1] && runconfig.use === processedArgs._[1]) return;
  424. var tasks = [
  425. ()=>{
  426. var p = nodeShellExec('rm', ['config']
  427. , { inherit : true, shell: true, env: process.env }
  428. ).catch((err)=>{ console.log('Ignoring benign error : ' + err); return true; })
  429. return p;
  430. },
  431. ()=>{
  432. var p = nodeShellExec('rm', ['data'], { inherit : true, shell: true, env: process.env }
  433. ).catch((err)=>{ console.log('Ignoring benign error : ' + err); return true; })
  434. return p;
  435. }
  436. ];
  437. any(tasks)
  438. if(!processedArgs.runas) return op['runas']()
  439. process.env.NODE_ENV = process.env.NODE_ENV || 'development';
  440. if(processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1];
  441. if(!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' }
  442. fs.writeFileSync('../run.js', 'module.exports = ' + JSON.stringify(runconfig))
  443. tasks = [
  444. ()=>{
  445. var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-' + process.env.NODE_ENV + '-config'], {
  446. inherit : true, shell: true
  447. , env: process.env
  448. }).catch((e)=>{ console.error(e) })
  449. return p;
  450. }
  451. ];
  452. if(processedArgs._[1]) {
  453. tasks.concat(
  454. [
  455. ()=>{
  456. var p = nodeShellExec('mklink', ['/D', 'data', runconfig.use + '-data'], {
  457. inherit : true, shell: true
  458. , env: process.env
  459. }).catch((e)=>{ console.error(e) })
  460. return p;
  461. }
  462. ]
  463. )
  464. }
  465. any(tasks)
  466. // Antibiotic stewardship program.
  467. // 1st use is fine.
  468. // Max vials dispense
  469. // 2nd use Pharmacy needs justification Form.
  470. // Approval after a certain period of time.
  471. }
  472. , 'g' : ()=>{
  473. if(processedArgs.h) {
  474. console.log('elxr g [modelname] => generate a model named [modelname]');
  475. console.log('elxr g => regenerate all known models');
  476. return
  477. }
  478. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  479. // console.log('Starting directory: ' + process.cwd());
  480. // try {
  481. // child = child.on('close', () => { process.chdir(label) } );
  482. // console.log('New directory: ' + process.cwd());
  483. // }
  484. // catch (err) {
  485. // console.log('chdir: ' + err);
  486. // }
  487. // child.on('close', function(){
  488. // var options = {
  489. // shell : true
  490. // , inherit : true
  491. // // , cwd : '' + process.cwd
  492. // // , env : process.env
  493. // };
  494. // nodeShellExec('git', ['init'], { inherit : true});
  495. if(0){
  496. // PB : TODO -- Special google chrome profile for tests etc.
  497. nodeShellExec('pwd', { inherit : true});
  498. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  499. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  500. "--profile-directory=Profile 1"
  501. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  502. }
  503. // nodeShellExec('npm', ['init', '-y'], options);
  504. // nodeShellExec('npm', ['init', '-y'], options);
  505. // })
  506. var g = {
  507. 'client' : ()=>{
  508. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  509. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  510. stdio: ['pipe', process.stdout, process.stderr],
  511. inherit : true,
  512. shell: true,
  513. cwd : path.dirname(__dirname),
  514. env: env
  515. })
  516. }
  517. }
  518. g[processedArgs._[1]]();
  519. }
  520. }
  521. return op[label] ? op[label]() : op['start'](label);
  522. }
  523. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  524. // mysql -u root -p db_name < drop_all_tables.sql
  525. var mysql = '../xampp/mysql/bin/mysql'
  526. var mysqldump = '../xampp/mysql/bin/mysqldump'
  527. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  528. // nodeShellExec('git', ['status']);
  529. function nodeShellExec() {
  530. var args = Array.from(arguments);
  531. args[2] ? null : args[2] = {}
  532. args[2].title ? null : args[2].title = `${args[0]} ${args[1]}`
  533. const child = spawn(...arguments);
  534. var p = new Promise(function(resolve, reject){
  535. if(!args[2].detached) {
  536. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  537. // use child.stdout.setEncoding('utf8'); if you want text chunks
  538. child.stdout.setEncoding('utf8');
  539. child.stderr.setEncoding('utf8');
  540. child.stdout.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  541. child.on('error', (chunk) => { messages.push(chunk); /*console.error(chunk)*/ });
  542. child.stderr.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  543. // child.stderr.pipe(process.stderr);
  544. child.on('close', (code) => {
  545. console.log([` ${args[2].title} exited with code ${code}`])
  546. // console.log([ messages.join('') ])
  547. process.stdout.write( messages.join('') )
  548. if(code !== 0 ) return reject(code)
  549. resolve(true)
  550. });
  551. }
  552. else {
  553. child.unref()
  554. resolve(true);
  555. }
  556. });
  557. p.process = child;
  558. return p;
  559. }