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

3 лет назад
3 лет назад
3 лет назад
6 лет назад
4 лет назад
4 лет назад
3 лет назад
5 лет назад
6 лет назад
3 лет назад
3 лет назад
3 лет назад
4 лет назад
4 лет назад
6 лет назад
5 лет назад
6 лет назад
5 лет назад
4 лет назад
4 лет назад
3 лет назад
4 лет назад
4 лет назад
3 лет назад
3 лет назад
3 лет назад
4 лет назад
4 лет назад
6 лет назад
5 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
3 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
3 лет назад
4 лет назад
3 лет назад
4 лет назад
3 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
3 лет назад
3 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
3 лет назад
4 лет назад
4 лет назад
4 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
4 лет назад
5 лет назад
5 лет назад
5 лет назад
4 лет назад
4 лет назад
5 лет назад
5 лет назад
4 лет назад
5 лет назад
5 лет назад
4 лет назад
5 лет назад
4 лет назад
5 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
5 лет назад
4 лет назад
4 лет назад
5 лет назад
3 лет назад
5 лет назад
4 лет назад
5 лет назад
4 лет назад
3 лет назад
4 лет назад
3 лет назад
5 лет назад
6 лет назад
5 лет назад
6 лет назад
5 лет назад
3 лет назад
6 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. var path = require('path');
  2. var utils = require('bbhverse');
  3. var any = utils.any;
  4. var Tasq = utils.Tasq
  5. var statuslog = utils.Traq
  6. Tasq.addlistener(statuslog.statuslog)
  7. function nodeShellExec() {
  8. var args = Array.from(arguments);
  9. var opts = args[2] = args[2] || {}
  10. opts.title ? null : opts.title = `${args[0]} ${args[1] }`
  11. const child = spawn(...arguments);
  12. var p = new Promise(function(resolve, reject){
  13. if(!opts.detached) {
  14. var messages = []; // PB : TODO -- Explore stream for Task level aggregation to prevent interleaved messages from multiple tasks...
  15. var success = true;
  16. if(opts.stdio !== 'ignore') {
  17. child.stdout.setEncoding('utf8');
  18. child.stderr.setEncoding('utf8');
  19. child.stdout.on('data', (chunk) => { chunk.trim() === '' ? null : messages.push(chunk); /* console.log('d: ' + chunk) */ });
  20. child.on('error', (chunk) => { success = false; messages.push(chunk); /* console.error('e: ' + chunk) */ } );
  21. child.stderr.on('data', (chunk) => {
  22. if(messages.join('').indexOf('fatal: not a git repository') > -1) opts.haserrors = true;
  23. messages.push(chunk);
  24. // console.error('stderr e: ' + chunk)
  25. });
  26. }
  27. child.on('close', (code) => {
  28. if(+code !== 0 || opts.haserrors) success = false;
  29. if(opts.stdio !== 'ignore') {
  30. var logEntry = { result: `${opts.title} exited with code ${code}`, messages }
  31. logEntry.success = success;
  32. if(opts.runas){
  33. // success ? logEntry.success = true : null;
  34. fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
  35. }
  36. else {
  37. // console.log( messages.join('') )
  38. process.stdout.write( messages.join('') )
  39. }
  40. }
  41. if(code !== 0 || opts.haserrors) return reject(logEntry)
  42. resolve(logEntry)
  43. });
  44. }
  45. else {
  46. child.unref()
  47. resolve(true);
  48. }
  49. });
  50. p.process = child;
  51. return p;
  52. }
  53. // 'use strict';
  54. // PB : TODO -- make sure folder context is proper coz we can now run elxr from anywhere.
  55. // --------------
  56. // elxr
  57. // A cli tool for elixr.
  58. // PB : TODO --
  59. // runas bypass non elevated tasks when we request privelege
  60. // runas message is always error needs to be fixed.
  61. // runas wait needs to be parallelized.
  62. // suppress elevation check error messages
  63. // support runas lauched directly from shell.
  64. // pass in environment in hta to shellexecute.
  65. const { existsSync } = require('fs');
  66. const fs = require('fs')
  67. const { spawn, spawnSync } = require('child_process');
  68. const cliargs = utils.cliargs;
  69. const processedArgs = cliargs(process.argv.slice(2));
  70. console.dir(processedArgs)
  71. var globSync = require('glob').sync;
  72. var path = require('path');
  73. const { isMaster } = require('cluster');
  74. // Default Config...
  75. var reposervers = [
  76. 'http://git.bbh'
  77. , 'https://git.bbh.org.in'
  78. , '//172.16.0.27/repos'
  79. ]
  80. var defaultRepoServer = reposervers[0]
  81. var currentGitAuthUser ; // nodeShellExec('git', ['config', 'user.email']) ... PB : TODO-- get the current gittea username
  82. var defaultRepoOwner = 'chess';
  83. // PB : TODO -- If we are run from an elevated shell it never moves forward and simply exits.
  84. // -- Currently workaround is to always run from a non-elevated shell.
  85. var __isElevated = null; // We assume non-Elevated until someone evaluates and sets this variable.
  86. var isRunningElevated = ()=>{
  87. if(__isElevated === null) {
  88. return nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
  89. inherit : true
  90. // , shell: true
  91. , stdio: 'ignore'
  92. , env: process.env
  93. , title : `check privileged execution mode using "fsutil dirty query C:"`
  94. }).then((exitcode)=>{
  95. console.log('Elevated')
  96. __isElevated = true;
  97. return true;
  98. }).catch(()=>{
  99. __isElevated = false;
  100. console.log('Not Elevated');
  101. throw false;
  102. });
  103. }
  104. else return Promise.resolve(__isElevated);
  105. }
  106. var cli = 'elxr';
  107. var ver = '#unversioned';
  108. var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
  109. // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
  110. // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
  111. var dbForLabel = function(label){
  112. var dbsForLabel = {
  113. devmysql : 'mysql'
  114. , development : 'mssql'
  115. , production : 'mssql'
  116. }
  117. return dbsForLabel[label] || 'mysql'
  118. }
  119. var gitInstallDir = "C:\\Program Files\\Git\\bin\\sh.exe"
  120. // var gitInstallDir = "G:\\Installed\\Git\\bin\\sh.exe"
  121. // Relevant git repos
  122. var brandName = 'elixir';
  123. // Runas windowshta clobbers and removes the NODE_ENV !!! We therefore pass it in.
  124. var nodeenv = process.env.NODE_ENV || processedArgs.node_env || 'development'
  125. if(nodeenv.trim() === 'production') nodeenv = 'production'
  126. // var repomanifest = require('../'+brandName+'-config-'+ nodeenv +'/repo-manifest')()
  127. var exludeMergeRepos = [];
  128. var useGitPull = processedArgs.useGitPull || false;
  129. var getPullCmd = (repo)=>{
  130. // console.log(useGitPull)
  131. var pullCmd = [ gitInstallDir
  132. , ['-c', 'for i in `git remote`; do git pull $i master; done;']
  133. , { cwd : repo, title : 'pull all origins for ' + repo }]
  134. // var pullCmd = ['pullall', [], { cwd : repo }]
  135. if(useGitPull) pullCmd = ['git', ['pull'], {
  136. inherit : true, shell: true,
  137. cwd : repo
  138. // , env: process.env
  139. , runas : processedArgs.runas
  140. , title : `git pull ${repo}`
  141. }]
  142. return pullCmd
  143. }
  144. var performPull = (repo) => {
  145. if(exludeMergeRepos[repo]) return Promise.resolve({ 'skipped' : true })
  146. if(existsSync(repo)) {
  147. console.log('pulling ' + repo)
  148. return nodeShellExec.apply(null, getPullCmd(repo)).catch((e)=>{ console.error(e) })
  149. }
  150. else {
  151. console.log('cloning ' + repo)
  152. // PB : TODO -- detect if a clonable repo exists in currentGitAuthUser
  153. return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'],
  154. {
  155. inherit : true, shell: true,
  156. env: process.env
  157. , runas : processedArgs.runas
  158. }).catch((e)=>{ console.error(e) }).then(()=>{
  159. return nodeShellExec('git', ['config', '--replace-all' , 'core.symlinks', true],
  160. {
  161. inherit : true, shell: true,
  162. env: process.env
  163. , cwd : repo
  164. , runas : processedArgs.runas
  165. , title : `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
  166. })
  167. })
  168. }
  169. }
  170. var configrepo = brandName+'-config-'+nodeenv
  171. var configpath = '../' + configrepo;
  172. var configPromise = null
  173. if(!existsSync(configpath)){
  174. configPromise = performPull(configrepo)
  175. }
  176. else configPromise = Promise.resolve(true)
  177. configPromise.then(()=>{
  178. var manifestpath = '../'+brandName+'-config-'+nodeenv+'/repo-manifest';
  179. // var manifestpath = '../'+brandName+'-config-'+'production'+'/repo-manifest';
  180. console.dir(manifestpath)
  181. var repomanifest = require(manifestpath)()
  182. console.dir(repomanifest)
  183. var gitRepos = repomanifest.repos
  184. // Repositiories that have symlinks that required elevated priviletes in windows to create symlinks
  185. //
  186. var elevatedRunasRepos = repomanifest.elevated
  187. exludeMergeRepos = repomanifest.exludeMergeRepos
  188. // var productionRepos = [ 'elixir-config-production' ]
  189. // var productionIsAllowed = (nodeenv === 'production');
  190. // if(productionIsAllowed) gitRepos = gitRepos.concat(productionRepos)
  191. var env = Object.assign({}, process.env); // Shallow clone it.
  192. var __runcmd = function(label){
  193. var op = {
  194. 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
  195. , 'undefined' : ()=>{ return op.h(); }
  196. , 'reset' : ()=>{
  197. // Reset NPM packages semver so major versions can be updated.
  198. const fs = require('fs')
  199. const wipeDependencies = (package) => {
  200. const file = fs.readFileSync(package + '/package.json')
  201. const content = JSON.parse(file)
  202. for (var devDep in content.devDependencies) {
  203. if (content.devDependencies[devDep].match(/\W+\d+.\d+.\d+-?((alpha|beta|rc)?.\d+)?/g)) {
  204. content.devDependencies[devDep] = '*';
  205. }
  206. }
  207. for (var dep in content.dependencies) {
  208. if (content.dependencies[dep].match(/\W+\d+.\d+.\d+-?((alpha|beta|rc)?.\d+)?/g)) {
  209. content.dependencies[dep] = '*';
  210. }
  211. }
  212. fs.writeFileSync(package + '/package.json', JSON.stringify(content))
  213. }
  214. var repos = ['client'];
  215. // repos = gitRepos;
  216. repos.forEach(wipeDependencies)
  217. // if (require.main === module) {
  218. // } else {
  219. // module.exports = wipeDependencies
  220. // }
  221. }
  222. , 'upgrade' : ()=>{
  223. console.log('upgrade.......')
  224. var tasks = [
  225. ()=>{
  226. var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
  227. inherit : true, shell: true
  228. , env: process.env
  229. }).catch((e)=>{ console.error(e) })
  230. p.position = 1;
  231. console.log('One')
  232. return p;
  233. }
  234. , ()=>{
  235. var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
  236. inherit : true, shell: true
  237. , env: process.env
  238. }).catch((e)=>{ console.error(e) })
  239. p.position = 2;
  240. console.log('Two')
  241. return p;
  242. }
  243. , ()=>{
  244. var p = nodeShellExec('npm', ['install', '-g', 'n'], {
  245. inherit : true, shell: true
  246. , env: process.env
  247. }).catch((e)=>{ console.error(e) })
  248. p.position = 3;
  249. console.log('Three')
  250. return p;
  251. }
  252. , ()=>{
  253. var p = nodeShellExec('n', ['latest'], {
  254. inherit : true, shell: true
  255. , env: process.env
  256. }).catch((e)=>{ console.error(e) })
  257. p.position = 4;
  258. console.log('Four')
  259. return p;
  260. }
  261. ]
  262. any(tasks)
  263. console.log('.......done')
  264. console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
  265. console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
  266. console.info('Uninstalling existing ember globally') ;
  267. var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
  268. stdio: ['pipe', process.stdout, process.stderr],
  269. inherit : true,
  270. shell: true,
  271. cwd : path.dirname(__dirname),
  272. env: env
  273. })
  274. step1.on('close', ()=>{
  275. console.info('Installing ember globally') ;
  276. var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
  277. stdio: ['pipe', process.stdout, process.stderr],
  278. inherit : true,
  279. shell: true,
  280. cwd : path.dirname(__dirname),
  281. env: env
  282. })
  283. step2.on('close', ()=>{
  284. nodeShellExec('cmd', ['/c', 'ember', '--version'], {
  285. stdio: ['pipe', process.stdout, process.stderr],
  286. inherit : true,
  287. shell: true,
  288. cwd : path.dirname(__dirname),
  289. env: env
  290. })
  291. })
  292. })
  293. }
  294. , 'runas' : ()=>{
  295. console.log('Testing Elevation')
  296. return isRunningElevated().then(
  297. (isElevated) => {
  298. if(isElevated) {
  299. try {
  300. op[ processedArgs.label || processedArgs._[0] || 'h']()
  301. }
  302. catch(e){
  303. console.error('Error Invalid command : ' + e)
  304. fs.writeFileSync('run.done', 'success')
  305. }
  306. finally {
  307. }
  308. } else throw false;
  309. }
  310. )
  311. .catch(()=>{
  312. console.log('Requesting Elevated Privileges');
  313. // Wait for the runas to complete before we read it.
  314. try {
  315. fs.unlinkSync('run.done')
  316. fs.unlinkSync('run.log')
  317. }
  318. catch(e) { } //Ignore
  319. // Find node path to send to hta.
  320. return nodeShellExec('where', ['node']).then(r => {
  321. console.log('result : ' + r)
  322. // throw 'rrrrrrrrrrrrrrrrr'
  323. // PB : TODO -- Convert all the cli args back to string.
  324. var namedArgs = [];
  325. Object.keys(processedArgs).forEach((v)=>{ v!='_' ? namedArgs.push('--'+v+'='+processedArgs[v]) : null; })
  326. //console.log(' namedArgs : ' + namedArgs)
  327. var env = Object.assign({}, process.env); // Shallow clone it.
  328. // console.dir(env)
  329. env.NODE_ENV = process.env.NODE_ENV || 'development';
  330. var args = [__dirname + '/windowselevate.hta'].concat(processedArgs._).concat(namedArgs.join(' ')); args.push('--runas=self');
  331. args.push('--nodepath='+r[r.length-1])
  332. args.push('--node_env='+env.NODE_ENV)
  333. // args.push('--debug=true') // Enable to debug elevated..
  334. return nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
  335. , {
  336. inherit : true
  337. , shell: true
  338. , env: env
  339. , runas : 'self'
  340. , title : `runas`
  341. }
  342. ).then(()=>{
  343. // runas returned.
  344. try {
  345. var runaslog = JSON.parse('[ { "success" : true, "result" : "runas Log" }' + fs.readFileSync('run.log', { flags : 'a+'}) + ']');
  346. runaslog.forEach((logEntry)=>{
  347. statuslog.statuslog(logEntry.success ? null : (logEntry.messages || []).join(' '))
  348. logEntry.success ? (console.log(['success :' + logEntry.result]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
  349. })
  350. }
  351. catch(e){
  352. // Ignore nonexistent log
  353. console.warn('Run log error probably was not created by runas : ' + e)
  354. }
  355. })
  356. .catch(err => console.error('Elevation failed : ' + err));
  357. })
  358. })
  359. }
  360. , 'push' : ()=>{
  361. if(!processedArgs._[1]) { console.error('push all not supported. Specify repo name'); return }
  362. // init remote bare from local
  363. // pushandinitremotebare
  364. // https://www.jeffgeerling.com/blogs/jeff-geerling/push-your-git-repositories
  365. // connect to repo server -- net use 172.16.0.27
  366. // cd 172.16.0.27/repos/
  367. // mkdir repo.git
  368. // cd repo.git
  369. // git init --bare
  370. // cd localrepo
  371. // git remote rename origin githubclone
  372. // git remote add origin //172.16.0.27/repos/repo.git
  373. // git push origin master
  374. var repo = processedArgs._[1];
  375. var sequentialTaskShellCommands = [];
  376. if(!existsSync(`Z:/${repo}.git`)){
  377. sequentialTaskShellCommands = [
  378. // ['net', ['use', 'Z:', defaultRepoServer.replace('/','\\')], {
  379. // inherit : true, shell: true
  380. // , env: process.env
  381. // }]
  382. ['pwd', { cwd : 'Z:', inherit : true}]
  383. , ['mkdir', [`${repo}.git`], {
  384. cwd : `Z:`
  385. , inherit : true, shell: true
  386. , env: process.env
  387. }]
  388. , ['pwd', { cwd : `Z:/${repo}.git`, inherit : true}]
  389. , ['git', ['init', '--bare'], { cwd : `Z:/${repo}.git`
  390. , inherit : true, shell: true
  391. , env: process.env
  392. }]
  393. // PB : TODO -- Do this conditionally only...
  394. , ['git', ['remote', 'rename', 'origin', 'githubclone'], { cwd : `${repo}`}, (err)=>{
  395. console.log('Ignoring origin rename error : ' + err); return true; //return true to continue.
  396. } ] // PB ; Todo -- new repositories created locally will not have origin. Handle this failure.
  397. , ['git', ['remote', 'add', 'origin', `${defaultRepoServer}/${repo}.git`], { cwd : `${repo}`}]
  398. // PB : TODO -- If threre is a gitbubclone origin
  399. // Set the master to pull from the local repo.
  400. ]
  401. if(!existsSync(`Z:`)){
  402. sequentialTaskShellCommands.splice(0,0, ['net', ['use', 'Z:', defaultRepoServer.replace(/\//gm,'\\')], {
  403. inherit : true, shell: true
  404. , env: process.env
  405. }])
  406. console.warn('Adding network drive z: for repo server. ' + sequentialTaskShellCommands[0])
  407. // throw 'done'
  408. }
  409. }
  410. sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd : `${repo}`}])
  411. // console.dir(sequentialTaskShellCommands);
  412. var tasks = [];
  413. sequentialTaskShellCommands.forEach(shellcmd => {
  414. // console.log(shellcmd)
  415. tasks.push(()=>{
  416. var p = nodeShellExec.apply(null, shellcmd.slice(0,3)).catch((e)=>{ if(shellcmd[3]) { return shellcmd[3]() } else { console.error(e);} })
  417. return p;
  418. })
  419. })
  420. any(tasks);
  421. }
  422. , 'is-git-repo' : (dir)=>{
  423. return nodeShellExec('git', ['-C', dir.name, 'rev-parse'], { stdio : 'ignore'})
  424. }
  425. , 'set-url' : (remotename, url) => {
  426. var pushable = processedArgs.pushable || false;
  427. remotename = remotename || processedArgs._[1]
  428. url = url || processedArgs._[2]
  429. var serial_perform_git_seturl = (repo)=>{
  430. var options = { cwd : repo }
  431. // console.log(repo)
  432. if(pushable) {
  433. return [
  434. ['git', ['remote', 'set-url', remotename, url + '/' + repo], { cwd : repo }]
  435. ]
  436. }
  437. else {
  438. console.error('not supported for non-pushable')
  439. }
  440. }
  441. var x = (args)=>{
  442. return ()=>{
  443. // console.log(args)
  444. return nodeShellExec.apply(null, args)
  445. }
  446. // return Promise.resolve(true)
  447. }
  448. var perform_git_seturl = (dir)=>{
  449. op['is-git-repo'](dir).then((code)=>{
  450. any( serial_perform_git_seturl(dir.name).map(x) )
  451. }).catch((e)=>{
  452. // console.log('Failed : ' + dir.name)
  453. })
  454. }
  455. const { readdir } = require("fs").promises
  456. const dirs = async (perform, path) => {
  457. for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
  458. if (dir.isDirectory()) perform(dir)
  459. }
  460. }
  461. dirs( perform_git_seturl)
  462. }
  463. , 'add' : (remotename, url, branch) => {
  464. var pushable = processedArgs.pushable || false;
  465. remotename = remotename || processedArgs._[1]
  466. url = url || processedArgs._[2]
  467. branch = branch || processedArgs._[3]
  468. var serial_perform_git_add = (repo)=>{
  469. var options = { cwd : repo }
  470. // console.log(repo)
  471. if(pushable) {
  472. return [
  473. ['git', ['remote', 'add', remotename, url + '/' + repo], { cwd : repo }]
  474. , ['git', ['pull', remotename, branch], { cwd : repo }]
  475. , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd : repo }]
  476. ]
  477. }
  478. else {
  479. return [
  480. ['git', ['remote', 'add', remotename, url + '/' + repo], { cwd : repo }]
  481. , ['git', ['remote', `set-url`, '--push', remotename, 'no-pushing'], { cwd : repo }]
  482. , ['git', ['pull', remotename, branch], { cwd : repo }]
  483. , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd : repo }]
  484. ]
  485. }
  486. }
  487. var x = (args)=>{
  488. return ()=>{
  489. // console.log(args)
  490. return nodeShellExec.apply(null, args)
  491. }
  492. // return Promise.resolve(true)
  493. }
  494. var perform_git_add = (dir)=>{
  495. op['is-git-repo'](dir).then((code)=>{
  496. // console.log(code)
  497. if(code) {
  498. nodeShellExec('git',['remote', 'get-url', remotename], { cwd : dir.name, stdio : 'ignore' }).then(()=>{
  499. console.log('skipped : ' + dir.name + ', reason : A remote with same name already exists.')
  500. })
  501. .catch((e)=>{
  502. any( serial_perform_git_add(dir.name).map(x) )
  503. })
  504. }
  505. // else console.log('Skipped : Not a Git Repo : ' + dir.name)
  506. }).catch((e)=>{
  507. // console.log('Failed : ' + dir.name)
  508. })
  509. }
  510. const { readdir } = require("fs").promises
  511. const dirs = async (perform, path) => {
  512. for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
  513. if (dir.isDirectory()) perform(dir)
  514. }
  515. }
  516. dirs(perform_git_add)
  517. }
  518. , 'remove' : (remotename) => {
  519. remotename = remotename || processedArgs._[1]
  520. var serial_perform_git_remove = (repo)=>{
  521. var options = { cwd : repo }
  522. // console.log(repo)
  523. return [
  524. ['git', ['remote', 'remove', remotename], { cwd : repo }]
  525. ]
  526. }
  527. var x = (args)=>{
  528. return ()=>{
  529. // console.log(args)
  530. return nodeShellExec.apply(null, args)
  531. }
  532. // return Promise.resolve(true)
  533. }
  534. var perform_git_remove = (dir)=>{
  535. op['is-git-repo'](dir).then((code)=>{
  536. // console.log(code)
  537. if(code) {
  538. nodeShellExec('git',['remote', 'get-url', remotename], { cwd : dir.name, stdio : 'ignore' }).then(()=>{
  539. any( serial_perform_git_remove(dir.name).map(x) )
  540. })
  541. .catch((e)=>{
  542. console.log('skipped : ' + dir.name + ', reason : No remote named origin')
  543. })
  544. }
  545. // else console.log('Skipped : Not a Git Repo : ' + dir.name)
  546. }).catch((e)=>{
  547. // console.log('Failed : ' + dir.name)
  548. })
  549. }
  550. const { readdir } = require("fs").promises
  551. const dirs = async (perform, path) => {
  552. for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
  553. if (dir.isDirectory()) perform(dir)
  554. }
  555. }
  556. dirs(perform_git_remove)
  557. }
  558. , 'init-gitea' : (user) => {
  559. user = user || processedArgs._[1]
  560. if(!user) throw 'User name required'
  561. var serial_perform_init_gitea = (repo)=>{
  562. var options = { cwd : repo }
  563. // console.log(repo)
  564. return [
  565. ['git', ['remote', 'add', 'chess', `${defaultRepoServer}/${user}/${repo}.git`], { cwd : repo }]
  566. , ['git', ['remote', 'set-url', '--push', 'chess', 'no-pushing'], { cwd : repo }]
  567. , ['git', ['remote', 'set-url', 'origin', `${defaultRepoServer}/${user}/${repo}.git`], { cwd : repo }]
  568. ]}
  569. var x = (args)=>{
  570. return ()=>{
  571. // console.log(args)
  572. return nodeShellExec.apply(null, args)
  573. }
  574. // return Promise.resolve(true)
  575. }
  576. var perform_init_gitea = (dir)=>{
  577. op['is-git-repo'](dir).then((code)=>{
  578. // console.log(code)
  579. if(code) {
  580. nodeShellExec('git',['remote', 'get-url', 'chess'], { cwd : dir.name, stdio : 'ignore' }).then(()=>{
  581. console.log('skipped : ' + dir.name + ', reason : Already has remote chess ')
  582. })
  583. .catch((e)=>{
  584. any( serial_perform_init_gitea(dir.name).map(x) )
  585. })
  586. }
  587. // else console.log('Skipped : Not a Git Repo : ' + dir.name)
  588. }).catch((e)=>{
  589. // console.log('Failed : ' + dir.name)
  590. })
  591. }
  592. const { readdir } = require("fs").promises
  593. const dirs = async (perform, path) => {
  594. for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
  595. if (dir.isDirectory()) perform(dir)
  596. }
  597. }
  598. dirs(perform_init_gitea)
  599. }
  600. , 'syncmaster' : (label) => {
  601. // Usage :
  602. // elxr pull -- Defaults to run config
  603. var env = Object.assign({}, process.env); // Shallow clone it.
  604. // console.dir(env)
  605. console.log('Running exlr pull : ' + path.dirname(__dirname))
  606. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  607. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  608. // stdio: ['pipe', process.stdout, process.stderr],
  609. // inherit : true,
  610. // shell: true,
  611. // cwd : path.dirname(__dirname),
  612. // env: env
  613. // })
  614. if(!processedArgs.runas) gitRepos.forEach(performPull)
  615. return isRunningElevated().then(
  616. (isElevated) => {
  617. if(isElevated) {
  618. any(elevatedRunasRepos.map((repo)=>performPull(repo))).then(()=>{
  619. fs.writeFileSync('run.done', 'success')
  620. }).catch(()=>{
  621. fs.writeFileSync('run.done', 'error')
  622. })
  623. }
  624. else throw false;
  625. }
  626. ).catch(
  627. () => {
  628. op['runas']()
  629. }
  630. )
  631. }
  632. , 'pull' : (label) => {
  633. // Usage :
  634. // elxr pull -- Defaults to run config
  635. var env = Object.assign({}, process.env); // Shallow clone it.
  636. // console.dir(env)
  637. console.log('Running exlr pull : ' + path.dirname(__dirname))
  638. // nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
  639. // // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
  640. // stdio: ['pipe', process.stdout, process.stderr],
  641. // inherit : true,
  642. // shell: true,
  643. // cwd : path.dirname(__dirname),
  644. // env: env
  645. // })
  646. var useGitPull = processedArgs.useGitPull || false;
  647. var getPullCmd = (repo)=>{
  648. // console.log(useGitPull)
  649. var pullCmd = [ gitInstallDir
  650. , ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
  651. , { cwd : repo, title : 'pull all origins for ' + repo }]
  652. // var pullCmd = ['pullall', [], { cwd : repo }]
  653. if(useGitPull) pullCmd = ['git', ['pull'], {
  654. inherit : true, shell: true,
  655. cwd : repo
  656. // , env: process.env
  657. , runas : processedArgs.runas
  658. , title : `git pull ${repo}`
  659. }]
  660. return pullCmd
  661. }
  662. var performPull = (repo) => {
  663. if(existsSync(repo)) {
  664. console.log('pulling ' + repo)
  665. return nodeShellExec.apply(null, getPullCmd(repo)).then(()=>{
  666. if(__isElevated) {
  667. fs.writeFileSync('run.log', ', ' + JSON.stringify({ repo, success:true}), {'flag':'a+'} )
  668. }
  669. else statuslog.statuslog(null, repo)
  670. })
  671. .catch((e)=>{
  672. e.repo = repo;
  673. if(__isElevated) {
  674. fs.writeFileSync('run.log', ', ' + JSON.stringify(e), {'flag':'a+'} )
  675. }
  676. else statuslog.statuslog(e); console.error(e)
  677. })
  678. }
  679. else {
  680. // PB : TODO -- detect if a clonable repo exists in currentGitAuthUser
  681. return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'],
  682. {
  683. inherit : true, shell: true,
  684. env: process.env
  685. , runas : processedArgs.runas
  686. }).catch((e)=>{
  687. throw e;
  688. }).then(()=>{
  689. return nodeShellExec('git', ['config', '--replace-all' , 'core.symlinks', true],
  690. {
  691. inherit : true, shell: true,
  692. env: process.env
  693. , cwd : repo
  694. , runas : processedArgs.runas
  695. , title : `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
  696. })
  697. .then(()=>{
  698. if(__isElevated) {
  699. fs.writeFileSync('run.log', ', ' + JSON.stringify({ repo, success:true}), {'flag':'a+'} )
  700. }
  701. else statuslog.statuslog(null, repo)
  702. })
  703. .catch((e)=>{
  704. e.repo = repo;
  705. if(__isElevated) {
  706. fs.writeFileSync('run.log', ', ' + JSON.stringify(e), {'flag':'a+'} )
  707. }
  708. else statuslog.statuslog(e);
  709. })
  710. })
  711. .catch(e=>{
  712. e.repo = repo;
  713. if(__isElevated) {
  714. fs.writeFileSync('run.log', ', ' + JSON.stringify(e), {'flag':'a+'} )
  715. }
  716. else statuslog.statuslog(e);
  717. })
  718. }
  719. }
  720. return isRunningElevated().then(
  721. (isElevated) => {
  722. if(isElevated) {
  723. any(elevatedRunasRepos.map((repo)=>performPull(repo))).then(()=>{
  724. fs.writeFileSync('run.done', 'success')
  725. }).catch(()=>{
  726. fs.writeFileSync('run.done', 'error')
  727. })
  728. }
  729. else throw false;
  730. }
  731. ).catch(
  732. () => {
  733. op['runas']().then(()=>{
  734. })
  735. .catch(()=>{})
  736. .finally(()=>{
  737. if(!processedArgs.runas) {
  738. var pendingpulls = [];
  739. gitRepos.forEach( (r)=>{ pendingpulls.push(performPull(r)) } )
  740. Promise.all(pendingpulls).then(results =>{
  741. }).finally(Traq.finally)
  742. }
  743. })
  744. }
  745. )
  746. }
  747. , 'isInstalled' : ()=>{
  748. return nodeShellExec('where', [processedArgs._[1]], { inherit : true} ).then(()=>{
  749. console.log(processedArgs._[1] + ' exists.')
  750. return true;
  751. });
  752. }
  753. , 'npmi' : ()=>{
  754. var tasks = [];
  755. var bowerRepos = ['client']
  756. var npmbuildrepos = ['loopback-jsonapi-model-serializer']
  757. npmbuildrepos.forEach(repo => {
  758. tasks.push(()=>{
  759. var p = nodeShellExec('npm', ['i --force'], {
  760. inherit : true, shell: true
  761. , cwd : repo
  762. , env: process.env
  763. , title : `npm i for ${repo}`
  764. }).catch((e)=>{
  765. console.error('Ignoring Benign Error'); console.error(e)
  766. }).then(()=>{
  767. return nodeShellExec('npm', ['run build'], {
  768. inherit : true, shell: true
  769. , cwd : repo
  770. , env: process.env
  771. , title : `npm run build for ${repo}`
  772. }).catch(Tasq.catch)
  773. })
  774. return p;
  775. })
  776. })
  777. any(tasks).then(()=>{
  778. gitRepos = gitRepos.concat(elevatedRunasRepos);
  779. gitRepos.push('client/server');
  780. var repotasks = []
  781. gitRepos.forEach(repo => {
  782. console.log('npm i for ' + repo)
  783. repotasks.push(
  784. nodeShellExec('rm', ['package-lock.json'], {
  785. inherit : true, shell: true
  786. , cwd : repo
  787. , env: process.env
  788. , title : `rm 'package-lock.json' for ${repo}`
  789. }).catch((e)=>{console.error(e)})
  790. .then(()=>{
  791. if( npmbuildrepos.indexOf(repo) < 0) {
  792. // tasks.push(()=>{
  793. var p = nodeShellExec('npm', ['i --force'], {
  794. inherit : true, shell: true
  795. , cwd : repo
  796. , env: process.env
  797. , title : `npm i for ${repo}`
  798. }).catch(Tasq.catch)
  799. return p;
  800. // })
  801. }
  802. })
  803. )
  804. })
  805. bowerRepos.forEach(repo => {
  806. repotasks.push(()=>{
  807. var p = nodeShellExec('bower', ['install'], {
  808. inherit : true, shell: true
  809. , cwd : repo
  810. , env: process.env
  811. , title : `bower i for ${repo}`
  812. }).catch(Tasq.catch)
  813. return p;
  814. })
  815. })
  816. return any(repotasks);
  817. }).catch(e=>{
  818. }).finally(statuslog.finally)
  819. }
  820. , 'start' : (label)=>{
  821. console.log('Starting Elixir Server.');
  822. var env = Object.assign({}, process.env); // Shallow clone it.
  823. // console.dir(env)
  824. env.NODE_ENV = process.env.NODE_ENV || 'development';
  825. env.DEBUG = 'loopback:connector:' + dbForLabel(label)
  826. var cmd = env.NODE_ENV === 'development' ? 'nodemon' : 'node';
  827. // cmd = 'node'
  828. cmd = [cmd, ['--inspect=9228', 'elixir/server.js']]
  829. var childPromise = nodeShellExec(...cmd, {
  830. // inherit : true,
  831. shell: true,
  832. detached: true,
  833. stdio: 'ignore',
  834. cwd : 'elixir-server'
  835. , env: env
  836. })
  837. var child = childPromise.process;
  838. if (typeof child.pid !== 'undefined') {
  839. console.log(`started Elixir Server PID(${child.pid}) : NODE_ENV=${process.NODE_ENV} ${cmd}`);
  840. fs.writeFileSync('.elixir-server.elixir.server.pid', child.pid, {
  841. encoding: 'utf8'
  842. })
  843. }
  844. // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
  845. // inherit : true,
  846. // shell: true, detached: true,
  847. // cwd : 'qms/server',
  848. // env: env,
  849. // shell : true
  850. // })
  851. // nodeShellExec('ember', ['s'], {
  852. // // inherit : true,
  853. // shell: true, detached: true,
  854. // cwd : 'client/',
  855. // env: env
  856. // })
  857. console.log('Starting Elixir Client Host.');
  858. var cmd = ['ember', ['s']]
  859. var childPromise = nodeShellExec(...cmd, {
  860. // var childPromise = nodeShellExec('node', ['--inspect=9227', './node_modules/.bin/ember', 's'], {
  861. // PB : TODO -- ember debugging.
  862. // inherit : true,
  863. shell: true,
  864. detached: true,
  865. stdio: 'ignore',
  866. cwd : 'client'
  867. , env: env
  868. })
  869. // .catch(e=>console.error(e))
  870. child = childPromise.process;
  871. if (typeof child.pid !== 'undefined') {
  872. console.log(`started Elixir Client Host PID(${child.pid}) : NODE_ENV=${process.NODE_ENV} ${cmd}`);
  873. fs.writeFileSync('.client.server.pid', child.pid, {
  874. encoding: 'utf8'
  875. })
  876. }
  877. }
  878. , 'stop' : (label)=>{
  879. const kill = require('tree-kill');
  880. var serverPid = fs.readFileSync('.elixir-server.elixir.server.pid', {
  881. encoding: 'utf8'
  882. })
  883. fs.unlinkSync('.elixir-server.elixir.server.pid')
  884. console.log(serverPid)
  885. kill(serverPid)
  886. serverPid = fs.readFileSync('.client.server.pid', {
  887. encoding: 'utf8'
  888. })
  889. fs.unlinkSync('.client.server.pid')
  890. console.log(serverPid)
  891. kill(serverPid)
  892. }
  893. , 'use' : ()=>{
  894. // use a certain named instance.
  895. // Eg :
  896. // 1) elxr use elixir
  897. // 2) elxr use cihsr
  898. // If environment is not specified defaults to development.
  899. // 1) NODE=test elxr use elixir
  900. /*// Steps
  901. 1) Delete Config and Data symlinks
  902. 2) Make Links for config ({{name}}-config-{{node_env}}) and data with the NODE_ENV specified or default to dev
  903. 3) Iterates all repos and pull all. 'git', ['pull', '--all'].
  904. 4) Iterates all repos and checkout to the ENV specified. 'git', ['checkout', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
  905. 5) Iterates all repos and merge from source configured in mergeSource. 'git', ['merge', mergeSource],
  906. */
  907. var runconfig = { NODE_ENV : process.env.NODE_ENV }
  908. try { runconfig = Object.assign(runconfig, require('../run.js')) } catch(e) { }
  909. if((!processedArgs.runas || processedArgs.runas !== 'self') &&
  910. runconfig.NODE_ENV && runconfig.NODE_ENV === (process.env.NODE_ENV || runconfig.NODE_ENV) &&
  911. processedArgs._[1] && runconfig.use === processedArgs._[1]) {
  912. console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`)
  913. if(processedArgs.runas) { fs.writeFileSync('run.done', 'success') }
  914. return
  915. }
  916. var tasks = [
  917. ()=>{
  918. if(existsSync('config')) {
  919. var p = nodeShellExec('rmdir', ['config'], {inherit : true, shell: true, env: process.env }
  920. ).catch((err)=>{ console.log('Ignoring benign error : ' + err); return true; })
  921. return p;
  922. }
  923. else return Promise.resolve(true);
  924. },
  925. ()=>{
  926. if(existsSync('data')) {
  927. var p = nodeShellExec('rmdir', ['data'], { inherit : true, shell: true, env: process.env }
  928. ).catch((err)=>{ console.log('Ignoring benign error : ' + err); return true; })
  929. return p;
  930. }
  931. else return Promise.resolve(true);
  932. },
  933. ];
  934. runconfig.NODE_ENV = process.env.NODE_ENV = process.env.NODE_ENV || runconfig.NODE_ENV || 'development';
  935. if(processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1];
  936. if(!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' }
  937. // console.log(process.env.cwd)
  938. fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig))
  939. var checkoutMap = {
  940. 'development' : 'master',
  941. }
  942. // cant use git checkout -b it fails with branch already exists.
  943. var performCheckout = (repo, branch)=>{
  944. if(!branch) return Promise.resolve({ 'skipped' : true })
  945. if(excludeCheckouts[repo]) return Promise.resolve({ 'skipped' : true })
  946. return nodeShellExec('git', ['checkout', branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV], {
  947. // return nodeShellExec('git', ['switch', '-m', '-C', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV], {
  948. // inherit : true, shell: true,
  949. cwd : repo
  950. // , stdio : ignore // Use when we want to silcence output completely.
  951. , runas : processedArgs.runas
  952. , title : `git checkout ${branch ||checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} for ${repo}`
  953. }).catch((e)=>{ console.error(e); return { error : true, message : repo} })
  954. }
  955. if(runconfig.NODE_ENV === 'development') performCheckout = ()=>{ return Promise.resolve(true) }
  956. var performPullAll = (repo)=>{
  957. if(excludeCheckouts[repo]) return Promise.resolve({ 'skipped' : true })
  958. return nodeShellExec('git', ['pull', '--all'], {
  959. // inherit : true, shell: true,
  960. cwd : repo
  961. , runas : processedArgs.runas
  962. , title : `git pull -all for ${checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} ${repo}`
  963. }).catch((e)=>{ console.error(e); return { error : true, message : repo} })
  964. }
  965. var mergeSources = {
  966. 'development' : null,
  967. 'test' : 'master',
  968. 'production' : 'master'
  969. }
  970. var excludeCheckouts = Object.assign(exludeMergeRepos)
  971. delete excludeCheckouts[`elixir-config-${runconfig.NODE_ENV}`]
  972. delete excludeCheckouts[`cihsr-config-${runconfig.NODE_ENV}`]
  973. var mergeSource = mergeSources[checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
  974. var performMerge = (repo)=>{
  975. if(exludeMergeRepos[repo]) return Promise.resolve({ 'skipped' : true })
  976. return nodeShellExec('git', ['merge', mergeSource], {
  977. inherit : true, shell: true,
  978. cwd : repo
  979. , runas : processedArgs.runas
  980. }).catch((e)=>{ console.error(e) })
  981. }
  982. if(runconfig.NODE_ENV === 'development') performMerge = ()=>{ return Promise.resolve(true) }
  983. any(tasks).then(()=>{
  984. if(!processedArgs.runas) return op['runas']()
  985. tasks = [
  986. ()=>{
  987. // Use junctions to avoid npm package issues
  988. var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-config' + '-' + process.env.NODE_ENV ], {
  989. inherit : true, shell: true
  990. , env: process.env
  991. }).catch((e)=>{ console.error(e) })
  992. return p;
  993. }
  994. ];
  995. if(processedArgs._[1]) {
  996. tasks = tasks.concat(
  997. [
  998. ()=>{
  999. var p = nodeShellExec('mklink', ['/J', 'data', runconfig.use + '-data'], {
  1000. inherit : true, shell: true
  1001. , env: process.env
  1002. }).catch((e)=>{ console.error(e) })
  1003. return p;
  1004. }
  1005. ]
  1006. )
  1007. }
  1008. return any(tasks)
  1009. //target is the env is we specify in elxr use command. Default is dev
  1010. .then( //Switch to target branch
  1011. () => any([ any(gitRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))
  1012. , any(elevatedRunasRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))]) )
  1013. .then( //PULL from target branch
  1014. () => any([ any(gitRepos.map((repo)=>performPullAll(repo))), any(elevatedRunasRepos.map((repo)=>performPullAll(repo)))]) )
  1015. .then( //Switch to merge source branch
  1016. () => any([ any(gitRepos.map((repo)=>performCheckout(repo, mergeSources[process.env.NODE_ENV || 'development'] )))
  1017. , any(elevatedRunasRepos.map((repo)=>performCheckout(repo, mergeSources[process.env.NODE_ENV || 'development'])))]) )
  1018. .then( //Pull on merge source branch
  1019. () => any([ any(gitRepos.map((repo)=>performPullAll(repo))), any(elevatedRunasRepos.map((repo)=>performPullAll(repo)))]) )
  1020. .then( //Switch to target branch
  1021. () => any([ any(gitRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))
  1022. , any(elevatedRunasRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))]) )
  1023. .then( //Merge source branch to target branch
  1024. () => any([ any(gitRepos.map((repo)=>performMerge(repo))).catch(err=>{ console.error('error in performMerge ' + err)}) ,
  1025. any(elevatedRunasRepos.map((repo)=>performMerge(repo))).catch(err=>{ console.error('error in performMerge ' + err)})]) )
  1026. .then( () => {
  1027. // Move test config from dev.
  1028. // if(process.env.NODE_ENV === 'test'){
  1029. // var devcfgreponame = runconfig.use + '-config' + '-development';
  1030. // var testcfgreponame = runconfig.use + '-config' + '-test';
  1031. // var testcfgdir = path.dirname(__dirname) + '/' + testcfgreponame + '/'
  1032. // var devcfgdir = path.dirname(__dirname) + '/' + devcfgreponame + '/' //eg (elxr/../elixir-config.development)
  1033. // return any([
  1034. // ()=>{
  1035. // return nodeShellExec('git', ['checkout', 'test'], {
  1036. // inherit : true, shell: true,
  1037. // cwd : testcfgdir
  1038. // // , env: process.env
  1039. // , runas : processedArgs.runas
  1040. // , title : `git checkout test for ${testcfgreponame}`
  1041. // }).catch((e)=>{ console.error(e) })
  1042. // }
  1043. // , ()=> {
  1044. // return nodeShellExec('git', ['checkout', 'master'], {
  1045. // inherit : true, shell: true,
  1046. // cwd : devcfgdir
  1047. // // , env: process.env
  1048. // , runas : processedArgs.runas
  1049. // , title : `git checkout master for ${devcfgreponame}`
  1050. // }).catch((e)=>{ console.error(e) })
  1051. // }
  1052. // , ()=> {
  1053. // globSync( '**/*.test.js', {cwd : devcfgdir}).map((filename) => {
  1054. // console.log('File found : ' + devcfgdir + filename)
  1055. // fs.copyFileSync(devcfgdir + filename, testcfgdir+ filename);
  1056. // })
  1057. // return nodeShellExec('git', ['checkout', 'test'], {
  1058. // inherit : true, shell: true,
  1059. // cwd : devcfgdir
  1060. // // , env: process.env
  1061. // , runas : processedArgs.runas
  1062. // , title : `git checkout test for ${devcfgreponame}`
  1063. // }).catch((e)=>{ console.error(e) })
  1064. // }
  1065. // ])
  1066. // }
  1067. // else {
  1068. return Promise.resolve(true)
  1069. // }
  1070. })
  1071. .then(()=>{
  1072. fs.writeFileSync('run.done', 'success')
  1073. }).catch(()=>{
  1074. fs.writeFileSync('run.done', 'error')
  1075. })
  1076. }).catch(()=>{
  1077. fs.writeFileSync('run.done', 'error')
  1078. })
  1079. // Antibiotic stewardship program.
  1080. // 1st use is fine.
  1081. // Max vials dispense
  1082. // 2nd use Pharmacy needs justification Form.
  1083. // Approval after a certain period of time.
  1084. }
  1085. , 'g' : ()=>{
  1086. if(processedArgs.h) {
  1087. console.log('elxr g [modelname] => generate a model named [modelname]');
  1088. console.log('elxr g => regenerate all known models');
  1089. return
  1090. }
  1091. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  1092. // console.log('Starting directory: ' + process.cwd());
  1093. // try {
  1094. // child = child.on('close', () => { process.chdir(label) } );
  1095. // console.log('New directory: ' + process.cwd());
  1096. // }
  1097. // catch (err) {
  1098. // console.log('chdir: ' + err);
  1099. // }
  1100. // child.on('close', function(){
  1101. // var options = {
  1102. // shell : true
  1103. // , inherit : true
  1104. // // , cwd : '' + process.cwd
  1105. // // , env : process.env
  1106. // };
  1107. // nodeShellExec('git', ['init'], { inherit : true});
  1108. if(0){
  1109. // PB : TODO -- Special google chrome profile for tests etc.
  1110. nodeShellExec('pwd', { inherit : true});
  1111. // /c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe --user-data-dir=/c/chess/instances/elixir_01/data/Google/Chrome/User\ Data --profile-directory="chess"
  1112. // "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\chess\instances\elixir_01\data\Google\Chrome\User Data" --profile-directory="chess" http://localhost:4200/admin/crud/create/item
  1113. // "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\chess\instances\elixir_01\data\Google\Chrome\User Data" --profile-directory="chess" http://localhost:4200/tests/index.html?grep=loopback
  1114. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  1115. "--profile-directory=Profile 1"
  1116. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  1117. }
  1118. // nodeShellExec('npm', ['init', '-y'], options);
  1119. // nodeShellExec('npm', ['init', '-y'], options);
  1120. // })
  1121. var g = {
  1122. 'client' : ()=>{
  1123. console.info('Creating new ember client named : ' + processedArgs._[2] ) ;
  1124. var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
  1125. stdio: ['pipe', process.stdout, process.stderr],
  1126. inherit : true,
  1127. shell: true,
  1128. cwd : path.dirname(__dirname),
  1129. env: env
  1130. })
  1131. },
  1132. 'modelr' : ()=>{
  1133. var tasks = [
  1134. ()=>{
  1135. var p = nodeShellExec('"ember"', [
  1136. 'g'
  1137. , 'modelr'
  1138. , processedArgs._[2]], {
  1139. inherit : true, shell: true, env: process.env
  1140. }).then(()=>{
  1141. console.log('Blueprint generation complete for : ' + processedArgs._[2])
  1142. return true;
  1143. }).catch((e)=>{ console.error(e) })
  1144. return p;
  1145. },
  1146. ()=>{
  1147. var chromePrefsFile = "C:\\chess\\instances\\elixir_01\\data\\Google\\Chrome\\User Data\\chess\\Preferences";
  1148. var chromeprefs = fs.readFileSync(chromePrefsFile, { encoding: 'utf8' })
  1149. chromeprefs = JSON.parse(chromeprefs)
  1150. var previous = chromeprefs.download.default_directory;
  1151. var parentDir = path.dirname(__dirname);
  1152. chromeprefs.download.default_directory = parentDir + "\\client\\app\\templates\\components\\resource";
  1153. fs.writeFileSync(chromePrefsFile, JSON.stringify(chromeprefs))
  1154. // PB : TODO -- detect where chrome is installed.
  1155. // PB : TODO -- set the download dir to the place where files are needed.
  1156. var p = nodeShellExec('"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"', [
  1157. '--user-data-dir="C:\\chess\\instances\\elixir_01\\data\\Google\\Chrome\\User Data"'
  1158. , '--profile-directory="chess"'
  1159. , 'http://localhost:4200/admin/crud/create/' + processedArgs._[2]], {
  1160. inherit : true, shell: true
  1161. , env: process.env
  1162. }).then(()=>{
  1163. chromeprefs.download.default_directory = previous;
  1164. fs.writeFileSync(chromePrefsFile, JSON.stringify(chromeprefs))
  1165. return true;
  1166. }).catch((e)=>{ console.error(e) })
  1167. return p;
  1168. }
  1169. ,
  1170. ()=>{
  1171. console.log('Browser process should have closed here....')
  1172. return true;
  1173. }
  1174. ]
  1175. any(tasks)
  1176. }
  1177. }
  1178. g[processedArgs._[1]]();
  1179. }
  1180. }
  1181. return op[label] ? op[label]() : null;
  1182. }
  1183. // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
  1184. // mysql -u root -p db_name < drop_all_tables.sql
  1185. var mysql = '../xampp/mysql/bin/mysql'
  1186. var mysqldump = '../xampp/mysql/bin/mysqldump'
  1187. // --runas
  1188. if(processedArgs.runas) {
  1189. // Weve been asked to run in priviledged mode. Check if we already are privileged.
  1190. __runcmd('runas')
  1191. }
  1192. else __runcmd(processedArgs.label || processedArgs._[0] || 'h');
  1193. })