Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // Serialize a set of functions that will execute to return a promises one after the other.
  10. // Will stop when any one fails.
  11. function any(iterable) {
  12. return iterable.reduce(
  13. (p, fn, i ,a) => {
  14. // console.log('accumulator :');
  15. // console.log(p);
  16. if(Promise.resolve(p) === p ) {
  17. return p.then((pVal) => {
  18. if(Promise.resolve(pVal) === pVal) {
  19. // Passed in function retured a promise. We still need to wait for it.
  20. pVal.then((pVal)=>{
  21. // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p);
  22. if(!pVal && !continueOnFailure) {
  23. console.error("E : pVal :" + pVal);
  24. // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]);
  25. // a.map((i)=>console.error(i.toString()));
  26. console.error('debugData 3 -------------------');
  27. // console.error(debugData);
  28. console.error('debugData 3 -------------------');
  29. console.log("Cancelling remaining...");
  30. throw 'Failed in reduce 1 '
  31. return false;
  32. }
  33. // console.log('calling fn : '+ fn);
  34. return (Promise.resolve(fn) === fn ) ? fn : fn() ;
  35. })
  36. }
  37. else {
  38. // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p);
  39. if(!pVal && !continueOnFailure) {
  40. console.error("E : pVal :" + pVal);
  41. // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]);
  42. // a.map((i)=>console.error(i.toString()));
  43. console.error('debugData 3 -------------------');
  44. // console.error(debugData);
  45. console.error('debugData 3 -------------------');
  46. console.log("Cancelling remaining...");
  47. throw 'Failed in reduce 1 '
  48. return false;
  49. }
  50. // console.log('calling fn : '+ fn);
  51. return (Promise.resolve(fn) === fn ) ? fn : fn() ;
  52. }
  53. }).catch((error) => {
  54. console.error("E : " + error);
  55. fn ? console.error("Fn : " + fn.toString()) : null;
  56. // i==0 ? null : console.error("E :2 a[i-1] " + a[i-1]);
  57. // a.map((i)=>console.error(i.toString()));
  58. console.error('debugData 4-------------------------');
  59. // console.error(debugData);
  60. console.error('debugData 4-------------------------');
  61. throw 'Failed in reduce 2 '
  62. return false;
  63. })
  64. }
  65. else if(!p) {
  66. console.log("Bypass on failure");
  67. return false;
  68. }
  69. }
  70. , Promise.resolve(true)
  71. );
  72. }
  73. var cli = 'elxr';
  74. var ver = '#unversioned';
  75. var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
  76. // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
  77. // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
  78. var dbForLabel = function(label){
  79. var dbsForLabel = {
  80. devmysql : 'mysql'
  81. , development : 'mssql'
  82. , production : 'mssql'
  83. }
  84. return dbsForLabel[label] || 'mysql'
  85. }
  86. var __runcmd = function(label){
  87. var op = {
  88. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  89. , 'upgrade' : ()=>{
  90. console.log('upgrade.......')
  91. var tasks = [
  92. ()=>{
  93. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  94. inherit : true, shell: true
  95. , env: process.env
  96. })
  97. p.position = 1;
  98. console.log('One')
  99. return p;
  100. }
  101. , ()=>{
  102. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  103. inherit : true, shell: true
  104. , env: process.env
  105. })
  106. p.position = 2;
  107. console.log('Two')
  108. return p;
  109. }
  110. , ()=>{
  111. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  112. inherit : true, shell: true
  113. , env: process.env
  114. })
  115. p.position = 3;
  116. console.log('Three')
  117. return p;
  118. }
  119. , ()=>{
  120. var p = nodeShellExec('n', ['latest'], {
  121. inherit : true, shell: true
  122. , env: process.env
  123. })
  124. p.position = 4;
  125. console.log('Four')
  126. return p;
  127. }
  128. ]
  129. any(tasks)
  130. console.log('.......done')
  131. }
  132. , 'pull' : ()=>{
  133. // Clone if not exists
  134. var gitclonables = [
  135. 'ember-masonry-grid'
  136. , 'bbhverse'
  137. , 'clientverse'
  138. , 'serververse'
  139. , 'client'
  140. , 'elixir-client'
  141. , 'elixir-client-unlinked'
  142. , 'ember-searchable-select'
  143. , 'loopback-component-jsonapi'
  144. , 'config'
  145. , 'loopback-connector-ds'
  146. , 'chess-server-lib'
  147. , 'elixir-server'
  148. , 'chess-server'
  149. , 'setup'
  150. ]
  151. gitclonables.forEach(clonable => {
  152. console.log('' + process.cwd())
  153. if(existsSync(clonable)) {
  154. console.log('pulling ' + clonable)
  155. nodeShellExec('git', ['pull'], {
  156. inherit : true, shell: true
  157. , cwd : clonable
  158. , env: process.env
  159. })
  160. }
  161. else {
  162. console.log('cloning...')
  163. nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + clonable + '.git'])
  164. }
  165. })
  166. console.log('pull')
  167. }
  168. , 'start' : (label)=>{
  169. console.log('Starting Elixir Server.');
  170. var env = Object.assign({}, process.env); // Shallow clone it.
  171. // console.dir(env)
  172. env.NODE_ENV = label;
  173. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  174. nodeShellExec('node', ['--inspect=9228', 'sage-rw/server.js'], {
  175. inherit : true,
  176. shell: true, detached: true,
  177. cwd : 'loopback',
  178. env: env
  179. })
  180. nodeShellExec('node', ['--inspect=9227', 'sage-r/server.js'], {
  181. inherit : true,
  182. shell: true, detached: true,
  183. cwd : 'loopback',
  184. env: env
  185. })
  186. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  187. // inherit : true,
  188. // shell: true, detached: true,
  189. // cwd : 'qms/server',
  190. // env: env,
  191. // shell : true
  192. // })
  193. nodeShellExec('ember', ['s'], {
  194. inherit : true,
  195. shell: true, detached: true,
  196. cwd : 'qms/',
  197. env: env,
  198. shell : true
  199. })
  200. }
  201. , 'g' : ()=>{
  202. if(processedArgs.h) {
  203. console.log('elxr g [modelname] => generate a model named [modelname]');
  204. console.log('elxr g => regenerate all known models');
  205. return
  206. }
  207. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  208. // console.log('Starting directory: ' + process.cwd());
  209. // try {
  210. // child = child.on('close', () => { process.chdir(label) } );
  211. // console.log('New directory: ' + process.cwd());
  212. // }
  213. // catch (err) {
  214. // console.log('chdir: ' + err);
  215. // }
  216. // child.on('close', function(){
  217. // var options = {
  218. // shell : true
  219. // , inherit : true
  220. // // , cwd : '' + process.cwd
  221. // // , env : process.env
  222. // };
  223. // nodeShellExec('git', ['init'], { inherit : true});
  224. nodeShellExec('pwd', { inherit : true});
  225. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  226. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  227. "--profile-directory=Profile 1"
  228. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  229. // nodeShellExec('npm', ['init', '-y'], options);
  230. // nodeShellExec('npm', ['init', '-y'], options);
  231. // })
  232. }
  233. }
  234. return op[label] ? op[label]() : op['start'](label);
  235. }
  236. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  237. // mysql -u root -p db_name < drop_all_tables.sql
  238. var mysql = '../xampp/mysql/bin/mysql'
  239. var mysqldump = '../xampp/mysql/bin/mysqldump'
  240. __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  241. // nodeShellExec('git', ['status']);
  242. function nodeShellExec() {
  243. const child = spawn(...arguments);
  244. return new Promise(function(resolve, reject){
  245. // use child.stdout.setEncoding('utf8'); if you want text chunks
  246. child.stdout.setEncoding('utf8');
  247. // console.log('here')
  248. child.stdout.on('data', (chunk) => console.log(chunk));
  249. child.on('error', (chunk) => console.error(chunk));
  250. child.stderr.pipe(process.stderr);
  251. child.on('close', (code) => {
  252. console.log(`child process ${Array.from(arguments)[0]} exited with code ${code}`)
  253. if(code !== 0 ) return reject(code)
  254. resolve(true)
  255. });
  256. });
  257. }