555
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 15KB

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