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

6 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
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
4 vuotta sitten
5 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
4 vuotta sitten
5 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
4 vuotta sitten
5 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
6 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. , title : `check privileged execution mode using fsutil dirty query C:`
  201. }).then((exitcode)=>{
  202. console.log('Elevated')
  203. }).catch(()=>{
  204. console.log('Not Elevated')
  205. console.log('Requesting Elevated Privileges')
  206. nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]
  207. , {
  208. inherit : true
  209. // , shell: true
  210. , env: process.env
  211. ,
  212. title : `runsAs`
  213. }
  214. );
  215. });
  216. // isElevated = true;
  217. }
  218. catch ( e ) {
  219. // isElevated = false;
  220. }
  221. // nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]);
  222. gitRepos.forEach(repo => {
  223. if(existsSync(repo)) {
  224. console.log('pulling ' + repo)
  225. nodeShellExec('git', ['pull'], {
  226. inherit : true, shell: true,
  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. cwd : repo
  238. , env: process.env
  239. , title : `git clone ${'//172.16.0.27/repos/' + repo + '.git'}`
  240. }).catch((e)=>{ console.error(e) })
  241. }
  242. })
  243. }
  244. , 'npmi' : ()=>{
  245. var tasks = [];
  246. gitRepos.forEach(repo => {
  247. console.log('npm i for ' + repo)
  248. // nodeShellExec('pwd', [], {
  249. // // inherit : true, shell: true
  250. // cwd : repo
  251. // // , env: process.env
  252. // , title : `pwd for ${repo}`
  253. // }).catch((e)=>{ console.error(e) })
  254. nodeShellExec('rm', ['package-lock.json'], {
  255. inherit : true, shell: true
  256. , cwd : repo
  257. , env: process.env
  258. , title : `rm 'package-lock.json' for ${repo}`
  259. }).catch((e)=>{ console.error(e) })
  260. tasks.push(()=>{
  261. var p = nodeShellExec('npm', ['i'], {
  262. inherit : true, shell: true
  263. , cwd : repo
  264. , env: process.env
  265. , title : `npm i for ${repo}`
  266. }).catch((e)=>{ console.error(e) })
  267. return p;
  268. })
  269. })
  270. any(tasks);
  271. }
  272. , 'start' : (label)=>{
  273. console.log('Starting Elixir Server.');
  274. var env = Object.assign({}, process.env); // Shallow clone it.
  275. // console.dir(env)
  276. env.NODE_ENV = label || 'development';
  277. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  278. nodeShellExec('node', ['--inspect=9228', 'elixir/server.js'], {
  279. inherit : true,
  280. shell: true, detached: true,
  281. cwd : 'elixir-server',
  282. env: env
  283. })
  284. nodeShellExec('node', ['--inspect=9227', 'elixr/server.js'], {
  285. inherit : true,
  286. shell: true, detached: true,
  287. cwd : 'elixir-server',
  288. env: env
  289. })
  290. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  291. // inherit : true,
  292. // shell: true, detached: true,
  293. // cwd : 'qms/server',
  294. // env: env,
  295. // shell : true
  296. // })
  297. nodeShellExec('ember', ['s'], {
  298. inherit : true,
  299. shell: true, detached: true,
  300. cwd : 'client/',
  301. env: env
  302. })
  303. }
  304. , 'g' : ()=>{
  305. if(processedArgs.h) {
  306. console.log('elxr g [modelname] => generate a model named [modelname]');
  307. console.log('elxr g => regenerate all known models');
  308. return
  309. }
  310. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  311. // console.log('Starting directory: ' + process.cwd());
  312. // try {
  313. // child = child.on('close', () => { process.chdir(label) } );
  314. // console.log('New directory: ' + process.cwd());
  315. // }
  316. // catch (err) {
  317. // console.log('chdir: ' + err);
  318. // }
  319. // child.on('close', function(){
  320. // var options = {
  321. // shell : true
  322. // , inherit : true
  323. // // , cwd : '' + process.cwd
  324. // // , env : process.env
  325. // };
  326. // nodeShellExec('git', ['init'], { inherit : true});
  327. if(0){
  328. // PB : TODO -- Special google chrome profile for tests etc.
  329. nodeShellExec('pwd', { inherit : true});
  330. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  331. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  332. "--profile-directory=Profile 1"
  333. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  334. }
  335. // nodeShellExec('npm', ['init', '-y'], options);
  336. // nodeShellExec('npm', ['init', '-y'], options);
  337. // })
  338. var g = {
  339. 'client' : ()=>{
  340. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  341. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  342. stdio: ['pipe', process.stdout, process.stderr],
  343. inherit : true,
  344. shell: true,
  345. cwd : path.dirname(__dirname),
  346. env: env
  347. })
  348. }
  349. }
  350. g[processedArgs._[1]]();
  351. }
  352. }
  353. return op[label] ? op[label]() : op['start'](label);
  354. }
  355. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  356. // mysql -u root -p db_name < drop_all_tables.sql
  357. var mysql = '../xampp/mysql/bin/mysql'
  358. var mysqldump = '../xampp/mysql/bin/mysqldump'
  359. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  360. // nodeShellExec('git', ['status']);
  361. function nodeShellExec() {
  362. var args = Array.from(arguments);
  363. const child = spawn(...arguments);
  364. return new Promise(function(resolve, reject){
  365. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  366. // use child.stdout.setEncoding('utf8'); if you want text chunks
  367. child.stdout.setEncoding('utf8');
  368. child.stderr.setEncoding('utf8');
  369. child.stdout.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  370. child.on('error', (chunk) => { messages.push(chunk); /*console.error(chunk)*/ });
  371. child.stderr.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  372. // child.stderr.pipe(process.stderr);
  373. child.on('close', (code) => {
  374. console.log([` ${args[2].title} exited with code ${code}`])
  375. // console.log([ messages.join('') ])
  376. process.stdout.write( messages.join('') )
  377. if(code !== 0 ) return reject(code)
  378. resolve(true)
  379. });
  380. });
  381. }