555
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 14KB

6 år sedan
5 år sedan
6 år sedan
6 år sedan
5 år sedan
6 år sedan
6 å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
5 år sedan
6 år sedan
5 år sedan
6 år sedan
5 år sedan
6 år sedan
6 år sedan
6 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. // ,
  226. cwd : clonable
  227. // , env: process.env
  228. , title : `git pull ${clonable}`
  229. }).catch((e)=>{ console.error(e) })
  230. }
  231. else {
  232. console.log('cloning ' + clonable)
  233. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + clonable + '.git'],
  234. {
  235. // inherit : true, shell: true
  236. // ,
  237. cwd : clonable
  238. // , env: process.env
  239. , title : `git clone ${'//172.16.0.27/repos/' + clonable + '.git'}`
  240. }).catch((e)=>{ console.error(e) })
  241. }
  242. })
  243. }
  244. , 'start' : (label)=>{
  245. console.log('Starting Elixir Server.');
  246. var env = Object.assign({}, process.env); // Shallow clone it.
  247. // console.dir(env)
  248. env.NODE_ENV = label || 'development';
  249. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  250. nodeShellExec('node', ['--inspect=9228', 'elixir/server.js'], {
  251. inherit : true,
  252. shell: true, detached: true,
  253. cwd : 'elixir-server',
  254. env: env
  255. })
  256. nodeShellExec('node', ['--inspect=9227', 'elixr/server.js'], {
  257. inherit : true,
  258. shell: true, detached: true,
  259. cwd : 'elixir-server',
  260. env: env
  261. })
  262. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  263. // inherit : true,
  264. // shell: true, detached: true,
  265. // cwd : 'qms/server',
  266. // env: env,
  267. // shell : true
  268. // })
  269. nodeShellExec('ember', ['s'], {
  270. inherit : true,
  271. shell: true, detached: true,
  272. cwd : 'client/',
  273. env: env
  274. })
  275. }
  276. , 'g' : ()=>{
  277. if(processedArgs.h) {
  278. console.log('elxr g [modelname] => generate a model named [modelname]');
  279. console.log('elxr g => regenerate all known models');
  280. return
  281. }
  282. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  283. // console.log('Starting directory: ' + process.cwd());
  284. // try {
  285. // child = child.on('close', () => { process.chdir(label) } );
  286. // console.log('New directory: ' + process.cwd());
  287. // }
  288. // catch (err) {
  289. // console.log('chdir: ' + err);
  290. // }
  291. // child.on('close', function(){
  292. // var options = {
  293. // shell : true
  294. // , inherit : true
  295. // // , cwd : '' + process.cwd
  296. // // , env : process.env
  297. // };
  298. // nodeShellExec('git', ['init'], { inherit : true});
  299. if(0){
  300. // PB : TODO -- Special google chrome profile for tests etc.
  301. nodeShellExec('pwd', { inherit : true});
  302. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  303. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  304. "--profile-directory=Profile 1"
  305. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  306. }
  307. // nodeShellExec('npm', ['init', '-y'], options);
  308. // nodeShellExec('npm', ['init', '-y'], options);
  309. // })
  310. var g = {
  311. 'client' : ()=>{
  312. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  313. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  314. stdio: ['pipe', process.stdout, process.stderr],
  315. inherit : true,
  316. shell: true,
  317. cwd : path.dirname(__dirname),
  318. env: env
  319. })
  320. }
  321. }
  322. g[processedArgs._[1]]();
  323. }
  324. }
  325. return op[label] ? op[label]() : op['start'](label);
  326. }
  327. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  328. // mysql -u root -p db_name < drop_all_tables.sql
  329. var mysql = '../xampp/mysql/bin/mysql'
  330. var mysqldump = '../xampp/mysql/bin/mysqldump'
  331. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  332. // nodeShellExec('git', ['status']);
  333. function nodeShellExec() {
  334. var args = Array.from(arguments);
  335. const child = spawn(...arguments);
  336. return new Promise(function(resolve, reject){
  337. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  338. // use child.stdout.setEncoding('utf8'); if you want text chunks
  339. child.stdout.setEncoding('utf8');
  340. child.stdout.on('data', (chunk) => { messages.push(chunk); /*console.log(chunk)*/});
  341. child.on('error', (chunk) => { messages.push(chunk); /*console.error(chunk)*/ });
  342. child.stderr.pipe(process.stderr);
  343. child.on('close', (code) => {
  344. console.log(` ${args[2].title} exited with code ${code}`)
  345. console.log(messages)
  346. if(code !== 0 ) return reject(code)
  347. resolve(true)
  348. });
  349. });
  350. }