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 16KB

6 yıl önce
4 yıl önce
5 yıl önce
6 yıl önce
4 yıl önce
4 yıl önce
6 yıl önce
5 yıl önce
6 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
6 yıl önce
5 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
5 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
4 yıl önce
5 yıl önce
5 yıl önce
4 yıl önce
4 yıl önce
5 yıl önce
4 yıl önce
5 yıl önce
4 yıl önce
5 yıl önce
4 yıl önce
5 yıl önce
4 yıl önce
5 yıl önce
6 yıl önce
5 yıl önce
6 yıl önce
5 yıl önce
6 yıl önce
6 yıl önce
6 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 cli = 'elxr';
  77. var ver = '#unversioned';
  78. var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
  79. // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
  80. // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
  81. var dbForLabel = function(label){
  82. var dbsForLabel = {
  83. devmysql : 'mysql'
  84. , development : 'mssql'
  85. , production : 'mssql'
  86. }
  87. return dbsForLabel[label] || 'mysql'
  88. }
  89. // Relevant git repos
  90. var gitRepos = [
  91. 'ember-masonry-grid'
  92. , 'bbhverse'
  93. , 'clientverse'
  94. , 'serververse'
  95. , 'client'
  96. // , 'client/server'
  97. , 'elxr'
  98. , 'elixir-client'
  99. , 'elixir-client-unlinked'
  100. , 'ember-searchable-select'
  101. , 'loopback-component-jsonapi'
  102. , 'elixir-config'
  103. , 'cihsr-config'
  104. , 'cihsr-data'
  105. , 'elixir-data'
  106. , 'loopback-connector-ds'
  107. , 'chess-server-lib'
  108. , 'setup'
  109. ]
  110. // Repositiories that have symlinks that required elevated priviletes in windows to create symlinks
  111. //
  112. var elevatedRunasRepos = [
  113. 'elixir-server'
  114. , 'cihsr-server'
  115. , 'chess-server'
  116. ]
  117. var env = Object.assign({}, process.env); // Shallow clone it.
  118. var __runcmd = function(label){
  119. var op = {
  120. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  121. , 'upgrade' : ()=>{
  122. console.log('upgrade.......')
  123. var tasks = [
  124. ()=>{
  125. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  126. inherit : true, shell: true
  127. , env: process.env
  128. }).catch((e)=>{ console.error(e) })
  129. p.position = 1;
  130. console.log('One')
  131. return p;
  132. }
  133. , ()=>{
  134. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  135. inherit : true, shell: true
  136. , env: process.env
  137. }).catch((e)=>{ console.error(e) })
  138. p.position = 2;
  139. console.log('Two')
  140. return p;
  141. }
  142. , ()=>{
  143. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  144. inherit : true, shell: true
  145. , env: process.env
  146. }).catch((e)=>{ console.error(e) })
  147. p.position = 3;
  148. console.log('Three')
  149. return p;
  150. }
  151. , ()=>{
  152. var p = nodeShellExec('n', ['latest'], {
  153. inherit : true, shell: true
  154. , env: process.env
  155. }).catch((e)=>{ console.error(e) })
  156. p.position = 4;
  157. console.log('Four')
  158. return p;
  159. }
  160. ]
  161. any(tasks)
  162. console.log('.......done')
  163. console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
  164. console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
  165. console.info('Uninstalling existing ember globally') ;
  166. var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
  167. stdio: ['pipe', process.stdout, process.stderr],
  168. inherit : true,
  169. shell: true,
  170. cwd : path.dirname(__dirname),
  171. env: env
  172. })
  173. step1.on('close', ()=>{
  174. console.info('Installing ember globally') ;
  175. var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
  176. stdio: ['pipe', process.stdout, process.stderr],
  177. inherit : true,
  178. shell: true,
  179. cwd : path.dirname(__dirname),
  180. env: env
  181. })
  182. step2.on('close', ()=>{
  183. nodeShellExec('cmd', ['/c', 'ember', '--version'], {
  184. stdio: ['pipe', process.stdout, process.stderr],
  185. inherit : true,
  186. shell: true,
  187. cwd : path.dirname(__dirname),
  188. env: env
  189. })
  190. })
  191. })
  192. }
  193. , 'pull' : (label) => {
  194. var env = Object.assign({}, process.env); // Shallow clone it.
  195. // console.dir(env)
  196. console.log('Running exlr pull : ' + path.dirname(__dirname))
  197. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  198. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  199. // stdio: ['pipe', process.stdout, process.stderr],
  200. // inherit : true,
  201. // shell: true,
  202. // cwd : path.dirname(__dirname),
  203. // env: env
  204. // })
  205. var performPull = repo => {
  206. if(existsSync(repo)) {
  207. console.log('pulling ' + repo)
  208. nodeShellExec('git', ['pull'], {
  209. inherit : true, shell: true,
  210. cwd : repo
  211. // , env: process.env
  212. , title : `git pull ${repo}`
  213. }).catch((e)=>{ console.error(e) })
  214. }
  215. else {
  216. console.log('cloning ' + repo)
  217. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + repo + '.git'],
  218. {
  219. inherit : true, shell: true,
  220. env: process.env
  221. , title : `git clone ${'//172.16.0.27/repos/' + repo + '.git'}`
  222. }).catch((e)=>{ console.error(e) })
  223. }
  224. }
  225. var isElevated;
  226. try {
  227. nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
  228. inherit : true, shell: true
  229. , env: process.env
  230. , title : `check privileged execution mode using fsutil dirty query C:`
  231. }).then((exitcode)=>{
  232. console.log('Elevated')
  233. var isElevated = true;
  234. console.dir(processedArgs._[1])
  235. if(processedArgs._[1] === 'runas') elevatedRunasRepos.forEach(performPull);
  236. else gitRepos.forEach(performPull);
  237. }).catch(()=>{
  238. console.log('Not Elevated');
  239. gitRepos.forEach(performPull)
  240. console.log('Requesting Elevated Privileges')
  241. nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull runas', '', 'runas', 1);close();"]
  242. , {
  243. // inherit : true
  244. // , shell: true
  245. env: process.env
  246. , title : `runas`
  247. }
  248. );
  249. });
  250. // isElevated = true;
  251. }
  252. catch ( e ) {
  253. console.error(e)
  254. }
  255. }
  256. , 'npmi' : ()=>{
  257. var tasks = [];
  258. gitRepos.forEach(repo => {
  259. console.log('npm i for ' + repo)
  260. // nodeShellExec('pwd', [], {
  261. // // inherit : true, shell: true
  262. // cwd : repo
  263. // // , env: process.env
  264. // , title : `pwd for ${repo}`
  265. // }).catch((e)=>{ console.error(e) })
  266. nodeShellExec('rm', ['package-lock.json'], {
  267. inherit : true, shell: true
  268. , cwd : repo
  269. , env: process.env
  270. , title : `rm 'package-lock.json' for ${repo}`
  271. }).catch((e)=>{ console.error(e) })
  272. tasks.push(()=>{
  273. var p = nodeShellExec('npm', ['i'], {
  274. inherit : true, shell: true
  275. , cwd : repo
  276. , env: process.env
  277. , title : `npm i for ${repo}`
  278. }).catch((e)=>{ console.error(e) })
  279. return p;
  280. })
  281. })
  282. any(tasks);
  283. }
  284. , 'start' : (label)=>{
  285. console.log('Starting Elixir Server.');
  286. var env = Object.assign({}, process.env); // Shallow clone it.
  287. // console.dir(env)
  288. env.NODE_ENV = label || 'development';
  289. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  290. nodeShellExec('node', ['--inspect=9228', 'elixir/server.js'], {
  291. // inherit : true,
  292. shell: true, detached: true,
  293. cwd : 'elixir-server',
  294. env: env
  295. })
  296. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  297. // inherit : true,
  298. // shell: true, detached: true,
  299. // cwd : 'qms/server',
  300. // env: env,
  301. // shell : true
  302. // })
  303. nodeShellExec('ember', ['s'], {
  304. // inherit : true,
  305. shell: true, detached: true,
  306. cwd : 'client/',
  307. env: env
  308. })
  309. }
  310. , 'use' : ()=>{
  311. try {
  312. var runconfig = require('./run.js')
  313. if(processedArgs._[1] && runconfig.use === processedArgs._[1]) {
  314. return;
  315. }
  316. else runconfig.use = processedArgs._[1];
  317. fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig))
  318. }
  319. catch(e) {
  320. runconfig = { use : processedArgs._[1] };
  321. fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig))
  322. }
  323. var tasks = [
  324. ()=>{
  325. var p = nodeShellExec('mklink', ['/D', 'data', 'data-' + runconfig.use], {
  326. inherit : true, shell: true
  327. , env: process.env
  328. }).catch((e)=>{ console.error(e) })
  329. return p;
  330. },
  331. ()=>{
  332. var p = nodeShellExec('mklink', ['/D', 'config', 'config-' + runconfig.use], {
  333. inherit : true, shell: true
  334. , env: process.env
  335. }).catch((e)=>{ console.error(e) })
  336. return p;
  337. }
  338. ]
  339. any(tasks)
  340. // Antibiotic stewardship program.
  341. // 1st use is fine.
  342. // Max vials dispense
  343. // 2nd use Pharmacy needs justification Form.
  344. // Approval after a certain period of time.
  345. }
  346. , 'g' : ()=>{
  347. if(processedArgs.h) {
  348. console.log('elxr g [modelname] => generate a model named [modelname]');
  349. console.log('elxr g => regenerate all known models');
  350. return
  351. }
  352. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  353. // console.log('Starting directory: ' + process.cwd());
  354. // try {
  355. // child = child.on('close', () => { process.chdir(label) } );
  356. // console.log('New directory: ' + process.cwd());
  357. // }
  358. // catch (err) {
  359. // console.log('chdir: ' + err);
  360. // }
  361. // child.on('close', function(){
  362. // var options = {
  363. // shell : true
  364. // , inherit : true
  365. // // , cwd : '' + process.cwd
  366. // // , env : process.env
  367. // };
  368. // nodeShellExec('git', ['init'], { inherit : true});
  369. if(0){
  370. // PB : TODO -- Special google chrome profile for tests etc.
  371. nodeShellExec('pwd', { inherit : true});
  372. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  373. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  374. "--profile-directory=Profile 1"
  375. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  376. }
  377. // nodeShellExec('npm', ['init', '-y'], options);
  378. // nodeShellExec('npm', ['init', '-y'], options);
  379. // })
  380. var g = {
  381. 'client' : ()=>{
  382. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  383. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  384. stdio: ['pipe', process.stdout, process.stderr],
  385. inherit : true,
  386. shell: true,
  387. cwd : path.dirname(__dirname),
  388. env: env
  389. })
  390. }
  391. }
  392. g[processedArgs._[1]]();
  393. }
  394. }
  395. return op[label] ? op[label]() : op['start'](label);
  396. }
  397. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  398. // mysql -u root -p db_name < drop_all_tables.sql
  399. var mysql = '../xampp/mysql/bin/mysql'
  400. var mysqldump = '../xampp/mysql/bin/mysqldump'
  401. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  402. // nodeShellExec('git', ['status']);
  403. function nodeShellExec() {
  404. var args = Array.from(arguments);
  405. const child = spawn(...arguments);
  406. return new Promise(function(resolve, reject){
  407. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  408. // use child.stdout.setEncoding('utf8'); if you want text chunks
  409. child.stdout.setEncoding('utf8');
  410. child.stderr.setEncoding('utf8');
  411. child.stdout.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  412. child.on('error', (chunk) => { messages.push(chunk); /*console.error(chunk)*/ });
  413. child.stderr.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  414. // child.stderr.pipe(process.stderr);
  415. child.on('close', (code) => {
  416. console.log([` ${args[2].title} exited with code ${code}`])
  417. // console.log([ messages.join('') ])
  418. process.stdout.write( messages.join('') )
  419. if(code !== 0 ) return reject(code)
  420. resolve(true)
  421. });
  422. });
  423. }