您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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