Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // --------------
  2. // elxr
  3. // A cli tool for elixr.
  4. const { existsSync } = require('fs');
  5. const { spawn, spawnSync } = require('child_process');
  6. const cliargs = require('../elxr/cliargs'); // Use minimist...
  7. const processedArgs = cliargs(process.argv.slice(2));
  8. console.dir(processedArgs)
  9. var path = require('path')
  10. // Serialize a set of functions that will execute to return a promises one after the other.
  11. // Will stop when any one fails.
  12. function any(iterable, continueOnFailure) {
  13. return iterable.reduce(
  14. (p, fn, i ,a) => {
  15. // console.log('accumulator :');
  16. // console.log(p);
  17. if(Promise.resolve(p) === p ) {
  18. return p.then((pVal) => {
  19. if(Promise.resolve(pVal) === pVal) {
  20. // Passed in function retured a promise. We still need to wait for it.
  21. pVal.then((pVal)=>{
  22. // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p);
  23. if(!pVal && !continueOnFailure) {
  24. console.error("E : pVal :" + pVal);
  25. // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]);
  26. // a.map((i)=>console.error(i.toString()));
  27. console.error('debugData 3 -------------------');
  28. // console.error(debugData);
  29. console.error('debugData 3 -------------------');
  30. console.log("Cancelling remaining...");
  31. throw 'Failed in reduce 1 '
  32. return false;
  33. }
  34. // console.log('calling fn : '+ fn);
  35. return (Promise.resolve(fn) === fn ) ? fn : fn() ;
  36. })
  37. }
  38. else {
  39. // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p);
  40. if(!pVal && !continueOnFailure) {
  41. console.error("E : pVal :" + pVal);
  42. // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]);
  43. // a.map((i)=>console.error(i.toString()));
  44. console.error('debugData 3 -------------------');
  45. // console.error(debugData);
  46. console.error('debugData 3 -------------------');
  47. console.log("Cancelling remaining...");
  48. throw 'Failed in reduce 1 '
  49. return false;
  50. }
  51. // console.log('calling fn : '+ fn);
  52. return (Promise.resolve(fn) === fn ) ? fn : fn() ;
  53. }
  54. }).catch((error) => {
  55. console.error("E : " + error);
  56. fn ? console.error("Fn : " + fn.toString()) : null;
  57. // i==0 ? null : console.error("E :2 a[i-1] " + a[i-1]);
  58. // a.map((i)=>console.error(i.toString()));
  59. console.error('debugData 4-------------------------');
  60. // console.error(debugData);
  61. console.error('debugData 4-------------------------');
  62. throw 'Failed in reduce 2 '
  63. return false;
  64. })
  65. }
  66. else if(!p) {
  67. console.log("Bypass on failure");
  68. return false;
  69. }
  70. }
  71. , Promise.resolve(true)
  72. );
  73. }
  74. var cli = 'elxr';
  75. var ver = '#unversioned';
  76. var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
  77. // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
  78. // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
  79. var dbForLabel = function(label){
  80. var dbsForLabel = {
  81. devmysql : 'mysql'
  82. , development : 'mssql'
  83. , production : 'mssql'
  84. }
  85. return dbsForLabel[label] || 'mysql'
  86. }
  87. // Relevant git repos
  88. var gitRepos = [
  89. 'ember-masonry-grid'
  90. , 'bbhverse'
  91. , 'clientverse'
  92. , 'serververse'
  93. , 'client'
  94. , 'elxr'
  95. , 'elixir-client'
  96. , 'elixir-client-unlinked'
  97. , 'ember-searchable-select'
  98. , 'loopback-component-jsonapi'
  99. , 'config'
  100. , 'loopback-connector-ds'
  101. , 'chess-server-lib'
  102. , 'elixir-server'
  103. , 'chess-server'
  104. , 'setup'
  105. ]
  106. var env = Object.assign({}, process.env); // Shallow clone it.
  107. var __runcmd = function(label){
  108. var op = {
  109. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  110. , 'upgrade' : ()=>{
  111. console.log('upgrade.......')
  112. var tasks = [
  113. ()=>{
  114. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  115. inherit : true, shell: true
  116. , env: process.env
  117. }).catch((e)=>{ console.error(e) })
  118. p.position = 1;
  119. console.log('One')
  120. return p;
  121. }
  122. , ()=>{
  123. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  124. inherit : true, shell: true
  125. , env: process.env
  126. }).catch((e)=>{ console.error(e) })
  127. p.position = 2;
  128. console.log('Two')
  129. return p;
  130. }
  131. , ()=>{
  132. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  133. inherit : true, shell: true
  134. , env: process.env
  135. }).catch((e)=>{ console.error(e) })
  136. p.position = 3;
  137. console.log('Three')
  138. return p;
  139. }
  140. , ()=>{
  141. var p = nodeShellExec('n', ['latest'], {
  142. inherit : true, shell: true
  143. , env: process.env
  144. }).catch((e)=>{ console.error(e) })
  145. p.position = 4;
  146. console.log('Four')
  147. return p;
  148. }
  149. ]
  150. any(tasks)
  151. console.log('.......done')
  152. console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
  153. console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
  154. console.info('Uninstalling existing ember globally') ;
  155. var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
  156. stdio: ['pipe', process.stdout, process.stderr],
  157. inherit : true,
  158. shell: true,
  159. cwd : path.dirname(__dirname),
  160. env: env
  161. })
  162. step1.on('close', ()=>{
  163. console.info('Installing ember globally') ;
  164. var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
  165. stdio: ['pipe', process.stdout, process.stderr],
  166. inherit : true,
  167. shell: true,
  168. cwd : path.dirname(__dirname),
  169. env: env
  170. })
  171. step2.on('close', ()=>{
  172. nodeShellExec('cmd', ['/c', 'ember', '--version'], {
  173. stdio: ['pipe', process.stdout, process.stderr],
  174. inherit : true,
  175. shell: true,
  176. cwd : path.dirname(__dirname),
  177. env: env
  178. })
  179. })
  180. })
  181. }
  182. , 'pull' : (label) => {
  183. var env = Object.assign({}, process.env); // Shallow clone it.
  184. // console.dir(env)
  185. console.log('Running exlr pull : ' + path.dirname(__dirname))
  186. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  187. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  188. // stdio: ['pipe', process.stdout, process.stderr],
  189. // inherit : true,
  190. // shell: true,
  191. // cwd : path.dirname(__dirname),
  192. // env: env
  193. // })
  194. var isElevated;
  195. try {
  196. nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
  197. inherit : true, shell: true
  198. , env: process.env
  199. }).then((exitcode)=>{
  200. console.log('Elevated')
  201. }).catch(()=>{
  202. console.log('Not Elevated')
  203. console.log('Requesting Elevated Privileges')
  204. nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]
  205. , {
  206. inherit : true
  207. // , shell: true
  208. // , env: process.env
  209. ,
  210. title : `runsAs`
  211. }
  212. );
  213. });
  214. // isElevated = true;
  215. }
  216. catch ( e ) {
  217. // isElevated = false;
  218. }
  219. // nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]);
  220. gitRepos.forEach(repo => {
  221. if(existsSync(repo)) {
  222. console.log('pulling ' + repo)
  223. nodeShellExec('git', ['pull'], {
  224. // inherit : true, shell: true
  225. // ,
  226. cwd : repo
  227. // , env: process.env
  228. , title : `git pull ${repo}`
  229. }).catch((e)=>{ console.error(e) })
  230. }
  231. else {
  232. console.log('cloning ' + repo)
  233. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + repo + '.git'],
  234. {
  235. // inherit : true, shell: true
  236. // ,
  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. }