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

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