123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272 |
- var statuslog = require('./result')
- var path = require('path');
- var utils = require('bbhverse');
- var any = utils.any;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- const { existsSync } = require('fs');
- const fs = require('fs')
- const { spawn, spawnSync } = require('child_process');
- const cliargs = utils.cliargs;
- const processedArgs = cliargs(process.argv.slice(2));
- console.dir(processedArgs)
- var globSync = require('glob').sync;
-
- var path = require('path');
- const { isMaster } = require('cluster');
-
-
- var reposervers = [
- 'http://git.bbh'
- , 'https://git.bbh.org.in'
- , '//172.16.0.27/repos'
- ]
- var defaultRepoServer = reposervers[0]
-
- var currentGitAuthUser ;
- var defaultRepoOwner = 'chess';
-
-
-
- var __isElevated = null;
- var isRunningElevated = ()=>{
- if(__isElevated === null) {
- return nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
- inherit : 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');
- throw false;
- });
- }
- else return Promise.resolve(__isElevated);
- }
-
-
- var cli = 'elxr';
- var ver = '#unversioned';
- var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
-
-
-
-
-
-
- var dbForLabel = function(label){
- var dbsForLabel = {
- devmysql : 'mysql'
- , development : 'mssql'
- , production : 'mssql'
- }
- return dbsForLabel[label] || 'mysql'
- }
-
- var gitInstallDir = "C:\\Program Files\\Git\\bin\\sh.exe"
-
-
- var gitRepos = [
- 'ember-masonry-grid'
- , 'bbhverse'
- , 'clientverse'
- , 'serververse'
- , 'client'
-
- , 'elxr'
- , 'ember-searchable-select'
- , 'loopback-component-jsonapi'
- , 'loopback-jsonapi-model-serializer'
- , 'loopback-connector-mysql'
- , 'elixir-config-development'
- , 'elixir-config-test'
- , 'cihsr-config-development'
- , 'cihsr-data'
- , 'elixir-data'
- , 'loopback-connector-ds'
- , 'chess-server-lib'
- , 'setup'
- , 'elixir-client-todos'
- , 'elixir-client-unlinked'
- , 'elixir-client'
- , 'ember-service-worker'
- , 'ember-service-worker-asset-cache'
- , 'ember-service-worker-cache-fallback'
- , 'ember-service-worker-index'
- , 'ember-sw-client-route'
- , 'global-this'
- ]
-
-
-
- var elevatedRunasRepos = [
- 'elixir-server'
- , 'cihsr-server'
- , 'chess-server'
- ]
-
- var exludeMergeRepos = {
- 'elixir-config-development' : true, 'elixir-config-test': true, 'elixir-config-production' : true
- , 'elixir-data' : true
- ,'cihsr-config-development' : true, 'cihsr-config-test': true, 'cihsr-config-production' : true
- , 'cihsr-data' : true
- }
-
-
- var productionRepos = [
- 'elixir-config-production'
- ]
- var productionIsAllowed = (process.env.NODE_ENV === 'production');
- if(productionIsAllowed) gitRepos = gitRepos.concat(productionRepos)
-
- var env = Object.assign({}, process.env);
- var __runcmd = function(label){
-
- var op = {
- 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' }
- , 'undefined' : ()=>{ return op.h(); }
- , 'reset' : ()=>{
-
-
-
- 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.forEach(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')
- return isRunningElevated().then(
- (isElevated) => {
- if(isElevated) {
- try {
- op[ processedArgs.label || processedArgs._[0] || 'h']()
- }
- catch(e){
- console.error('Error Invalid command : ' + e)
- fs.writeFileSync('run.done', 'success')
- }
- finally {
- }
- } else throw false;
- }
- )
- .catch(()=>{
- console.log('Requesting Elevated Privileges');
-
- try {
- fs.unlinkSync('run.done')
- fs.unlinkSync('run.log')
- }
- catch(e) { }
-
-
- nodeShellExec('where', ['node']).then(r => {
- console.log('result : ' + r)
-
-
- var namedArgs = [];
- Object.keys(processedArgs).forEach((v)=>{ v!='_' ? namedArgs.push('--'+v+'='+processedArgs[v]) : null; })
-
- process.env.NODE_ENV = process.env.NODE_ENV || 'development';
- var args = [__dirname + '/windowselevate.hta'].concat(processedArgs._).concat(namedArgs.join(' ')); args.push('--runas=self');
- args.push('--nodepath='+r[r.length-1])
- nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
- , {
- inherit : true
- , shell: true
- , env: process.env
- , runas : 'self'
- , title : `runas`
- }
- ).then(()=>{
-
- try {
- var runaslog = JSON.parse('[ { "success" : true, "result" : "runas Log" }' + fs.readFileSync('run.log', { flags : 'a+'}) + ']');
- runaslog.forEach((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){
-
- console.warn('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 }
-
-
-
-
-
-
-
-
-
-
-
-
-
- var repo = processedArgs._[1];
-
- var sequentialTaskShellCommands = [];
- if(!existsSync(`Z:/${repo}.git`)){
- sequentialTaskShellCommands = [
-
-
-
-
- ['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
- }]
-
- , ['git', ['remote', 'rename', 'origin', 'githubclone'], { cwd : `${repo}`}, (err)=>{
- console.log('Ignoring origin rename error : ' + err); return true;
- } ]
- , ['git', ['remote', 'add', 'origin', `${defaultRepoServer}/${repo}.git`], { cwd : `${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])
-
- }
- }
-
- sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd : `${repo}`}])
-
-
- var tasks = [];
- sequentialTaskShellCommands.forEach(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 : repo }
-
- if(pushable) {
- return [
- ['git', ['remote', 'set-url', remotename, url + '/' + repo], { cwd : repo }]
- ]
- }
- else {
- console.error('not supported for non-pushable')
- }
-
- }
- var x = (args)=>{
- return ()=>{
-
- return nodeShellExec.apply(null, args)
- }
-
- }
- var perform_git_seturl = (dir)=>{
- op['is-git-repo'](dir).then((code)=>{
- any( serial_perform_git_seturl(dir.name).map(x) )
- }).catch((e)=>{
-
- })
- }
-
- 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_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 : repo }
-
- if(pushable) {
- return [
- ['git', ['remote', 'add', remotename, url + '/' + repo], { cwd : repo }]
- , ['git', ['pull', remotename, branch], { cwd : repo }]
- , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd : repo }]
- ]
- }
- else {
-
- return [
- ['git', ['remote', 'add', remotename, url + '/' + repo], { cwd : repo }]
- , ['git', ['remote', `set-url`, '--push', remotename, 'no-pushing'], { cwd : repo }]
- , ['git', ['pull', remotename, branch], { cwd : repo }]
- , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd : repo }]
- ]
- }
-
- }
- var x = (args)=>{
- return ()=>{
-
- return nodeShellExec.apply(null, args)
- }
-
- }
- var perform_git_add = (dir)=>{
- op['is-git-repo'](dir).then((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) )
- })
- }
-
- }).catch((e)=>{
-
- })
- }
-
- 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 : repo }
-
- return [
- ['git', ['remote', 'remove', remotename], { cwd : repo }]
- ]
- }
- var x = (args)=>{
- return ()=>{
-
- return nodeShellExec.apply(null, args)
- }
-
- }
- var perform_git_remove = (dir)=>{
- op['is-git-repo'](dir).then((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')
- })
- }
-
- }).catch((e)=>{
-
- })
- }
-
- 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 : repo }
-
- return [
- ['git', ['remote', 'add', 'chess', `${defaultRepoServer}/${user}/${repo}.git`], { cwd : repo }]
- , ['git', ['remote', 'set-url', '--push', 'chess', 'no-pushing'], { cwd : repo }]
- , ['git', ['remote', 'set-url', 'origin', `${defaultRepoServer}/${user}/${repo}.git`], { cwd : repo }]
- ]}
- var x = (args)=>{
- return ()=>{
-
- return nodeShellExec.apply(null, args)
- }
-
- }
- var perform_init_gitea = (dir)=>{
- op['is-git-repo'](dir).then((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) )
- })
- }
-
- }).catch((e)=>{
-
- })
- }
-
- 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) => {
-
-
-
- var env = Object.assign({}, process.env);
-
- console.log('Running exlr pull : ' + path.dirname(__dirname))
-
-
-
-
-
-
-
-
-
- var useGitPull = processedArgs.useGitPull || false;
- var getPullCmd = (repo)=>{
-
-
- var pullCmd = [ gitInstallDir
- , ['-c', 'for i in `git remote`; do git pull $i master; done;']
- , { cwd : repo, title : 'pull all origins for ' + repo }]
-
- if(useGitPull) pullCmd = ['git', ['pull'], {
- inherit : true, shell: true,
- cwd : repo
-
- , runas : processedArgs.runas
- , title : `git pull ${repo}`
- }]
- return pullCmd
- }
-
- var performPull = (repo) => {
-
- if(exludeMergeRepos[repo]) return Promise.resolve({ 'skipped' : true })
-
-
- if(existsSync(repo)) {
- console.log('pulling ' + repo)
- return nodeShellExec.apply(null, getPullCmd(repo)).catch((e)=>{ console.error(e) })
- }
- else {
- console.log('cloning ' + repo)
-
- return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'],
- {
- inherit : true, shell: true,
- env: process.env
- , runas : processedArgs.runas
- }).catch((e)=>{ console.error(e) }).then(()=>{
-
- return nodeShellExec('git', ['config', '--replace-all' , 'core.symlinks', true],
- {
- inherit : true, shell: true,
- env: process.env
- , cwd : repo
- , runas : processedArgs.runas
- , title : `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
- })
- })
- }
- }
-
- if(!processedArgs.runas) gitRepos.forEach(performPull)
- return isRunningElevated().then(
- (isElevated) => {
- if(isElevated) {
- any(elevatedRunasRepos.map((repo)=>performPull(repo))).then(()=>{
- fs.writeFileSync('run.done', 'success')
- }).catch(()=>{
- fs.writeFileSync('run.done', 'error')
- })
- }
- else throw false;
- }
- ).catch(
- () => {
- op['runas']()
- }
- )
- }
-
- , 'pull' : (label) => {
-
-
-
- var env = Object.assign({}, process.env);
-
- console.log('Running exlr pull : ' + path.dirname(__dirname))
-
-
-
-
-
-
-
-
-
- var useGitPull = processedArgs.useGitPull || false;
- var getPullCmd = (repo)=>{
-
-
- var pullCmd = [ gitInstallDir
- , ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
- , { cwd : repo, title : 'pull all origins for ' + repo }]
-
- if(useGitPull) pullCmd = ['git', ['pull'], {
- inherit : true, shell: true,
- cwd : repo
-
- , runas : processedArgs.runas
- , title : `git pull ${repo}`
- }]
- return pullCmd
- }
-
- var errors = [];
- var performPull = (repo) => {
- if(existsSync(repo)) {
- console.log('pulling ' + repo)
- return nodeShellExec.apply(null, getPullCmd(repo)).then((srepo)=>{
- statuslog.statuslog(null, srepo)}).catch((e)=>{ statuslog.statuslog(e, srepo); console.error(e) })
- }
- else {
-
- return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'],
- {
- inherit : true, shell: true,
- env: process.env
- , runas : processedArgs.runas
- }).catch((e)=>{
- errors.push({ repo , e})
- console.error(e)
- }).then(()=>{
-
- return nodeShellExec('git', ['config', '--replace-all' , 'core.symlinks', true],
- {
- inherit : true, shell: true,
- env: process.env
- , cwd : repo
- , runas : processedArgs.runas
- , title : `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
- })
- })
- }
- }
-
- if(!processedArgs.runas) {
- var pendingpulls = [];
- gitRepos.forEach( (r)=>{
- pendingpulls.push(performPull(r))
- } )
- Promise.all(pendingpulls).then(results =>{
-
- console.log('Total : ' + (statuslog.log.SKIPPED.length + statuslog.log.SUCCESS.length + statuslog.log.FAILURE.length))
- console.log('Pass : ' + statuslog.log.SUCCESS.length )
- console.log('Fail : ' + statuslog.log.FAILURE.length)
- console.log('Skipped : ' + statuslog.log.SKIPPED.length)
- })
- }
- return isRunningElevated().then(
- (isElevated) => {
- if(isElevated) {
- any(elevatedRunasRepos.map((repo)=>performPull(repo))).then(()=>{
- fs.writeFileSync('run.done', 'success')
- }).catch(()=>{
- fs.writeFileSync('run.done', 'error')
- })
- }
- else throw false;
- }
- ).catch(
- () => {
- op['runas']()
- }
- )
- }
- , 'isInstalled' : ()=>{
- return nodeShellExec('where', [processedArgs._[1]], { inherit : true} ).then(()=>{
- console.log(processedArgs._[1] + ' exists.')
- return true;
- });
- }
- , 'npmi' : ()=>{
- var tasks = [];
- var bowerRepos = ['client']
-
- var npmbuildrepos = ['loopback-jsonapi-model-serializer']
- npmbuildrepos.forEach(repo => {
- tasks.push(()=>{
- var p = nodeShellExec('npm', ['run build'], {
- inherit : true, shell: true
- , cwd : repo
- , env: process.env
- , title : `npm build for ${repo}`
- }).catch((e)=>{ console.error(e) })
- return p;
- })
- })
- bowerRepos.forEach(repo => {
- tasks.push(()=>{
- var p = nodeShellExec('bower', ['install'], {
- inherit : true, shell: true
- , cwd : repo
- , env: process.env
- , title : `bower i for ${repo}`
- }).catch((e)=>{ console.error(e) })
- return p;
- })
- })
- gitRepos = gitRepos.concat(elevatedRunasRepos);
- gitRepos.push('client/server');
- gitRepos.forEach(repo => {
-
- console.log('npm i for ' + repo)
-
-
-
-
-
-
- nodeShellExec('rm', ['package-lock.json'], {
- inherit : true, shell: true
- , cwd : repo
- , env: process.env
- , title : `rm 'package-lock.json' for ${repo}`
- }).catch((e)=>{ console.error(e) })
-
- if( npmbuildrepos.indexOf(repo) < 0) {
- tasks.push(()=>{
- var p = nodeShellExec('npm', ['i --force'], {
- inherit : true, shell: true
- , cwd : repo
- , env: process.env
- , title : `npm i for ${repo}`
- }).catch((e)=>{ console.error(e) })
- return p;
- })
- }
- })
- any(tasks);
-
- }
- , 'start' : (label)=>{
- console.log('Starting Elixir Server.');
- var env = Object.assign({}, process.env);
-
- env.NODE_ENV = process.env.NODE_ENV || 'development';
- env.DEBUG = 'loopback:connector:' + dbForLabel(label)
- var cmd = env.NODE_ENV === 'development' ? 'nodemon' : 'node';
-
- cmd = [cmd, ['--inspect=9228', 'elixir/server.js']]
- var childPromise = nodeShellExec(...cmd, {
-
- shell: true,
- detached: true,
- stdio: 'ignore',
- cwd : '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'
- })
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- console.log('Starting Elixir Client Host.');
- var cmd = ['ember', ['s']]
- var childPromise = nodeShellExec(...cmd, {
-
-
-
- shell: true,
- detached: true,
- stdio: 'ignore',
- cwd : 'client'
- , env: env
- })
-
-
- 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' : ()=>{
-
-
-
-
-
-
-
-
-
- var runconfig = { NODE_ENV : process.env.NODE_ENV }
- try { runconfig = Object.assign(runconfig, require('../run.js')) } catch(e) { }
- if((!processedArgs.runas || processedArgs.runas !== 'self') &&
- runconfig.NODE_ENV && runconfig.NODE_ENV === (process.env.NODE_ENV || runconfig.NODE_ENV) &&
- processedArgs._[1] && runconfig.use === processedArgs._[1]) {
-
- 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.' }
-
- fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig))
-
- var checkoutMap = {
- 'development' : 'master',
- }
-
-
- 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], {
-
-
- cwd : repo
-
- , runas : processedArgs.runas
- , title : `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'], {
-
- cwd : 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 : repo
- , runas : processedArgs.runas
- }).catch((e)=>{ console.error(e) })
- }
- if(runconfig.NODE_ENV === 'development') performMerge = ()=>{ return Promise.resolve(true) }
-
- any(tasks).then(()=>{
- if(!processedArgs.runas) return op['runas']()
-
- tasks = [
- ()=>{
-
- var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-config' + '-' + process.env.NODE_ENV ], {
- inherit : true, shell: true
- , 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
- , env: process.env
- }).catch((e)=>{ console.error(e) })
- return p;
- }
- ]
- )
- }
-
- return any(tasks)
-
- .then(
- () => any([ any(gitRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))
- , any(elevatedRunasRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))]) )
- .then(
- () => any([ any(gitRepos.map((repo)=>performPullAll(repo))), any(elevatedRunasRepos.map((repo)=>performPullAll(repo)))]) )
- .then(
- () => 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(
- () => any([ any(gitRepos.map((repo)=>performPullAll(repo))), any(elevatedRunasRepos.map((repo)=>performPullAll(repo)))]) )
- .then(
- () => any([ any(gitRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))
- , any(elevatedRunasRepos.map((repo)=>performCheckout(repo, process.env.NODE_ENV || 'development')))]) )
- .then(
- () => 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( () => {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return Promise.resolve(true)
-
- })
- .then(()=>{
- fs.writeFileSync('run.done', 'success')
- }).catch(()=>{
- fs.writeFileSync('run.done', 'error')
- })
- }).catch(()=>{
- fs.writeFileSync('run.done', 'error')
- })
-
-
-
-
-
-
- }
- , 'g' : ()=>{
- if(processedArgs.h) {
-
- console.log('elxr g [modelname] => generate a model named [modelname]');
- console.log('elxr g => regenerate all known models');
- return
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(0){
-
- nodeShellExec('pwd', { inherit : true});
-
-
-
- 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' ]);
-
- }
-
-
-
-
- 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))
-
-
- 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]]();
- }
- }
- return op[label] ? op[label]() : null;
- }
-
-
-
-
- var mysql = '../xampp/mysql/bin/mysql'
- var mysqldump = '../xampp/mysql/bin/mysqldump'
-
-
- if(processedArgs.runas) {
-
- __runcmd('runas')
- }
- else __runcmd(processedArgs.label || processedArgs._[0] || 'h');
-
- function nodeShellExec() {
-
- var args = Array.from(arguments);
- var opts = args[2] = args[2] || {}
- opts.title ? null : opts.title = `${args[0]} ${args[1] }`
- const child = spawn(...arguments);
-
- var p = new Promise(function(resolve, reject){
- if(!opts.detached) {
- var messages = [];
- var success = true;
-
- if(opts.stdio !== 'ignore') {
- child.stdout.setEncoding('utf8');
- child.stderr.setEncoding('utf8');
- child.stdout.on('data', (chunk) => { chunk.trim() === '' ? null : messages.push(chunk); });
- child.on('error', (chunk) => { success = false; messages.push(chunk); } );
- child.stderr.on('data', (chunk) => { messages.push(chunk);
-
- });
- }
- child.on('close', (code) => {
- if(+code !== 0) success = false;
- if(opts.stdio !== 'ignore') {
- if(opts.runas){
- var logEntry = { result: `${opts.title} exited with code ${code}`, messages : messages }
- success ? logEntry.success = true : null;
- fs.writeFileSync('run.log', ', ' + JSON.stringify(logEntry), {'flag':'a+'} )
- }
- else {
- success ? console.log(['success : ' + ` ${opts.title} exited with code ${code}`]) : console.error([`error : ${opts.title} exited with code ${code}`])
-
- process.stdout.write( messages.join('') )
- }
- }
- if(code !== 0) return reject(code)
- resolve(messages)
- });
- }
- else {
- child.unref()
- resolve(true);
- }
- });
- p.process = child;
- return p;
- }
|