555
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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