You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 9.6KB

6 yıl önce
5 yıl önce
6 yıl önce
6 yıl önce
5 yıl önce
6 yıl önce
6 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
6 yıl önce
5 yıl önce
6 yıl önce
5 yıl önce
6 yıl önce
6 yıl önce
6 yıl önce
6 yıl önce
6 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // 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 __runcmd = function(label){
  88. var op = {
  89. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  90. , 'upgrade' : ()=>{
  91. console.log('upgrade.......')
  92. var tasks = [
  93. ()=>{
  94. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  95. inherit : true, shell: true
  96. , env: process.env
  97. })
  98. p.position = 1;
  99. console.log('One')
  100. return p;
  101. }
  102. , ()=>{
  103. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  104. inherit : true, shell: true
  105. , env: process.env
  106. })
  107. p.position = 2;
  108. console.log('Two')
  109. return p;
  110. }
  111. , ()=>{
  112. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  113. inherit : true, shell: true
  114. , env: process.env
  115. })
  116. p.position = 3;
  117. console.log('Three')
  118. return p;
  119. }
  120. , ()=>{
  121. var p = nodeShellExec('n', ['latest'], {
  122. inherit : true, shell: true
  123. , env: process.env
  124. })
  125. p.position = 4;
  126. console.log('Four')
  127. return p;
  128. }
  129. ]
  130. any(tasks)
  131. console.log('.......done')
  132. }
  133. , 'pull' : ()=>{
  134. // Clone if not exists
  135. var gitclonables = [
  136. 'ember-masonry-grid'
  137. , 'bbhverse'
  138. , 'clientverse'
  139. , 'serververse'
  140. , 'client'
  141. , 'elixir-client'
  142. , 'elixir-client-unlinked'
  143. , 'ember-searchable-select'
  144. , 'loopback-component-jsonapi'
  145. , 'config'
  146. , 'loopback-connector-ds'
  147. , 'chess-server-lib'
  148. , 'elixir-server'
  149. , 'chess-server'
  150. , 'setup'
  151. ]
  152. gitclonables.forEach(clonable => {
  153. console.log('' + process.cwd())
  154. if(existsSync(clonable)) {
  155. console.log('pulling ' + clonable)
  156. nodeShellExec('git', ['pull'], {
  157. inherit : true, shell: true
  158. , cwd : clonable
  159. , env: process.env
  160. })
  161. }
  162. else {
  163. console.log('cloning...')
  164. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + clonable + '.git'])
  165. }
  166. })
  167. console.log('pull')
  168. }
  169. , 'start' : (label)=>{
  170. console.log('Starting Elixir Server.');
  171. var env = Object.assign({}, process.env); // Shallow clone it.
  172. // console.dir(env)
  173. env.NODE_ENV = label;
  174. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  175. nodeShellExec('node', ['--inspect=9228', 'sage-rw/server.js'], {
  176. inherit : true,
  177. shell: true, detached: true,
  178. cwd : 'loopback',
  179. env: env
  180. })
  181. nodeShellExec('node', ['--inspect=9227', 'sage-r/server.js'], {
  182. inherit : true,
  183. shell: true, detached: true,
  184. cwd : 'loopback',
  185. env: env
  186. })
  187. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  188. // inherit : true,
  189. // shell: true, detached: true,
  190. // cwd : 'qms/server',
  191. // env: env,
  192. // shell : true
  193. // })
  194. nodeShellExec('ember', ['s'], {
  195. inherit : true,
  196. shell: true, detached: true,
  197. cwd : 'qms/',
  198. env: env,
  199. shell : true
  200. })
  201. }
  202. , 'g' : ()=>{
  203. if(processedArgs.h) {
  204. console.log('elxr g [modelname] => generate a model named [modelname]');
  205. console.log('elxr g => regenerate all known models');
  206. return
  207. }
  208. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  209. // console.log('Starting directory: ' + process.cwd());
  210. // try {
  211. // child = child.on('close', () => { process.chdir(label) } );
  212. // console.log('New directory: ' + process.cwd());
  213. // }
  214. // catch (err) {
  215. // console.log('chdir: ' + err);
  216. // }
  217. // child.on('close', function(){
  218. // var options = {
  219. // shell : true
  220. // , inherit : true
  221. // // , cwd : '' + process.cwd
  222. // // , env : process.env
  223. // };
  224. // nodeShellExec('git', ['init'], { inherit : true});
  225. nodeShellExec('pwd', { inherit : true});
  226. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  227. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  228. "--profile-directory=Profile 1"
  229. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  230. // nodeShellExec('npm', ['init', '-y'], options);
  231. // nodeShellExec('npm', ['init', '-y'], options);
  232. // })
  233. }
  234. }
  235. return op[label] ? op[label]() : op['start'](label);
  236. }
  237. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  238. // mysql -u root -p db_name < drop_all_tables.sql
  239. var mysql = '../xampp/mysql/bin/mysql'
  240. var mysqldump = '../xampp/mysql/bin/mysqldump'
  241. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  242. // nodeShellExec('git', ['status']);
  243. function nodeShellExec() {
  244. const child = spawn(...arguments);
  245. return new Promise(function(resolve, reject){
  246. // use child.stdout.setEncoding('utf8'); if you want text chunks
  247. child.stdout.setEncoding('utf8');
  248. // console.log('here')
  249. child.stdout.on('data', (chunk) => console.log(chunk));
  250. child.on('error', (chunk) => console.error(chunk));
  251. child.stderr.pipe(process.stderr);
  252. child.on('close', (code) => {
  253. console.log(`child process ${Array.from(arguments)[0]} exited with code ${code}`)
  254. if(code !== 0 ) return reject(code)
  255. resolve(true)
  256. });
  257. });
  258. }