555
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 22KB

6 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. , 'ember-searchable-select'
  112. , 'loopback-component-jsonapi'
  113. , 'elixir-config-development'
  114. , 'cihsr-config'
  115. , 'cihsr-data'
  116. , 'elixir-data'
  117. , 'loopback-connector-ds'
  118. , 'chess-server-lib'
  119. , 'setup'
  120. , 'elixir-client-todos'
  121. , 'elixir-client-unlinked'
  122. , 'elixir-client'
  123. , 'ember-service-worker'
  124. , 'ember-service-worker-asset-cache'
  125. , 'ember-service-worker-cache-fallback'
  126. , 'ember-service-worker-index'
  127. , 'ember-sw-client-route'
  128. , 'global-this'
  129. ]
  130. // Repositiories that have symlinks that required elevated priviletes in windows to create symlinks
  131. //
  132. var elevatedRunasRepos = [
  133. 'elixir-server'
  134. , 'cihsr-server'
  135. , 'chess-server'
  136. ]
  137. var env = Object.assign({}, process.env); // Shallow clone it.
  138. var __runcmd = function(label){
  139. var op = {
  140. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  141. , 'upgrade' : ()=>{
  142. console.log('upgrade.......')
  143. var tasks = [
  144. ()=>{
  145. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  146. inherit : true, shell: true
  147. , env: process.env
  148. }).catch((e)=>{ console.error(e) })
  149. p.position = 1;
  150. console.log('One')
  151. return p;
  152. }
  153. , ()=>{
  154. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  155. inherit : true, shell: true
  156. , env: process.env
  157. }).catch((e)=>{ console.error(e) })
  158. p.position = 2;
  159. console.log('Two')
  160. return p;
  161. }
  162. , ()=>{
  163. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  164. inherit : true, shell: true
  165. , env: process.env
  166. }).catch((e)=>{ console.error(e) })
  167. p.position = 3;
  168. console.log('Three')
  169. return p;
  170. }
  171. , ()=>{
  172. var p = nodeShellExec('n', ['latest'], {
  173. inherit : true, shell: true
  174. , env: process.env
  175. }).catch((e)=>{ console.error(e) })
  176. p.position = 4;
  177. console.log('Four')
  178. return p;
  179. }
  180. ]
  181. any(tasks)
  182. console.log('.......done')
  183. console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
  184. console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
  185. console.info('Uninstalling existing ember globally') ;
  186. var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
  187. stdio: ['pipe', process.stdout, process.stderr],
  188. inherit : true,
  189. shell: true,
  190. cwd : path.dirname(__dirname),
  191. env: env
  192. })
  193. step1.on('close', ()=>{
  194. console.info('Installing ember globally') ;
  195. var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
  196. stdio: ['pipe', process.stdout, process.stderr],
  197. inherit : true,
  198. shell: true,
  199. cwd : path.dirname(__dirname),
  200. env: env
  201. })
  202. step2.on('close', ()=>{
  203. nodeShellExec('cmd', ['/c', 'ember', '--version'], {
  204. stdio: ['pipe', process.stdout, process.stderr],
  205. inherit : true,
  206. shell: true,
  207. cwd : path.dirname(__dirname),
  208. env: env
  209. })
  210. })
  211. })
  212. }
  213. , 'runas' : ()=>{
  214. return isRunningElevated().then(() => op[ processedArgs.label || processedArgs._[0] || 'h']() )
  215. .catch(()=>{
  216. console.log('Not Elevated');
  217. console.dir(processedArgs)
  218. // PB : TODO -- Convert all the cli args back to string.
  219. var namedArgs = [];
  220. Object.keys(processedArgs).forEach((v)=>{ v!='_' ? namedArgs.push('--'+v+'='+processedArgs[v]) : null; })
  221. var cargs = `${processedArgs.debug ? '--inspect-brk' : ''} --inspect-brk elxr ${processedArgs._.join(' ')} ${namedArgs.join(' ') } --runas=true`
  222. console.log('Requesting Elevated Privileges for : ' + `node ${cargs}`)
  223. // fs.writeFileSync('runas.js', `javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', '${cargs}', '', 'runas', 1);close();`)
  224. nodeShellExec('MSHTA', [`javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', '${cargs}', '', 'runas', 1);close();`]
  225. , {
  226. // inherit : true
  227. // , shell: true
  228. // cwd : process.env.pwd
  229. // ,
  230. env: process.env
  231. }
  232. );
  233. })
  234. }
  235. , 'push' : ()=>{
  236. if(!processedArgs._[1]) { console.error('push all not supported. Specify repo name'); return }
  237. // init remote bare from local
  238. // pushandinitremotebare
  239. // https://www.jeffgeerling.com/blogs/jeff-geerling/push-your-git-repositories
  240. // connect to repo server -- net use 172.16.0.27
  241. // cd 172.16.0.27/repos/
  242. // mkdir repo.git
  243. // cd repo.git
  244. // git init --bare
  245. // cd localrepo
  246. // git remote rename origin githubclone
  247. // git remote add origin //172.16.0.27/repos/repo.git
  248. // git push origin master
  249. var repo = processedArgs._[1];
  250. var defaultRepoServer = '//172.16.0.27/repos'
  251. var sequentialTaskShellCommands = [];
  252. if(!existsSync(`Z:/${repo}.git`)){
  253. sequentialTaskShellCommands = [
  254. // ['net', ['use', 'Z:', defaultRepoServer.replace('/','\\')], {
  255. // inherit : true, shell: true
  256. // , env: process.env
  257. // }]
  258. ['pwd', { cwd : 'Z:', inherit : true}]
  259. , ['mkdir', [`${repo}.git`], {
  260. cwd : `Z:`
  261. , inherit : true, shell: true
  262. , env: process.env
  263. }]
  264. , ['pwd', { cwd : `Z:/${repo}.git`, inherit : true}]
  265. , ['git', ['init', '--bare'], { cwd : `Z:/${repo}.git`
  266. , inherit : true, shell: true
  267. , env: process.env
  268. }]
  269. , ['git', ['remote', 'rename', 'origin', 'githubclone'], { cwd : `${repo}`}, (err)=>{
  270. console.log('Ignoring origin rename error : ' + err); return true; //return true to continue.
  271. } ] // PB ; Todo -- new repositories created locally will not have origin. Handle this failure.
  272. , ['git', ['remote', 'add', 'origin', `${defaultRepoServer}/${repo}.git`], { cwd : `${repo}`}]
  273. ]
  274. if(!existsSync(`Z:`)){
  275. sequentialTaskShellCommands.splice(0,0, ['net', ['use', 'Z:', defaultRepoServer.replace(/\//gm,'\\')], {
  276. inherit : true, shell: true
  277. , env: process.env
  278. }])
  279. console.warn('Adding network drive z: for repo server. ' + sequentialTaskShellCommands[0])
  280. // throw 'done'
  281. }
  282. }
  283. sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd : `${repo}`}])
  284. // console.dir(sequentialTaskShellCommands);
  285. var tasks = [];
  286. sequentialTaskShellCommands.forEach(shellcmd => {
  287. // console.log(shellcmd)
  288. tasks.push(()=>{
  289. var p = nodeShellExec.apply(null, shellcmd.slice(0,3)).catch((e)=>{ if(shellcmd[3]) { return shellcmd[3]() } else { console.error(e);} })
  290. return p;
  291. })
  292. })
  293. any(tasks);
  294. }
  295. , 'pull' : (label) => {
  296. var env = Object.assign({}, process.env); // Shallow clone it.
  297. // console.dir(env)
  298. console.log('Running exlr pull : ' + path.dirname(__dirname))
  299. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  300. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  301. // stdio: ['pipe', process.stdout, process.stderr],
  302. // inherit : true,
  303. // shell: true,
  304. // cwd : path.dirname(__dirname),
  305. // env: env
  306. // })
  307. var performPull = repo => {
  308. if(existsSync(repo)) {
  309. console.log('pulling ' + repo)
  310. nodeShellExec('git', ['pull'], {
  311. inherit : true, shell: true,
  312. cwd : repo
  313. // , env: process.env
  314. , title : `git pull ${repo}`
  315. }).catch((e)=>{ console.error(e) })
  316. }
  317. else {
  318. console.log('cloning ' + repo)
  319. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + repo + '.git'],
  320. {
  321. inherit : true, shell: true,
  322. env: process.env
  323. , title : `git clone ${'//172.16.0.27/repos/' + repo + '.git'}`
  324. }).catch((e)=>{ console.error(e) })
  325. }
  326. }
  327. if(!processedArgs.runas) gitRepos.forEach(performPull)
  328. return isRunningElevated().then(
  329. ()=> elevatedRunasRepos.forEach(performPull))
  330. .catch(
  331. () => op['runas']()
  332. )
  333. }
  334. , 'npmi' : ()=>{
  335. var tasks = [];
  336. gitRepos.forEach(repo => {
  337. console.log('npm i for ' + repo)
  338. // nodeShellExec('pwd', [], {
  339. // // inherit : true, shell: true
  340. // cwd : repo
  341. // // , env: process.env
  342. // , title : `pwd for ${repo}`
  343. // }).catch((e)=>{ console.error(e) })
  344. nodeShellExec('rm', ['package-lock.json'], {
  345. inherit : true, shell: true
  346. , cwd : repo
  347. , env: process.env
  348. , title : `rm 'package-lock.json' for ${repo}`
  349. }).catch((e)=>{ console.error(e) })
  350. tasks.push(()=>{
  351. var p = nodeShellExec('npm', ['i'], {
  352. inherit : true, shell: true
  353. , cwd : repo
  354. , env: process.env
  355. , title : `npm i for ${repo}`
  356. }).catch((e)=>{ console.error(e) })
  357. return p;
  358. })
  359. })
  360. any(tasks);
  361. }
  362. , 'start' : (label)=>{
  363. console.log('Starting Elixir Server.');
  364. var env = Object.assign({}, process.env); // Shallow clone it.
  365. // console.dir(env)
  366. env.NODE_ENV = label || 'development';
  367. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  368. var cmd = env.NODE_ENV === 'development' ? 'nodemon' : 'node';
  369. // cmd = 'node'
  370. var childPromise = nodeShellExec(cmd, ['--inspect=9228', 'elixir/server.js'], {
  371. // inherit : true,
  372. shell: true,
  373. detached: true,
  374. stdio: 'ignore',
  375. cwd : 'elixir-server'
  376. , env: env
  377. })
  378. var child = childPromise.process;
  379. if (typeof child.pid !== 'undefined') {
  380. fs.writeFileSync('.elixir-server.elixir.server.pid', child.pid, {
  381. encoding: 'utf8'
  382. })
  383. }
  384. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  385. // inherit : true,
  386. // shell: true, detached: true,
  387. // cwd : 'qms/server',
  388. // env: env,
  389. // shell : true
  390. // })
  391. // nodeShellExec('ember', ['s'], {
  392. // // inherit : true,
  393. // shell: true, detached: true,
  394. // cwd : 'client/',
  395. // env: env
  396. // })
  397. var childPromise = nodeShellExec('ember', ['s'], {
  398. // var childPromise = nodeShellExec('node', ['--inspect=9227', './node_modules/.bin/ember', 's'], {
  399. // PB : TODO -- ember debugging.
  400. // inherit : true,
  401. shell: true,
  402. detached: true,
  403. stdio: 'ignore',
  404. cwd : 'client'
  405. , env: env
  406. })
  407. // .catch(e=>console.error(e))
  408. child = childPromise.process;
  409. if (typeof child.pid !== 'undefined') {
  410. fs.writeFileSync('.client.server.pid', child.pid, {
  411. encoding: 'utf8'
  412. })
  413. }
  414. }
  415. , 'stop' : (label)=>{
  416. const kill = require('tree-kill');
  417. var serverPid = fs.readFileSync('.elixir-server.elixir.server.pid', {
  418. encoding: 'utf8'
  419. })
  420. fs.unlinkSync('.elixir-server.elixir.server.pid')
  421. console.log(serverPid)
  422. kill(serverPid)
  423. serverPid = fs.readFileSync('.client.server.pid', {
  424. encoding: 'utf8'
  425. })
  426. fs.unlinkSync('.client.server.pid')
  427. console.log(serverPid)
  428. kill(serverPid)
  429. }
  430. , 'use' : ()=>{
  431. var runconfig = { NODE_ENV : process.env.NODE_ENV }
  432. try {
  433. runconfig = Object.assign(runconfig, require('../run.js'))
  434. } catch(e) { }
  435. if(runconfig.NODE_ENV === process.env.NODE_ENV && processedArgs._[1] && runconfig.use === processedArgs._[1]) return;
  436. var tasks = [
  437. ()=>{
  438. if(existsSync('config')) {
  439. var p = nodeShellExec('rmdir', ['config'], {inherit : true, shell: true, env: process.env }
  440. ).catch((err)=>{ console.log('Ignoring benign error : ' + err); return true; })
  441. return p;
  442. }
  443. else return Promise.resolve(true);
  444. },
  445. ()=>{
  446. if(existsSync('data')) {
  447. var p = nodeShellExec('rmdir', ['data'], { inherit : true, shell: true, env: process.env }
  448. ).catch((err)=>{ console.log('Ignoring benign error : ' + err); return true; })
  449. return p;
  450. }
  451. else return Promise.resolve(true);
  452. }
  453. ];
  454. any(tasks)
  455. if(!processedArgs.runas) return op['runas']()
  456. process.env.NODE_ENV = process.env.NODE_ENV || 'development';
  457. if(processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1];
  458. if(!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' }
  459. // console.log(process.env.cwd)
  460. fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig))
  461. tasks = [
  462. ()=>{
  463. // Use junctions to avoid npm package issues
  464. var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-' + process.env.NODE_ENV + '-config'], {
  465. inherit : true, shell: true
  466. , env: process.env
  467. }).catch((e)=>{ console.error(e) })
  468. return p;
  469. }
  470. ];
  471. if(processedArgs._[1]) {
  472. tasks.concat(
  473. [
  474. ()=>{
  475. var p = nodeShellExec('mklink', ['/D', 'data', runconfig.use + '-data'], {
  476. inherit : true, shell: true
  477. , env: process.env
  478. }).catch((e)=>{ console.error(e) })
  479. return p;
  480. }
  481. ]
  482. )
  483. }
  484. any(tasks)
  485. // Antibiotic stewardship program.
  486. // 1st use is fine.
  487. // Max vials dispense
  488. // 2nd use Pharmacy needs justification Form.
  489. // Approval after a certain period of time.
  490. }
  491. , 'g' : ()=>{
  492. if(processedArgs.h) {
  493. console.log('elxr g [modelname] => generate a model named [modelname]');
  494. console.log('elxr g => regenerate all known models');
  495. return
  496. }
  497. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  498. // console.log('Starting directory: ' + process.cwd());
  499. // try {
  500. // child = child.on('close', () => { process.chdir(label) } );
  501. // console.log('New directory: ' + process.cwd());
  502. // }
  503. // catch (err) {
  504. // console.log('chdir: ' + err);
  505. // }
  506. // child.on('close', function(){
  507. // var options = {
  508. // shell : true
  509. // , inherit : true
  510. // // , cwd : '' + process.cwd
  511. // // , env : process.env
  512. // };
  513. // nodeShellExec('git', ['init'], { inherit : true});
  514. if(0){
  515. // PB : TODO -- Special google chrome profile for tests etc.
  516. nodeShellExec('pwd', { inherit : true});
  517. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  518. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  519. "--profile-directory=Profile 1"
  520. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  521. }
  522. // nodeShellExec('npm', ['init', '-y'], options);
  523. // nodeShellExec('npm', ['init', '-y'], options);
  524. // })
  525. var g = {
  526. 'client' : ()=>{
  527. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  528. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  529. stdio: ['pipe', process.stdout, process.stderr],
  530. inherit : true,
  531. shell: true,
  532. cwd : path.dirname(__dirname),
  533. env: env
  534. })
  535. }
  536. }
  537. g[processedArgs._[1]]();
  538. }
  539. }
  540. return op[label] ? op[label]() : op['start'](label);
  541. }
  542. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  543. // mysql -u root -p db_name < drop_all_tables.sql
  544. var mysql = '../xampp/mysql/bin/mysql'
  545. var mysqldump = '../xampp/mysql/bin/mysqldump'
  546. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  547. // nodeShellExec('git', ['status']);
  548. function nodeShellExec() {
  549. var args = Array.from(arguments);
  550. args[2] ? null : args[2] = {}
  551. args[2].title ? null : args[2].title = `${args[0]} ${args[1]}`
  552. const child = spawn(...arguments);
  553. var p = new Promise(function(resolve, reject){
  554. if(!args[2].detached) {
  555. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  556. // use child.stdout.setEncoding('utf8'); if you want text chunks
  557. child.stdout.setEncoding('utf8');
  558. child.stderr.setEncoding('utf8');
  559. child.stdout.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  560. child.on('error', (chunk) => { messages.push(chunk); /*console.error(chunk)*/ });
  561. child.stderr.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  562. // child.stderr.pipe(process.stderr);
  563. child.on('close', (code) => {
  564. console.log([` ${args[2].title} exited with code ${code}`])
  565. // console.log([ messages.join('') ])
  566. process.stdout.write( messages.join('') )
  567. if(code !== 0 ) return reject(code)
  568. resolve(true)
  569. });
  570. }
  571. else {
  572. child.unref()
  573. resolve(true);
  574. }
  575. });
  576. p.process = child;
  577. return p;
  578. }