Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.js 15KB

6 år sedan
5 år sedan
6 år sedan
6 år sedan
5 år sedan
6 år sedan
6 år sedan
5 år sedan
4 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
6 år sedan
5 år sedan
6 år sedan
5 år sedan
6 år sedan
6 år sedan
6 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. 
  2. // --------------
  3. // elxr
  4. // A cli tool for elixr.
  5. const { existsSync } = 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. , 'elxr'
  96. , 'elixir-client'
  97. , 'elixir-client-unlinked'
  98. , 'ember-searchable-select'
  99. , 'loopback-component-jsonapi'
  100. , 'config'
  101. , 'loopback-connector-ds'
  102. , 'chess-server-lib'
  103. , 'elixir-server'
  104. , 'chess-server'
  105. , 'setup'
  106. ]
  107. var env = Object.assign({}, process.env); // Shallow clone it.
  108. var __runcmd = function(label){
  109. var op = {
  110. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  111. , 'upgrade' : ()=>{
  112. console.log('upgrade.......')
  113. var tasks = [
  114. ()=>{
  115. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  116. inherit : true, shell: true
  117. , env: process.env
  118. }).catch((e)=>{ console.error(e) })
  119. p.position = 1;
  120. console.log('One')
  121. return p;
  122. }
  123. , ()=>{
  124. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  125. inherit : true, shell: true
  126. , env: process.env
  127. }).catch((e)=>{ console.error(e) })
  128. p.position = 2;
  129. console.log('Two')
  130. return p;
  131. }
  132. , ()=>{
  133. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  134. inherit : true, shell: true
  135. , env: process.env
  136. }).catch((e)=>{ console.error(e) })
  137. p.position = 3;
  138. console.log('Three')
  139. return p;
  140. }
  141. , ()=>{
  142. var p = nodeShellExec('n', ['latest'], {
  143. inherit : true, shell: true
  144. , env: process.env
  145. }).catch((e)=>{ console.error(e) })
  146. p.position = 4;
  147. console.log('Four')
  148. return p;
  149. }
  150. ]
  151. any(tasks)
  152. console.log('.......done')
  153. console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
  154. console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
  155. console.info('Uninstalling existing ember globally') ;
  156. var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
  157. stdio: ['pipe', process.stdout, process.stderr],
  158. inherit : true,
  159. shell: true,
  160. cwd : path.dirname(__dirname),
  161. env: env
  162. })
  163. step1.on('close', ()=>{
  164. console.info('Installing ember globally') ;
  165. var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-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. step2.on('close', ()=>{
  173. nodeShellExec('cmd', ['/c', 'ember', '--version'], {
  174. stdio: ['pipe', process.stdout, process.stderr],
  175. inherit : true,
  176. shell: true,
  177. cwd : path.dirname(__dirname),
  178. env: env
  179. })
  180. })
  181. })
  182. }
  183. , 'pull' : (label) => {
  184. var env = Object.assign({}, process.env); // Shallow clone it.
  185. // console.dir(env)
  186. console.log('Running exlr pull : ' + path.dirname(__dirname))
  187. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  188. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  189. // stdio: ['pipe', process.stdout, process.stderr],
  190. // inherit : true,
  191. // shell: true,
  192. // cwd : path.dirname(__dirname),
  193. // env: env
  194. // })
  195. var isElevated;
  196. try {
  197. nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
  198. inherit : true, shell: true
  199. , env: process.env
  200. }).then((exitcode)=>{
  201. console.log('Elevated')
  202. }).catch(()=>{
  203. console.log('Not Elevated')
  204. console.log('Requesting Elevated Privileges')
  205. nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]
  206. , {
  207. inherit : true
  208. // , shell: true
  209. // , env: process.env
  210. ,
  211. title : `runsAs`
  212. }
  213. );
  214. });
  215. // isElevated = true;
  216. }
  217. catch ( e ) {
  218. // isElevated = false;
  219. }
  220. // nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]);
  221. gitRepos.forEach(repo => {
  222. if(existsSync(repo)) {
  223. console.log('pulling ' + repo)
  224. nodeShellExec('git', ['pull'], {
  225. // inherit : true, shell: true
  226. // ,
  227. cwd : repo
  228. // , env: process.env
  229. , title : `git pull ${repo}`
  230. }).catch((e)=>{ console.error(e) })
  231. }
  232. else {
  233. console.log('cloning ' + repo)
  234. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + repo + '.git'],
  235. {
  236. // inherit : true, shell: true
  237. // ,
  238. cwd : repo
  239. // , env: process.env
  240. , title : `git clone ${'//172.16.0.27/repos/' + repo + '.git'}`
  241. }).catch((e)=>{ console.error(e) })
  242. }
  243. })
  244. }
  245. , 'npmi' : ()=>{
  246. var tasks = [];
  247. gitRepos.forEach(repo => {
  248. console.log('npm i for ' + repo)
  249. // nodeShellExec('pwd', [], {
  250. // // inherit : true, shell: true
  251. // cwd : repo
  252. // // , env: process.env
  253. // , title : `pwd for ${repo}`
  254. // }).catch((e)=>{ console.error(e) })
  255. nodeShellExec('rm', ['package-lock.json'], {
  256. inherit : true, shell: true
  257. , cwd : repo
  258. , env: process.env
  259. , title : `rm 'package-lock.json' for ${repo}`
  260. }).catch((e)=>{ console.error(e) })
  261. tasks.push(()=>{
  262. var p = nodeShellExec('npm', ['i'], {
  263. inherit : true, shell: true
  264. , cwd : repo
  265. , env: process.env
  266. , title : `npm i for ${repo}`
  267. }).catch((e)=>{ console.error(e) })
  268. return p;
  269. })
  270. })
  271. any(tasks);
  272. }
  273. , 'start' : (label)=>{
  274. console.log('Starting Elixir Server.');
  275. var env = Object.assign({}, process.env); // Shallow clone it.
  276. // console.dir(env)
  277. env.NODE_ENV = label || 'development';
  278. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  279. nodeShellExec('node', ['--inspect=9228', 'elixir/server.js'], {
  280. inherit : true,
  281. shell: true, detached: true,
  282. cwd : 'elixir-server',
  283. env: env
  284. })
  285. nodeShellExec('node', ['--inspect=9227', 'elixr/server.js'], {
  286. inherit : true,
  287. shell: true, detached: true,
  288. cwd : 'elixir-server',
  289. env: env
  290. })
  291. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  292. // inherit : true,
  293. // shell: true, detached: true,
  294. // cwd : 'qms/server',
  295. // env: env,
  296. // shell : true
  297. // })
  298. nodeShellExec('ember', ['s'], {
  299. inherit : true,
  300. shell: true, detached: true,
  301. cwd : 'client/',
  302. env: env
  303. })
  304. }
  305. , 'g' : ()=>{
  306. if(processedArgs.h) {
  307. console.log('elxr g [modelname] => generate a model named [modelname]');
  308. console.log('elxr g => regenerate all known models');
  309. return
  310. }
  311. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  312. // console.log('Starting directory: ' + process.cwd());
  313. // try {
  314. // child = child.on('close', () => { process.chdir(label) } );
  315. // console.log('New directory: ' + process.cwd());
  316. // }
  317. // catch (err) {
  318. // console.log('chdir: ' + err);
  319. // }
  320. // child.on('close', function(){
  321. // var options = {
  322. // shell : true
  323. // , inherit : true
  324. // // , cwd : '' + process.cwd
  325. // // , env : process.env
  326. // };
  327. // nodeShellExec('git', ['init'], { inherit : true});
  328. if(0){
  329. // PB : TODO -- Special google chrome profile for tests etc.
  330. nodeShellExec('pwd', { inherit : true});
  331. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  332. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  333. "--profile-directory=Profile 1"
  334. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  335. }
  336. // nodeShellExec('npm', ['init', '-y'], options);
  337. // nodeShellExec('npm', ['init', '-y'], options);
  338. // })
  339. var g = {
  340. 'client' : ()=>{
  341. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  342. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  343. stdio: ['pipe', process.stdout, process.stderr],
  344. inherit : true,
  345. shell: true,
  346. cwd : path.dirname(__dirname),
  347. env: env
  348. })
  349. }
  350. }
  351. g[processedArgs._[1]]();
  352. }
  353. }
  354. return op[label] ? op[label]() : op['start'](label);
  355. }
  356. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  357. // mysql -u root -p db_name < drop_all_tables.sql
  358. var mysql = '../xampp/mysql/bin/mysql'
  359. var mysqldump = '../xampp/mysql/bin/mysqldump'
  360. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  361. // nodeShellExec('git', ['status']);
  362. function nodeShellExec() {
  363. var args = Array.from(arguments);
  364. const child = spawn(...arguments);
  365. return new Promise(function(resolve, reject){
  366. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  367. // use child.stdout.setEncoding('utf8'); if you want text chunks
  368. child.stdout.setEncoding('utf8');
  369. child.stderr.setEncoding('utf8');
  370. child.stdout.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  371. child.on('error', (chunk) => { messages.push(chunk); /*console.error(chunk)*/ });
  372. child.stderr.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  373. // child.stderr.pipe(process.stderr);
  374. child.on('close', (code) => {
  375. console.log([` ${args[2].title} exited with code ${code}`])
  376. // console.log([ messages.join('') ])
  377. process.stdout.write( messages.join('') )
  378. if(code !== 0 ) return reject(code)
  379. resolve(true)
  380. });
  381. });
  382. }