Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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