12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805 |
- var path = require('path');
- var utils = require('bbhverse');
- var any = utils.any;
- var Tasq = utils.Tasq
- var statuslog = utils.Traq
- var Traq = utils.Traq
- Tasq.addlistener(statuslog.statuslog)
- var cli = require('./cliverse')
- var nodeShellExec = cli.nodeShellExec;
- var chalk = require('chalk')
-
-
- const BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]';
- function getVersion() { return BUILD_VERSION; }
- console.log(getVersion())
-
- // 'use strict';
-
- // PB : TODO -- make sure folder context is proper coz we can now run elxr from anywhere.
-
- // PB : TODO --
- // runas bypass non elevated tasks when we request privelege
- // runas message is always error needs to be fixed.
- // runas wait needs to be parallelized.
- // suppress elevation check error messages
- // support runas lauched directly from shell.
- // pass in environment in hta to shellexecute.
-
- const { existsSync } = require('fs');
- const fs = require('fs')
-
- const cliargs = utils.cliargs;
- const processedArgs = cliargs(process.argv.slice(2));
- console.dir(processedArgs)
- var globSync = require('glob').sync;
-
- var ENV = Object.assign({}, process.env); // Shallow clone it.
-
- const { readdir } = require("fs").promises
-
- // Directory shallow walk and do perform on each dir.
- const dirs = async (perform, path) => {
- for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
- if (dir.isDirectory()) perform(dir)
- }
- }
-
- var getShellTask = (command, args, options) => {
- options = options || {}
- return () => {
- var p = nodeShellExec.apply(null, [command, args, Object.assign({
- inherit: true, shell: true, env: ENV, title: `${command} ${args}`
- }, options)
- ])
- if (options.ignorefailures) {
- return p.catch(e => {
- // Ignore. Not a major error.
- })
- }
- else return p;
- }
- }
-
- var callsheltask = (args) => { return () => { return nodeShellExec.apply(null, args) } }
-
- var getTaskCheckExists = (command, options) => {
- options = options || {}
- return () => {
- var p = nodeShellExec.apply(null, ['where', [command]])
- if (options.ignorefailures) {
- return p.then(() => { return true }).catch(e => { // Ignore. Not a major error.
- return false;
- })
- }
- else return p.then(() => { return true });
- }
- }
-
- var getPullCmd = (repo, branch) => {
- // console.log(useGitPull)var getPullCmd = (repo, branch) => {
- // console.log(useGitPull)
-
- if(branch) {
- var pullCmd = [ gitbash
- , ['-c', 'for i in `git remote`; do git pull $i ' + branch + '; done;']
- , { cwd: instanceroot + '/' + repo, title : 'pull all origins for ' + repo }]
- }
- else {
- var pullCmd = [gitbash
- , ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
- , { cwd: instanceroot + '/' + repo, title: 'pull all origins for ' + repo }]
- }
- // var pullCmd = ['pullall', [], { cwd : repo }]
- if (useGitPull) pullCmd = ['git', ['pull'], {
- inherit: true, shell: true,
- cwd: instanceroot + '/' + repo
- // , env: process.env
- , runas: processedArgs.runas
- , title: `git pull ${repo}`
- }]
- return pullCmd
- }
-
- // var getPullCmd = (repo, branch) => {
- // var pullCmd = [gitbash
- // , ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
- // , { cwd: instanceroot + '/' + repo, title: 'pull all origins for ' + repo }]
- // // var pullCmd = ['pullall', [], { cwd : repo }]
- // if (useGitPull) pullCmd = ['git', ['pull'], {
- // inherit: true, shell: true,
- // cwd: instanceroot + '/' + repo
- // // , env: process.env
- // , runas: processedArgs.runas
- // , title: `git pull ${repo}`
- // }]
- // return pullCmd
- // }
-
- var performPull = (repo, branch) => {
- if (existsSync(instanceroot + '/' + repo)) {
- var branchprint = branch ? ' branch :' + branch : '';
- console.log('pulling ' + instanceroot + '/' + repo + branchprint )
- return nodeShellExec.apply(null, getPullCmd(repo, branch)).then(() => {
- if (__isElevated) {
- fs.writeFileSync('run.log', ', ' + JSON.stringify({ repo, success: true }), { 'flag': 'a+' })
- }
- else statuslog.statuslog(null, repo)
- return true;
- })
- .catch((e) => {
- e.repo = repo;
- if (__isElevated) {
- fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
- }
- else statuslog.statuslog(e); console.error(e)
- })
- }
- else {
- console.log('cloning ' + repo)
- // PB : TODO -- detect if a clonable repo exists in currentGitAuthUser
- return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'],
- {
- inherit: true, shell: true,
- env: process.env
- , cwd : instanceroot
- , runas: processedArgs.runas
- }).catch((e) => {
- throw e;
- }).then(() => {
-
- return nodeShellExec('git', ['config', '--replace-all', 'core.symlinks', true],
- {
- inherit: true, shell: true,
- env: process.env
- , cwd: instanceroot + '/' + repo
- , runas: processedArgs.runas
- , title: `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
- })
- .then(() => {
- if (__isElevated) {
- fs.writeFileSync('run.log', ', ' + JSON.stringify({ repo, success: true }), { 'flag': 'a+' })
- }
- else statuslog.statuslog(null, repo)
- })
- .catch((e) => {
- e.repo = repo;
- if (__isElevated) {
- fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
- }
- else statuslog.statuslog(e);
- })
- })
- .catch(e => {
- e.repo = repo;
- if (__isElevated) {
- fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
- }
- else statuslog.statuslog(e);
- })
- }
- }
-
- // PB : TODO -- If we are run from an elevated shell it never moves forward and simply exits.
- // -- Currently workaround is to always run from a non-elevated shell.
- var __isElevated = null; // We assume non-Elevated until someone evaluates and sets this variable.
- var acquireElevationState = () => {
- if (__isElevated === null) {
- return nodeShellExec("fsutil", ["dirty", "query", "C:"], {
- inherit: true
- // , shell: true
- , stdio: 'ignore'
- , env: process.env
- , title: `check privileged execution mode using "fsutil dirty query C:"`
- }).then((exitcode) => {
- console.log('Elevated')
- __isElevated = true;
- return true;
- }).catch(() => {
- __isElevated = false;
- console.log('Not Elevated');
- return false;
- });
- }
- else return Promise.resolve(__isElevated);
- }
-
-
- var repomanifest = null;
- var currentGitAuthUser; // nodeShellExec('git', ['config', 'user.email']) ... PB : TODO-- get the current gittea username
- var defaultRepoOwner = 'chess';
- var elevatedRunasRepos = null
- var gitRepos = null
- var defaultRepoServer = null
-
- // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
-
-
- // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
-
- var dbForLabel = function (label) {
- var dbsForLabel = {
- devmysql: 'mysql'
- , development: 'mssql'
- , production: 'mssql'
- }
- return dbsForLabel[label] || 'mysql'
- }
-
- // SAM : TODO Use nodeshellexec where to detect git installation dir
- var gitbash = "C:\\Program Files\\Git\\bin\\sh.exe"
- // var gitbash = "G:\\Installed\\Git\\bin\\sh.exe"
- // Relevant git repos
- // var repomanifest = require('../'+repomanifest.instanceName+'-config-'+ nodeenv +'/repo-manifest')()
- var exludeMergeRepos = [];
- var useGitPull = processedArgs.useGitPull || false;
- var configPromise = null
-
- // elxr cli operations
- var op = {
- 'h': () => { console.log(elxr.help()); return '-h' }
- , 'clean' : () => {
- // delete all node_module folders and links.
- var tasklist = [];
- dirs( (repodir)=> tasklist.push(getShellTask('rm',['-rf', 'node_modules'], { cwd : instanceroot + '/' + repodir.name })()), instanceroot )
- return Promise.all(tasklist)
- }
- , 'undefined': () => { return op.h(); }
- , 'reset': () => {
-
- // Reset NPM packages semver so major versions can be updated.
-
- const fs = require('fs')
- const wipeDependencies = (__package) => {
- const file = fs.readFileSync(__package + '/package.json')
- const content = JSON.parse(file)
- for (var devDep in content.devDependencies) {
- if (content.devDependencies[devDep].match(/\W+\d+.\d+.\d+-?((alpha|beta|rc)?.\d+)?/g)) {
- content.devDependencies[devDep] = '*';
- }
- }
- for (var dep in content.dependencies) {
- if (content.dependencies[dep].match(/\W+\d+.\d+.\d+-?((alpha|beta|rc)?.\d+)?/g)) {
- content.dependencies[dep] = '*';
- }
- }
- fs.writeFileSync(__package + '/package.json', JSON.stringify(content))
- }
-
- var repos = ['client'];
- // repos = gitRepos;
- repos.forEach(wipeDependencies)
- // if (require.main === module) {
- // } else {
- // module.exports = wipeDependencies
- // }
-
- }
- , 'upgrade': () => {
- console.log('upgrade.......')
- var tasks = [
- () => {
- var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
- inherit: true, shell: true
- , env: process.env
- }).catch((e) => { console.error(e) })
- p.position = 1;
- console.log('One')
- return p;
- }
- , () => {
- var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
- inherit: true, shell: true
- , env: process.env
- }).catch((e) => { console.error(e) })
-
- p.position = 2;
- console.log('Two')
- return p;
- }
- , () => {
- var p = nodeShellExec('npm', ['install', '-g', 'n'], {
- inherit: true, shell: true
- , env: process.env
- }).catch((e) => { console.error(e) })
- p.position = 3;
- console.log('Three')
- return p;
- }
- , () => {
- var p = nodeShellExec('n', ['latest'], {
- inherit: true, shell: true
- , env: process.env
- }).catch((e) => { console.error(e) })
- p.position = 4;
- console.log('Four')
- return p;
- }
- ]
- any(tasks)
- console.log('.......done')
- console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
- console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
- console.info('Uninstalling existing ember globally');
- var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: env
- })
- step1.on('close', () => {
- console.info('Installing ember globally');
- var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: env
- })
-
- step2.on('close', () => {
- nodeShellExec('cmd', ['/c', 'ember', '--version'], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: env
- })
- })
- })
- }
- , 'runas': () => {
- console.log('Testing Elevation')
-
- if (__isElevated) {
- try {
- op[processedArgs.label || processedArgs._[0] || 'h']()
- }
- catch (e) {
- console.error('Error Invalid command : ' + e)
- fs.writeFileSync('run.done', 'error')
- }
- finally {
- }
- }
- else {
- console.log('Requesting Elevated Privileges');
- // Wait for the runas to complete before we read it.
- try {
- fs.unlinkSync('run.done')
- fs.unlinkSync('run.log')
- }
- catch (e) { } //Ignore
-
- // Find node path to send to hta.
- return nodeShellExec('where', ['node']).then(r => {
- var namedArgs = [];
- console.log('result : ' + JSON.stringify(r))
- Object.keys(processedArgs).forEach((v) => { v != '_' ? namedArgs.push('--' + v + '=' + processedArgs[v]) : null; })
- // PB : TODO -- Convert all the cli args back to string.
- var args = [__dirname + '/windowselevate.hta'].concat(processedArgs._).concat(namedArgs.join(' ')); args.push('--runas=self');
- args.push('--nodepath=' + r[r.length - 1])
- if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV)
- if (processedArgs.debug) args.push('--debug=true') // Enable to debug elevated..
-
- return nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
- , {
- inherit: true
- , shell: true
- , env: ENV
- , runas: 'self'
- , title: `runas`
- }
- ).then(() => {
- // runas returned.
- try {
- // PB : TODO -- Log is comma prefixed. Needs to be proper JSON.
- var runaslog = JSON.parse('[ { "success" : true, "result" : "started"}' + fs.readFileSync('run.log', { flags: 'a+' }) + ']');
- runaslog.forEach((logEntry) => {
- statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
- logEntry.success ? (console.log(['success :' + logEntry.result]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
- })
- }
- catch (e) {
- // We must have a runas log
- statuslog.statuslog(e)
- console.error('Run log error probably was not created by runas : ' + e)
- }
- })
- .catch(err => console.error('Elevation failed : ' + err));
- })
-
- }
- }
- , 'push': () => {
- if (!processedArgs._[1]) { console.error('push all not supported. Specify repo name'); return }
- // init remote bare from local
- // pushandinitremotebare
- // https://www.jeffgeerling.com/blogs/jeff-geerling/push-your-git-repositories
- // connect to repo server -- net use 172.16.0.27
- // cd 172.16.0.27/repos/
- // mkdir repo.git
- // cd repo.git
- // git init --bare
- // cd localrepo
- // git remote rename origin githubclone
- // git remote add origin //172.16.0.27/repos/repo.git
- // git push origin master
-
- var repo = processedArgs._[1];
-
- var sequentialTaskShellCommands = [];
- if (!existsSync(`Z:/${repo}.git`)) {
- sequentialTaskShellCommands = [
- // ['net', ['use', 'Z:', defaultRepoServer.replace('/','\\')], {
- // inherit : true, shell: true
- // , env: process.env
- // }]
- ['pwd', { cwd: 'Z:', inherit: true }]
- , ['mkdir', [`${repo}.git`], {
- cwd: `Z:`
- , inherit: true, shell: true
- , env: process.env
- }]
- , ['pwd', { cwd: `Z:/${repo}.git`, inherit: true }]
- , ['git', ['init', '--bare'], {
- cwd: `Z:/${repo}.git`
- , inherit: true, shell: true
- , env: process.env
- }]
- // PB : TODO -- Do this conditionally only...
- , ['git', ['remote', 'rename', 'origin', 'githubclone'], { cwd: `${instanceroot + '/' + repo}` }, (err) => {
- console.log('Ignoring origin rename error : ' + err); return true; //return true to continue.
- }] // PB ; Todo -- new repositories created locally will not have origin. Handle this failure.
- , ['git', ['remote', 'add', 'origin', `${defaultRepoServer}/${repo}.git`], { cwd: `${instanceroot + '/' + repo}` }]
- // PB : TODO -- If threre is a gitbubclone origin
- // Set the master to pull from the local repo.
- ]
- if (!existsSync(`Z:`)) {
- sequentialTaskShellCommands.splice(0, 0, ['net', ['use', 'Z:', defaultRepoServer.replace(/\//gm, '\\')], {
- inherit: true, shell: true
- , env: process.env
- }])
- console.warn('Adding network drive z: for repo server. ' + sequentialTaskShellCommands[0])
- // throw 'done'
- }
- }
-
- sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd: `${instanceroot + '/' + repo}` }])
- // console.dir(sequentialTaskShellCommands);
-
- var tasks = [];
- sequentialTaskShellCommands.forEach(shellcmd => {
- // console.log(shellcmd)
- tasks.push(() => {
-
- var p = nodeShellExec.apply(null, shellcmd.slice(0, 3)).catch((e) => { if (shellcmd[3]) { return shellcmd[3]() } else { console.error(e); } })
- return p;
- })
- })
- any(tasks);
-
- }
- , 'is-git-repo': (dir) => {
- return nodeShellExec('git', ['-C', dir.name, 'rev-parse'], { stdio: 'ignore' })
- }
- , 'set-url': (remotename, url) => {
- var pushable = processedArgs.pushable || false;
- remotename = remotename || processedArgs._[1]
- url = url || processedArgs._[2]
- var serial_perform_git_seturl = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
- // console.log(repo)
- if (pushable) {
- return [
- ['git', ['remote', 'set-url', remotename, url + '/' + repo], { cwd: instanceroot + '/' + repo }]
- ]
- }
- else {
- console.error('not supported for non-pushable')
- }
-
- }
-
- var perform_git_seturl = (dir) => {
- op['is-git-repo'](dir).then((code) => {
- any(serial_perform_git_seturl(dir.name).map(x))
- }).catch((e) => {
- // console.log('Failed : ' + dir.name)
- })
- }
-
- dirs(perform_git_seturl)
- }
- , 'add': (remotename, url, branch) => {
- var pushable = processedArgs.pushable || false;
- remotename = remotename || processedArgs._[1]
- url = url || processedArgs._[2]
- branch = branch || processedArgs._[3]
- var serial_perform_git_add = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
- // console.log(repo)
- if (pushable) {
- return [
- ['git', ['remote', 'add', remotename, url + '/' + repo], { cwd: instanceroot + '/' + repo }]
- , ['git', ['pull', remotename, branch], { cwd: instanceroot + '/' + repo }]
- , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd: instanceroot + '/' + repo }]
- ]
- }
- else {
-
- return [
- ['git', ['remote', 'add', remotename, url + '/' + repo], { cwd: instanceroot + '/' + repo }]
- , ['git', ['remote', `set-url`, '--push', remotename, 'no-pushing'], { cwd: instanceroot + '/' + repo }]
- , ['git', ['pull', remotename, branch], { cwd: instanceroot + '/' + repo }]
- , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd: instanceroot + '/' + repo }]
- ]
- }
-
- }
- var x = (args) => {
- return () => {
- // console.log(args)
- return nodeShellExec.apply(null, args)
- }
- // return Promise.resolve(true)
- }
- var perform_git_add = (dir) => {
- op['is-git-repo'](dir).then((code) => {
- // console.log(code)
- if (code) {
- nodeShellExec('git', ['remote', 'get-url', remotename], { cwd: dir.name, stdio: 'ignore' }).then(() => {
- console.log('skipped : ' + dir.name + ', reason : A remote with same name already exists.')
- })
- .catch((e) => {
- any(serial_perform_git_add(dir.name).map(x))
- })
- }
- // else console.log('Skipped : Not a Git Repo : ' + dir.name)
- }).catch((e) => {
- // console.log('Failed : ' + dir.name)
- })
- }
-
- const { readdir } = require("fs").promises
-
- const dirs = async (perform, path) => {
- for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
- if (dir.isDirectory()) perform(dir)
- }
- }
-
- dirs(perform_git_add)
- }
- , 'remove': (remotename) => {
- remotename = remotename || processedArgs._[1]
- var serial_perform_git_remove = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
- // console.log(repo)
- return [
- ['git', ['remote', 'remove', remotename], { cwd: instanceroot + '/' + repo }]
- ]
- }
- var x = (args) => {
- return () => {
- // console.log(args)
- return nodeShellExec.apply(null, args)
- }
- // return Promise.resolve(true)
- }
- var perform_git_remove = (dir) => {
- op['is-git-repo'](dir).then((code) => {
- // console.log(code)
- if (code) {
- nodeShellExec('git', ['remote', 'get-url', remotename], { cwd: dir.name, stdio: 'ignore' }).then(() => {
- any(serial_perform_git_remove(dir.name).map(x))
- })
- .catch((e) => {
- console.log('skipped : ' + dir.name + ', reason : No remote named origin')
- })
- }
- // else console.log('Skipped : Not a Git Repo : ' + dir.name)
- }).catch((e) => {
- // console.log('Failed : ' + dir.name)
- })
- }
-
- const { readdir } = require("fs").promises
-
- const dirs = async (perform, path) => {
- for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
- if (dir.isDirectory()) perform(dir)
- }
- }
-
- dirs(perform_git_remove)
- }
- , 'init-gitea': (user) => {
- user = user || processedArgs._[1]
- if (!user) throw 'User name required'
- var serial_perform_init_gitea = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
- // console.log(repo)
- return [
- ['git', ['remote', 'add', 'chess', `${defaultRepoServer}/${user}/${repo}.git`], { cwd: instanceroot + '/' + repo }]
- , ['git', ['remote', 'set-url', '--push', 'chess', 'no-pushing'], { cwd: instanceroot + '/' + repo }]
- , ['git', ['remote', 'set-url', 'origin', `${defaultRepoServer}/${user}/${repo}.git`], { cwd: instanceroot + '/' + repo }]
- ]
- }
- var x = (args) => {
- return () => {
- // console.log(args)
- return nodeShellExec.apply(null, args)
- }
- // return Promise.resolve(true)
- }
- var perform_init_gitea = (dir) => {
- op['is-git-repo'](dir).then((code) => {
- // console.log(code)
- if (code) {
- nodeShellExec('git', ['remote', 'get-url', 'chess'], { cwd: dir.name, stdio: 'ignore' }).then(() => {
- console.log('skipped : ' + dir.name + ', reason : Already has remote chess ')
- })
- .catch((e) => {
- any(serial_perform_init_gitea(dir.name).map(x))
- })
- }
- // else console.log('Skipped : Not a Git Repo : ' + dir.name)
- }).catch((e) => {
- // console.log('Failed : ' + dir.name)
- })
- }
-
- const { readdir } = require("fs").promises
-
- const dirs = async (perform, path) => {
- for (const dir of await readdir(path || process.cwd(), { withFileTypes: true })) {
- if (dir.isDirectory()) perform(dir)
- }
- }
-
- dirs(perform_init_gitea)
- }
- , 'syncmaster': (label) => {
- // Usage :
- // elxr pull -- Defaults to run config
-
- var env = Object.assign({}, process.env); // Shallow clone it.
- // console.dir(env)
- console.log('Running exlr pull : ' + path.dirname(__dirname))
-
- if (!processedArgs.runas) gitRepos.map((repo) => performPull(repo, 'master'))
- if (__isElevated) {
- return any(elevatedRunasRepos.map((repo) => performPull(repo, 'master'))).then(() => {
- fs.writeFileSync('run.done', 'success')
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }
- else return op['runas']()
- }
-
- , 'pull': (label) => {
- // Usage :
- // elxr pull -- Defaults to run config
-
- var env = Object.assign({}, process.env); // Shallow clone it.
- console.log('Running exlr pull : ' + path.dirname(__dirname))
-
- var useGitPull = processedArgs.useGitPull || false;
-
- if (__isElevated) {
- return any(elevatedRunasRepos.map((repo) => performPull(repo))).then(() => {
- fs.writeFileSync('run.done', 'success')
- return true;
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }
- else {
- // PB : TODO -- Rename op['runas'] to 'elevate'
- return op['runas']().then(() => { return true; })
- .catch((e) => {
- console.error(e)
- })
- .finally(() => {
- if (!processedArgs.runas) {
- var pendingpulls = [];
- gitRepos.forEach((r) => { pendingpulls.push(performPull(r)) })
- return Promise.all(pendingpulls).then(results => {
- return true;
- }).finally(Traq.finally)
- }
- })
- }
- }
- , 'isInstalled': () => {
- return nodeShellExec('where', [processedArgs._[1]], { inherit: true }).then(() => {
- console.log(processedArgs._[1] + ' exists.')
- return true;
- });
- }
- , 'i': () => {
-
- var tasks = []
- tasks.push(op['pull']);
- tasks.push(getShellTask.apply(null, ['rm', [instanceroot + '/run.js'], { ignorefailures: true }]))
- tasks.push(op['use'])
- if (!__isElevated) {
- tasks.push(op['npmi'])
- }
-
- // var tasksdefs = [
- // ['elxr', ['pull']]
- // , ['elxr', ['use', 'elixir']]
- // , ['elxr', ['npmi']]
- // ]
-
-
- // tasksdefs.forEach(tasksdef=>{
- // tasks.push( ()=> nodeShellExec.apply(null, tasksdef) )
- // })
-
- return any(tasks);
-
- }
- , 'npmi': () => {
- var tasks = [];
-
- // Build fails without babel...
- tasks.push(getShellTask(
- 'npm',[ 'i', '-g', 'babel-cli' ]
- ) )
-
- var npmbuildrepos = ['loopback-jsonapi-model-serializer']
- npmbuildrepos = []
- npmbuildrepos.forEach(repo => {
- tasks.push(() => {
- return nodeShellExec('npm', ['i --force'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env: process.env
- , title: `npm i for ${repo}`
- }).catch((e) => {
- console.error('Ignoring Benign Error'); console.error(e);
- }).then(() => {
- console.log(`--------------------npm run build for ${repo}--------------------`)
- return nodeShellExec('npm', ['run build'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env: process.env
- , title: `npm run build for ${repo}`
- }).then(Tasq.then).catch(Tasq.catch)
- })
- })
- })
-
- any(tasks).then(() => {
-
- gitRepos.push('client/server');
- gitRepos = gitRepos.concat(elevatedRunasRepos);
-
- // gitRepos = [
- // // 'bbhverse', 'serververse', 'elixir-server',
- // // 'clientverse',
- // 'client'
- // ];
- var rmtasks = []
- var repotasks = []
- var env = Object.assign({}, process.env)
- delete env.NODE_ENV
- gitRepos.forEach(repo => {
-
- rmtasks.push(
- // () => {
- // console.log(`--------------------rm package-lock.json for ${repo}--------------------`)
- // return nodeShellExec(`"${gitbash}"`, ['-c', '"rm package-lock.json"'], {
- nodeShellExec('rm', ['package-lock.json'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env: process.env
- , title: `rm 'package-lock.json' for ${repo}`
- })
- .then(()=>{
- console.log(`--------------------rm package-lock.json for ${repo}--------------------`)
- }).catch((e) => { console.error(e) })
- // }
- )
-
- if (npmbuildrepos.indexOf(repo) < 0) {
- repotasks.push(
- () => {
- console.log(`--------------------npm i for ${repo}--------------------`)
- var p = nodeShellExec('npm', ['i', '--force'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env
- , title: `npm i for ${repo}`
- }).then(Tasq.then).catch(Tasq.catch)
- return p;
- })
- }
- })
-
-
- // PB : NOTE -- npm i for client does not complete when NODE_ENV=production
- // therefore bower doesn't get installed !!! which then fails installing the bower dependenciew !!!
- // We work around this by running npm i for client without any NODE_ENV which probably defualts to development.
- // PB : TODO -- Investigate why NODE_ENF has an impact on npm i !??
- // Second time try also doesnt work.
- // repotasks.push(
- // () => {
- // var env = Object.assign({}, process.env)
- // delete env.NODE_ENV
- // console.log(`--------------------second time npm i for client--------------------`)
- // return nodeShellExec(`"${gitbash}"`, ['-c', '"npm i --force"'], {
- // // return nodeShellExec('npm', ['i --force'], {
- // inherit: true, shell: true
- // , cwd: instanceroot + '/' + 'client'
- // , env
- // , title: `npm i for client`
- // }).then(Tasq.then).catch(Tasq.catch)
- // })
-
- var bowerRepos = ['client']
- bowerRepos.forEach(repo => {
- repotasks.push(() => {
- console.log(instanceroot + '/' + repo + '/node_modules/bower/bin/bower')
- // var p = nodeShellExec('node_modules/bower/bin/bower', ['install'], {
- var p = nodeShellExec(`"${gitbash}"`, ['-c', '"node_modules/bower/bin/bower i"'], {
-
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env: process.env
- , title: `bower i for ${repo}`
- }).then(Tasq.then).catch(Tasq.catch)
- return p;
- })
- })
- // console.log('rmtasks.length : ' + rmtasks.length)
- return Promise.all(rmtasks).then(() => any(repotasks));
- }).catch(e => {
-
- }).finally(statuslog.finally)
-
- }
- , 'start': (label) => {
- console.log('Starting Elixir Server.');
- var env = Object.assign({}, process.env); // Shallow clone it.
- // console.dir(env)
- env.NODE_ENV = process.env.NODE_ENV || 'development';
- env.DEBUG = 'loopback:connector:' + dbForLabel(label)
- var cmd = env.NODE_ENV === 'development' ? 'nodemon' : 'node';
- // cmd = 'node'
- cmd = [cmd, ['--inspect=9228', 'elixir/server.js']]
- var childPromise = nodeShellExec(...cmd, {
- // inherit : true,
- shell: true,
- detached: true,
- stdio: 'ignore',
- cwd: instanceroot + '/' + 'elixir-server'
- , env: env
- })
-
- var child = childPromise.process;
- if (typeof child.pid !== 'undefined') {
- console.log(`started Elixir Server PID(${child.pid}) : NODE_ENV=${process.NODE_ENV} ${cmd}`);
- fs.writeFileSync('.elixir-server.elixir.server.pid', child.pid, {
- encoding: 'utf8'
- })
- }
-
- // nodeShellExec('node', ['--inspect=9226', ' bin/www'], {
- // inherit : true,
- // shell: true, detached: true,
- // cwd : 'qms/server',
- // env: env,
- // shell : true
- // })
-
- // nodeShellExec('ember', ['s'], {
- // // inherit : true,
- // shell: true, detached: true,
- // cwd : 'client/',
- // env: env
- // })
- console.log('Starting Elixir Client Host.');
- var cmd = ['ember', ['s']]
- var childPromise = nodeShellExec(...cmd, {
- // var childPromise = nodeShellExec('node', ['--inspect=9227', './node_modules/.bin/ember', 's'], {
- // PB : TODO -- ember debugging.
- // inherit : true,
- shell: true,
- detached: true,
- stdio: 'ignore',
- cwd: instanceroot + '/' + 'client'
- , env: env
- })
- // .catch(e=>console.error(e))
-
- child = childPromise.process;
- if (typeof child.pid !== 'undefined') {
- console.log(`started Elixir Client Host PID(${child.pid}) : NODE_ENV=${process.NODE_ENV} ${cmd}`);
- fs.writeFileSync('.client.server.pid', child.pid, {
- encoding: 'utf8'
- })
- }
-
- }
- , 'stop': (label) => {
- const kill = require('tree-kill');
-
- var serverPid = fs.readFileSync('.elixir-server.elixir.server.pid', {
- encoding: 'utf8'
- })
- fs.unlinkSync('.elixir-server.elixir.server.pid')
- console.log(serverPid)
- kill(serverPid)
-
- serverPid = fs.readFileSync('.client.server.pid', {
- encoding: 'utf8'
- })
- fs.unlinkSync('.client.server.pid')
- console.log(serverPid)
- kill(serverPid)
- }
- , 'use': () => {
- // use a certain named instance.
- // Eg :
- // 1) elxr use elixir
- // 2) elxr use cihsr
- // If environment is not specified defaults to development.
- // 1) NODE=test elxr use elixir
-
- /*// Steps
- 1) Delete Config and Data symlinks
- 2) Make Links for config ({{name}}-config-{{node_env}}) and data with the NODE_ENV specified or default to dev
- 3) Iterates all repos and pull all. 'git', ['pull', '--all'].
- 4) Iterates all repos and checkout to the ENV specified. 'git', ['checkout', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
- 5) Iterates all repos and merge from source configured in mergeSource. 'git', ['merge', mergeSource],
- */
- var runconfig = { NODE_ENV: repomanifest.node_env }
- try { runconfig = Object.assign(runconfig, require(instanceroot + '/run.js')) } catch (e) { }
- if ((!processedArgs.runas || processedArgs.runas !== 'self') &&
- runconfig.NODE_ENV && runconfig.NODE_ENV === (repomanifest.node_env || runconfig.NODE_ENV) &&
- repomanifest.instanceName && runconfig.use === repomanifest.instanceName) {
-
- console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`)
- if (processedArgs.runas) { fs.writeFileSync('run.done', 'success') }
- return
- }
-
- var tasks = [
- () => {
- if (existsSync('config')) {
- var p = nodeShellExec('rmdir', ['config'], { inherit: true, shell: true, env: process.env }
- ).catch((err) => { console.log('Ignoring benign error : ' + err); return true; })
- return p;
- }
- else return Promise.resolve(true);
- },
- () => {
- if (existsSync('data')) {
- var p = nodeShellExec('rmdir', ['data'], { inherit: true, shell: true, env: process.env }
- ).catch((err) => { console.log('Ignoring benign error : ' + err); return true; })
- return p;
- }
- else return Promise.resolve(true);
- },
- ];
- runconfig.NODE_ENV = process.env.NODE_ENV = process.env.NODE_ENV || runconfig.NODE_ENV || 'development';
- if (processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1];
- if (!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' }
- // console.log(process.env.cwd)
- fs.writeFileSync(instanceroot + '/run.js', 'module.exports = ' + JSON.stringify(runconfig))
-
- // Maps an environment to a branch. Not required if the branch is appropriately named.
- var checkoutMap = { 'development': 'master' }
-
- // cant use git checkout -b it fails with branch already exists.
- var performCheckout = (repo, branch) => {
- if (!branch) return Promise.resolve({ 'skipped': true })
- if (excludeCheckouts[repo]) return Promise.resolve({ 'skipped': true })
- return nodeShellExec('git', ['checkout', branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV], {
- // return nodeShellExec('git', ['switch', '-m', '-C', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV], {
- // inherit : true, shell: true,
- cwd: instanceroot + '/' + repo
- // , stdio : ignore // Use when we want to silcence output completely.
- , runas: processedArgs.runas
- , title: `git checkout ${branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} for ${repo}`
- }).then(()=>{ console.log( `SUCCESS : git checkout ${branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} for ${repo}` ) }).catch((e) => { console.error(e); return { error: true, message: repo } })
- }
- if (runconfig.NODE_ENV === 'development') performCheckout = () => { return Promise.resolve(true) }
-
- var performPullAll = (repo) => {
- if (excludeCheckouts[repo]) return Promise.resolve({ 'skipped': true })
- return nodeShellExec('git', ['pull', '--all'], {
- // inherit : true, shell: true,
- cwd: instanceroot + '/' + repo
- , runas: processedArgs.runas
- , title: `git pull -all for ${checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} ${repo}`
- }).catch((e) => { console.error(e); return { error: true, message: repo } })
- }
-
- var mergeSources = {
- 'development': null,
- 'test': 'master',
- 'production': 'master'
- }
- var excludeCheckouts = Object.assign(exludeMergeRepos)
- delete excludeCheckouts[`elixir-config-${runconfig.NODE_ENV}`]
- delete excludeCheckouts[`cihsr-config-${runconfig.NODE_ENV}`]
-
- var mergeSource = mergeSources[checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
- var performMerge = (repo) => {
- if (exludeMergeRepos[repo]) return Promise.resolve({ 'skipped': true })
- return nodeShellExec('git', ['merge', mergeSource], {
- inherit: true, shell: true,
- cwd: instanceroot + '/' + repo
- , runas: processedArgs.runas
- }).catch((e) => { console.error(e) })
- }
- if (runconfig.NODE_ENV === 'development') performMerge = () => { return Promise.resolve(true) }
-
- return any(tasks).then(() => {
- if (!processedArgs.runas) return op['runas']()
-
- tasks = [
- () => {
- // Use junctions to avoid npm package issues
- var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-config' + '-' + process.env.NODE_ENV], {
- inherit: true, shell: true
- , cwd : instanceroot
- , env: process.env
- }).catch((e) => { console.error(e) })
- return p;
- }
- ];
- if (processedArgs._[1]) {
- tasks = tasks.concat(
- [
- () => {
- var p = nodeShellExec('mklink', ['/J', 'data', runconfig.use + '-data'], {
- inherit: true, shell: true
- , cwd : instanceroot
- , env: process.env
- }).catch((e) => { console.error(e) })
- return p;
- }
- ]
- )
- }
-
- return any(tasks)
- //target is the env is we specify in elxr use command. Default is dev
- .then( //Switch to target branch
- () => any([any(gitRepos.map((repo) => performCheckout(repo, process.env.NODE_ENV || 'development')))
- , any(elevatedRunasRepos.map((repo) => performCheckout(repo, process.env.NODE_ENV || 'development')))]))
- .then( //PULL from target branch
- () => any([any(gitRepos.map((repo) => performPullAll(repo))), any(elevatedRunasRepos.map((repo) => performPullAll(repo)))]))
- .then( //Switch to merge source branch
- () => any([any(gitRepos.map((repo) => performCheckout(repo, mergeSources[process.env.NODE_ENV || 'development'])))
- , any(elevatedRunasRepos.map((repo) => performCheckout(repo, mergeSources[process.env.NODE_ENV || 'development'])))]))
- .then( //Pull on merge source branch
- () => any([any(gitRepos.map((repo) => performPullAll(repo))), any(elevatedRunasRepos.map((repo) => performPullAll(repo)))]))
- .then( //Switch to target branch
- () => any([any(gitRepos.map((repo) => performCheckout(repo, process.env.NODE_ENV || 'development')))
- , any(elevatedRunasRepos.map((repo) => performCheckout(repo, process.env.NODE_ENV || 'development')))]))
- .then( //Merge source branch to target branch
- () => any([any(gitRepos.map((repo) => performMerge(repo))).catch(err => { console.error('error in performMerge ' + err) }),
- any(elevatedRunasRepos.map((repo) => performMerge(repo))).catch(err => { console.error('error in performMerge ' + err) })]))
- .then(() => {
- // Move test config from dev.
- // if(process.env.NODE_ENV === 'test'){
- // var devcfgreponame = runconfig.use + '-config' + '-development';
- // var testcfgreponame = runconfig.use + '-config' + '-test';
-
- // var testcfgdir = path.dirname(__dirname) + '/' + testcfgreponame + '/'
- // var devcfgdir = path.dirname(__dirname) + '/' + devcfgreponame + '/' //eg (elxr/../elixir-config.development)
-
- // return any([
- // ()=>{
- // return nodeShellExec('git', ['checkout', 'test'], {
- // inherit : true, shell: true,
- // cwd : testcfgdir
- // // , env: process.env
- // , runas : processedArgs.runas
- // , title : `git checkout test for ${testcfgreponame}`
- // }).catch((e)=>{ console.error(e) })
- // }
- // , ()=> {
- // return nodeShellExec('git', ['checkout', 'master'], {
- // inherit : true, shell: true,
- // cwd : devcfgdir
- // // , env: process.env
- // , runas : processedArgs.runas
- // , title : `git checkout master for ${devcfgreponame}`
- // }).catch((e)=>{ console.error(e) })
- // }
- // , ()=> {
- // globSync( '**/*.test.js', {cwd : devcfgdir}).map((filename) => {
- // console.log('File found : ' + devcfgdir + filename)
- // fs.copyFileSync(devcfgdir + filename, testcfgdir+ filename);
- // })
- // return nodeShellExec('git', ['checkout', 'test'], {
- // inherit : true, shell: true,
- // cwd : devcfgdir
- // // , env: process.env
- // , runas : processedArgs.runas
- // , title : `git checkout test for ${devcfgreponame}`
- // }).catch((e)=>{ console.error(e) })
- // }
- // ])
- // }
- // else {
- return Promise.resolve(true)
- // }
- })
- .then(() => {
- fs.writeFileSync('run.done', 'success')
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
-
- // Antibiotic stewardship program.
- // 1st use is fine.
- // Max vials dispense
- // 2nd use Pharmacy needs justification Form.
- // Approval after a certain period of time.
- }
- , 'g': () => {
- if (processedArgs.h) {
-
- console.log('elxr g [modelname] => generate a model named [modelname]');
- console.log('elxr g => regenerate all known models');
- return
- }
- // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
- // console.log('Starting directory: ' + process.cwd());
- // try {
- // child = child.on('close', () => { process.chdir(label) } );
- // console.log('New directory: ' + process.cwd());
- // }
- // catch (err) {
- // console.log('chdir: ' + err);
- // }
- // child.on('close', function(){
- // var options = {
- // shell : true
- // , inherit : true
- // // , cwd : '' + process.cwd
- // // , env : process.env
- // };
- // nodeShellExec('git', ['init'], { inherit : true});
- if (0) {
- // PB : TODO -- Special google chrome profile for tests etc.
- nodeShellExec('pwd', { inherit: true });
- // /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"
- // "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
- // "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
- nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
- "--profile-directory=Profile 1"
- , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
-
- }
- // nodeShellExec('npm', ['init', '-y'], options);
- // nodeShellExec('npm', ['init', '-y'], options);
- // })
-
- var g = {
- 'client': () => {
- console.info('Creating new ember client named : ' + processedArgs._[2]);
- var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: env
- })
- },
-
- 'modelr': () => {
- var tasks = [
- () => {
- var p = nodeShellExec('"ember"', [
- 'g'
- , 'modelr'
- , processedArgs._[2]], {
- inherit: true, shell: true, env: process.env
- }).then(() => {
- console.log('Blueprint generation complete for : ' + processedArgs._[2])
- return true;
- }).catch((e) => { console.error(e) })
- return p;
- },
- () => {
- var chromePrefsFile = "C:\\chess\\instances\\elixir_01\\data\\Google\\Chrome\\User Data\\chess\\Preferences";
- var chromeprefs = fs.readFileSync(chromePrefsFile, { encoding: 'utf8' })
- chromeprefs = JSON.parse(chromeprefs)
- var previous = chromeprefs.download.default_directory;
- var parentDir = path.dirname(__dirname);
- chromeprefs.download.default_directory = parentDir + "\\client\\app\\templates\\components\\resource";
- fs.writeFileSync(chromePrefsFile, JSON.stringify(chromeprefs))
- // PB : TODO -- detect where chrome is installed.
- // PB : TODO -- set the download dir to the place where files are needed.
- var p = nodeShellExec('"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/' + processedArgs._[2]], {
- inherit: true, shell: true
- , env: process.env
- }).then(() => {
- chromeprefs.download.default_directory = previous;
- fs.writeFileSync(chromePrefsFile, JSON.stringify(chromeprefs))
- return true;
- }).catch((e) => { console.error(e) })
- return p;
- }
- ,
- () => {
- console.log('Browser process should have closed here....')
- return true;
- }
- ]
- any(tasks)
-
- }
-
- }
-
- g[processedArgs._[1]]();
- }
- }
-
- var util = require('util')
- var cliname = 'elxr';
- var ver = '1.1';
- var help = `# list of commands... please refer dveloper documentation for ${cliname}
- ${
- // util.inspect(
- [Object.keys(op)]
- // )
- }
- `;
-
- var elxr = {
- help() {
- return chalk.cyanBright(`
- -------------------------------------------------------------------------------
- *** BBH Elixir ***
- -------------------------------------------------------------------------------
- elxr ${ver}
- A cli tool for chess
- ${help}
- -------------------------------------------------------------------------------
- `)
- }
- , info() {
- return chalk.cyanBright(`
- -------------------------------------------------------------------------------
- *** BBH Elixir ***
- -------------------------------------------------------------------------------
- elxr ${ver}
- A cli tool for chess
- -------------------------------------------------------------------------------
- `)
- }
- }
-
- // The main elxr cli process
- function elxrworker(hasconfig) {
- // Everything runs after this check is completed. Elevation occurs out of process when needed.
- gitRepos = repomanifest.repos
-
- // Repositiories that have symlinks that require elevated priviletes in windows to create symlinks
- elevatedRunasRepos = repomanifest.elevated
- // Repos that should excluded from merge for releases...
- exludeMergeRepos = repomanifest.exludeMergeRepos
-
- var env = Object.assign({}, process.env); // Shallow clone it.
- var __runcmd = function (label) {
- return op[label] ? op[label]() : null;
- }
-
- // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
- // mysql -u root -p db_name < drop_all_tables.sql
-
- var mysql = '../xampp/mysql/bin/mysql'
- var mysqldump = '../xampp/mysql/bin/mysqldump'
-
- // --runas
- if (processedArgs.runas) {
- // Weve been asked to run in priviledged mode. Check if we already are privileged.
- return __runcmd('runas')
- }
- else return __runcmd(processedArgs.label || processedArgs._[0] || 'h');
- }
-
- var getManifest = function () {
- // Once choices are made we need to load config according to those choices.
-
- // No trace of a previous run...
- // Default Config...
- return repomanifest = selectedinstance;
- }
-
- function acquireChoices(selectedinstance) {
-
- var hasconfig = false;
- console.warn(chalk.yellow(`
- -------------------------------------------------------------------------------
- Warning : Cannot locate your preferred configuration since it was not specified
- You should fork the default chess configuration to customize and make it
- your own instance with a named config as
- {{yourowninstancename}}-config-{{yourcurrentenvironment}}
- And then run this tool as follows
- NODE_ENV={{yourenvironment}} elxr i {{yourowninstancename}}
- OR
- Run this tool with the following command to use a quick start default.
- node elxr --default
- OR
- Choose the the option to create a new instance for you interactively.
-
- We will run your choice of default or create your own at the next prompt.
- -------------------------------------------------------------------------------
- `))
- var prompt = cli.prompt;
- return prompt.ask(`Choose an option :
- d) Install the default chess instance.
- => elxr i chess node_env=development --default
- n) Create your custom new instance interactively
- => elxr i {{instanceName}} node_env={{environment}}
- i) Choose an instance and environment to install
- => elxr i {{instanceName}} node_env={{environment}}
- c) Choose a command to run ( pull, use, i, npmi ... ) <= pull
- => elxr {{cmd}} {{instanceName}} node_env={{environment}}
- h) Help
- q) Quit
- Default <= d
- : `).then((choice) => {
- prompt.close();
- if (choice && choice === 'd' || !choice) {
- processedArgs._[0] = 'i'
- selectedinstance.instanceName = processedArgs._[1] = processedArgs._[1] || 'chess'
- selectedinstance.node_env = processedArgs.node_env = (process.env.NODE_ENV && process.env.NODE_ENV.trim()) || processedArgs.node_env || 'development'
- defaultRepoServer = 'https://git.bbh.org.in'
- getManifest() // PB : TODO -- acquire the manifest directly from http url instead of clone before cloning the config. Since
- // This is because the manifest at any server location can redirect to the preferred server..
- }
- else if (choice === 'h') {
- processedArgs._[0] = 'h'
- fs.writeFileSync('run.done', 'noop help');
- console.log(elxr.help()); process.exit()
- }
- else if (choice === 'n' || choice === 'i') {
- var p1 = cli.prompt;
- return p1.ask(`Enter Instance Name ( <= ${selectedinstance.instanceName} ) : `).then(function (instanceName) {
- processedArgs._[0] = 'i'
- selectedinstance.instanceName = processedArgs._[1] = instanceName || selectedinstance.instanceName;
- return p1.ask(`Enter Environment ( <= ${selectedinstance.node_env} ) : `).then(function (node_env) {
- selectedinstance.node_env = processedArgs.node_env = node_env || selectedinstance.node_env
- if (choice === 'n') {
- defaultRepoServer = 'https://git.bbh.org.in'
- console.warn(
- chalk.magenta('No Option Available. Your account may not have privileges. You can request here http://git.bbh.org.in/chess'))
- process.exit();
- }
-
- return p1.ask(`Enter preferred repo server ( <= ${selectedinstance.reposervers[0]} ) : `).then(function (reposerver) {
- p1.close()
- defaultRepoServer = reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
- })
- })
- })
- } else if (choice === 'c') {
- var p1 = cli.prompt;
- return p1.ask(`Enter Instance Name ( <= ${selectedinstance.instanceName} ) : `).then(function (instanceName) {
- selectedinstance.instanceName = processedArgs._[1] = instanceName || selectedinstance.instanceName
- return p1.ask(`Enter Environment ( <= ${selectedinstance.node_env} ) : `).then(function (node_env) {
- selectedinstance.node_env = processedArgs.node_env = node_env || selectedinstance.node_env
- return p1.ask(`Enter cmd :
- p) pull
- Default <= p
- : `).then(function (cmd) {
- if (!cmd || cmd === 'p') {
- processedArgs._[0] = 'pull'
- }
- else processedArgs._[0] = cmd
- return p1.ask(`Enter preferred repo server ( <= ${selectedinstance.reposervers[0]} ) : `).then(function (reposerver) {
- defaultRepoServer = reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
- p1.close()
- })
- })
- })
- })
- } else {
- console.log(chalk.gray(`Default option not exercised. Please follow manual instructions to customize your instance here http://git.bbh.org.in/chess and try again.`));
- fs.writeFileSync('run.log', ', ' + JSON.stringify({ success: 'quit without execution' }), { 'flag': 'a+' })
- fs.writeFileSync('run.done', 'noop quit');
- process.exit()
- }
- })
- }
-
- var acquireConfig = function (selected, chessinstances) {
-
- var configrepo = selected.instanceName + '-config-' + selected.node_env;
- return performPull(configrepo).then(() => {
- var manifestpath = path.normalize(selected.root + '/' + selected.instanceName + '-config-' + selected.node_env + '/repo-manifest');
- repomanifest = require(manifestpath)()
- chessinstances = chessinstances || {};
- chessinstances[selected.instanceName] = chessinstances[selected.instanceName] || {}
- chessinstances[selected.instanceName][selected.node_env] = chessinstances[selected.instanceName][selected.node_env] || {}
- chessinstances['current_run'] = { instanceName: selected.instanceName, node_env: selected.node_env }
- // Config from server always override merges into selection ecept for the current selection.
- selectedinstance = utils.assign(chessinstances[selected.instanceName][selected.node_env], selected, repomanifest)
- fs.writeFileSync(instanceroot + '/chessinstances.js', 'module.exports = ' + JSON.stringify(chessinstances) + '', { 'flag': 'w' })
- // PB : TODO -- We should probably write the new server config also...
- defaultRepoServer = selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online...
- ENV.NODE_ENV = selectedinstance.node_env;
- })
- .catch((e) => {
- console.error(e)
- console.error('Config acquisition failed.')
- })
- }
-
- var launchpath = process.cwd().replace(/\\/, '/')
- var thisscriptdir = __dirname
- var instanceroot = launchpath;
- var detectInstance = function () {
- console.log(`launchpath = ${launchpath}`)
- console.log(`thisscriptdir = ${thisscriptdir}`)
-
- var root = launchpath;
-
- // We need a reference to the root director for elxr cli to be properly oriented.
- if ((launchpath + path.normalize('/elxr')) === thisscriptdir) {
- // We ran unbuilt from the proper root with elxr subfolder.
- console.log(`Instance Path : ${root}`)
- }
- else {
- if (launchpath === thisscriptdir) {
- // Same directory doesn't mean we are being run from elxr directory or the root directory.
- // It could be a standalone elxr build which may or maynot be in the proper location.
- if (BUILD_VERSION.indexOf('Version: {version} - built on {date}') > -1) {
- // Unbuilt therefore we are in the elxr directory.
- root = path.normalize(launchpath + '/..');
- }
- else {
- // Built version.
- // check if we have a elxr subfolder.
- if (fs.existsSync(launchpath + '/..' + path.normalize('/elxr'))) {
- // Probably in the right place.
- root = path.normalize(launchpath + '/..');
- }
- else {
- // Assume launchpath is meaningless.
- // Figure it out from the input instance name and environment parameters if we are in the right location.
- root = path.normalize(launchpath + '/' + processedArgs._[1] + '/' + processedArgs.node_env)
- }
- }
- }
- }
- instanceroot = root.replace(/\\/, '/');
-
- return Object.assign({ root }, __default);
- }
-
- var __default = {
-
- // Default is public server only.
- // All public repos are by default available.
- reposervers: ['https://git.bbh.org.in']
- , repos: [
- 'setup'
- , 'elxr'
- , 'loopback-connector-mysql'
- , 'loopback-jsonapi-model-serializer'
- , 'loopback-component-jsonapi'
- , 'ember-service-worker'
- , 'ember-service-worker-asset-cache'
- , 'ember-service-worker-cache-fallback'
- , 'ember-service-worker-index'
- , 'ember-sw-client-route'
- ]
-
- , elevated: []
- , exludeMergeRepos: {}
- , instanceName: 'chess'
- // Runas windowshta clobbers and removes the NODE_ENV !!! We therefore pass it in.
- , node_env: 'development'
- }
-
- var downloadsdir = '../Downloads';
- var prerequisites = [
- {
- shellcmd: 'git',
- url: 'https://github.com/git-for-windows/git/releases/download/v2.31.0.windows.1/Git-2.31.0-64-bit.exe'
- , installer: 'Git-2.31.0-64-bit.exe'
- , installcmd: ['cmd', ['/c', 'start',
- '/WAIT', path.resolve(downloadsdir + '/' + 'Git-2.31.0-64-bit.exe')
- , '/VERYSILENT'
- // , '/MERGETASKS=!runcode' // This is required only for vscode...
- ]]
- , preinstallsteps: function() {
- var prompt = cli.prompt;
- var steps = [
- () => {
- if (!existsSync(downloadsdir + '/' + this.installer)) {
- return nodeShellExec('download.bat', [this.url, downloadsdir + '/' + this.installer])
- }
- else return Promise.resolve(true)
- }
- ]
- var prompts = [
- () => prompt.ask(`git user name : ( <= ${gitUser} )`).then((user) => { gitUser = user; })
- , () => prompt.ask(`git email : ( <= ${gitEmail} )`).then((email) => { gitEmail = email; })
- ]
- return any([any(steps), any(prompts)]).then(() => { prompt.close() })
- }
- , installsteps: function () {
- return any([this.installcmd].map(callsheltask))
- }
- , postinstallsteps: function(){
-
- var prompt = cli.prompt;
-
- var gitUser = 'guest';
- var gitEmail = 'guest@bbh.org.in';
- var prompts = [];
- prompts.push(
- ()=>{
- var choices = { 0 : 'guest', 1 : 'chessdemo' }
- return prompt.ask(
- `git user name \n ` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${choices[0]} ) : `
- ).then(choice => {
- if(!choice) return gitUser = choices[0];
- if(choice && isNaN(+choice)) return gitUser = choice;
- return gitUser = choices[choice + 1];
- })
- }
- )
-
- prompts.push(
- ()=>{
- var choices = { 0 : 'guest@bbh.org.in', 1 : 'chessdemo@bbh.org.in' }
- return prompt.ask(
- `git user email \n ` + Object.keys(choices).map(choice => { return ` ${(+choice) + 1}) ${choices[choice]} `}).join('\n') + `\n default ( <= ${choices[0]} ) : `
- ).then(choice => {
- if(!choice) return gitEmail = choices[0];
- if(choice && isNaN(+choice)) return gitEmail = choice;
- return gitEmail = choices[choice + 1];
- })
- }
- )
-
- return any(prompts).then(()=>{
- var steps = [
- ['git', ['config', '--global', '--add', 'user.name', `${gitUser}`]]
- , ['git', ['config', '--global', '--add', 'user.email', `${gitEmail}`]]
- ]
- return any(steps.map(callsheltask)).then(() => {
-
- })
- });
- }
- , install: function () {
- return any([ /*this.preinstallsteps,*/ this.installsteps.bind(this), this.postinstallsteps.bind(this)])
- }
- , verifyAndInstall : function(){
- return getTaskCheckExists(this.shellcmd, { ignorefailures: true })().then((exists) => {
- if(exists) {
- // return any(['git', ['config', '--global', '-l']].map(callsheltask))
- return this.getUser(null, this.postinstallsteps.bind(this))
- }
- return this.install();
- });
- }
- , getUser : function(repo, onNoResult){
-
-
- onNoResult = onNoResult || function(){return false}
- var globalOrLocal = '--global';
- if(!repo) globalOrLocal = '--global';
- else globalOrLocal = '--local'
-
- return any([['git', ['config', globalOrLocal, '--get-all', 'user.name']]].map(callsheltask)).then((result)=>{
- // not yet configured.
- if(!result.success) return onNoResult()
- else {
- var users = result.messages[0].trim().split('\n');
- if(users.length === 0 ||
- users.length === 1 && users[0] === 'guest') {
-
- return onNoResult()
- }
- else return users[0]; // PB : TODO == We should probably prompt with all the users available for selection !
- }
- })
- .catch((e)=>{
- console.log(e)
- return onNoResult()
- })
- }
- }
- ,
- {
- shellcmd: 'node',
- url: 'https://nodejs.org/dist/v14.16.0/node-v14.16.0-x64.msi'
- , installer: 'node-v14.16.0-x64.msi'
- , installcmd: ['MSIEXEC.exe', ['/i'
- , path.resolve(downloadsdir + '/' + 'node-v14.16.0-x64.msi')
- , 'ACCEPT=YES', '/passive']]
- , install : function() { return any([this.installcmd].map(callsheltask)).then(() => { }) }
- }
- ]
-
- prerequisites.forEach(p=>{ prerequisites[p.shellcmd] = p })
-
- function ensureDirectoryExistence(filePath) {
- var dirname = path.dirname(filePath);
- if (fs.existsSync(dirname)) {
- return filePath;
- }
- ensureDirectoryExistence(dirname);
- fs.mkdirSync(dirname);
- return filePath;
- }
-
- var mainTasks = [];
- function verifyAndInstallPrerequisites() {
- fs.writeFileSync(ensureDirectoryExistence(path.normalize(downloadsdir + '/readme.txt')), `${getVersion()} Your local downloads for this instance`)
- var downloadbatch =
- `::**************************************************************************
- :Download_ <url> <File>
- Powershell.exe ^
- $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'; ^
- [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols; ^
- (New-Object System.Net.WebClient).DownloadFile('%1','%2')
- exit /b
- ::**************************************************************************`
- fs.writeFileSync('download.bat', downloadbatch)
-
- var downloadtasks = [];
- var installtasks = [];
- prerequisites.forEach(preq => {
- downloadtasks.push(getTaskCheckExists(preq.shellcmd, { ignorefailures: true })().then((exists) => {
- if (exists) console.log(`${preq.shellcmd} exists`)
- else {
- console.log(`${preq.shellcmd} is not installed`)
- return preq.preinstallsteps.call(preq).then(() => {
- installtasks.push(preq.install.bind(preq))
- })
- }
- }))
- })
- return Promise.all(downloadtasks).then(() => { return any(installtasks) })
- }
-
- function initinstances(chessinstances, selected) {
- chessinstances.current_run.instanceName = processedArgs._[1] = processedArgs._[1] || chessinstances.current_run.instanceName || selected.instanceName;
- chessinstances.current_run.node_env = processedArgs.node_env = processedArgs.node_env || chessinstances.current_run.node_env || selected.node_env;
- chessinstances[chessinstances.current_run.instanceName] = chessinstances[chessinstances.current_run.instanceName] || {}
- selectedinstance = Object.assign(selected, chessinstances[chessinstances.current_run.instanceName][chessinstances.current_run.node_env], clioverrides);
- repomanifest = selectedinstance
- return chessinstances
- }
- function acquirelocalinstances(selected){
- var chessinstances = utils.assign(require(path.normalize(selected.root + '/chessinstances.js')));
- return initinstances(chessinstances, selected)
- }
-
- function updateselection(selected) { selectedinstance = utils.assign(selectedinstance, selected) }
- var detectedInstance = detectInstance(); // From launch location etc.
- var selectedinstance = detectedInstance
- var clioverrides = { }
- processedArgs._[1] ? clioverrides.instanceName = processedArgs._[1]: null;
- processedArgs.node_env ? clioverrides.node_env = (process.env.NODE_ENV && process.env.NODE_ENV.trim()) || processedArgs.node_env
- : (process.env.NODE_ENV && process.env.NODE_ENV.trim()) ? clioverrides.node_env = (process.env.NODE_ENV && process.env.NODE_ENV.trim()): null;
-
- selectedinstance = detectedInstance = Object.assign(selectedinstance, clioverrides);
- var maintask = () => {
- try {
- // Default cmd to run !
- var chessinstances = acquirelocalinstances(selectedinstance);
- processedArgs._[0] === processedArgs._[0] || 'pull';
- // defaultRepoServer = repomanifest.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available...
- return prerequisites.git.verifyAndInstall().then(()=>{
- return acquireConfig(selectedinstance, chessinstances).catch((e) => {
- console.error('Exisitng config Failed. Fix config and rerun or chose another.')
- console.error(e);
- }).then(() => { return elxrworker(true) })
- });
- }
- catch (e) {
- console.error(e)
- // No local instances config found. We acquire user choices and proceed to reattempt.
- chessinstances = {}
- chessinstances.current_run = {}
- initinstances(chessinstances, selectedinstance)
- var todo = Promise.resolve(true);
- if(processedArgs._[0] && selectedinstance.node_env && selectedinstance.instanceName){
- // Weve been told what to do.
- todo = Promise.resolve(true)
- }
- else todo = acquireChoices(selectedinstance)
-
- return todo.then(() => {
- try {
- var chessinstances = acquirelocalinstances(selectedinstance)
- }
- catch (e) {
- console.error(e)
- console.error('No local instances config found in current root = ' + selectedinstance.root);
- console.log('A config will be createed with the instance and environment chosen...')
- }
- finally {
- return prerequisites.git.verifyAndInstall().then(()=>{
- return acquireConfig(selectedinstance, chessinstances).catch((e) => {
- console.error('Chosen cofiguraton failed or not found')
- console.error(e)
- }).then(() => { return elxrworker(true) })
- .finally(()=>{
- fs.writeFileSync('run.log', ', ' + JSON.stringify({ error: e.message }), { 'flag': 'a+' })
- fs.writeFileSync('run.done', 'error');
- // return process.exit()
- })
- })
- }
- })
- }
- }
-
- acquireElevationState().then(() => {
- var skipprerequisites = false
- if (!skipprerequisites && !__isElevated) mainTasks.push(verifyAndInstallPrerequisites);
- mainTasks.push(maintask)
- return any(mainTasks);
- })
-
- // detect if alread installed -> Take no action.
- // download if no installer avalable -> next()
- // install
- function chackandinstall(items) {
- var tasks = []
- items.forEach(item => {
- tasks.push(getTaskCheckExists(item.shellcommand || prerequisites[item]))
- })
-
- return Promise.all(tasks).then(existances => {
- existances.forEach((exists, i) => {
- if (!exists) {
- return downloadandinstall([items[i]])
- }
- })
- })
- }
-
- function downloadandinstall(items) {
- var tasks = []
- items.forEach(item => {
- tasks.push(getTaskDownload(item))
- })
- return Promise.all(tasks)
- }
-
- // Sample instances config.
- // var instances = {
- // "elixir": {
- // "production": {
- // "reposervers": ["http://git.bbh", "https://git.bbh.org.in"]
- // , "repos": ["ember-masonry-grid", "client", "elixir-client"]
- // , "exludeMergeRepos": {
- // "elixir-config-development": true, "elixir-config-test": true
- // , "elixir-config-production": true, "elixir-data": true
- // }
- // , "instanceName": "elixir", "node_env": "production"
- // }
- // },
- // "current_run": { "instanceName": "elixir", "node_env": "production" }
- // }
|