瀏覽代碼

Merge branch 'production' of http://git.bbh/chess/elxr into production

pull/9/head
anishbhattacharjee 3 年之前
父節點
當前提交
b233ae41f1
共有 1 個檔案被更改,包括 138 行新增70 行删除
  1. 138
    70
      index.js

+ 138
- 70
index.js 查看文件

@@ -36,7 +36,17 @@ 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}`
@@ -67,17 +77,18 @@ var getTaskCheckExists = (command, options) => {
}

var getPullCmd = (repo, branch) => {
// console.log(useGitPull)var getPullCmd = (repo, branch) => {
// console.log(useGitPull)

if(branch) {
var pullCmd = [ gitInstallDir
var pullCmd = [ gitbash
, ['-c', 'for i in `git remote`; do git pull $i ' + branch + '; done;']
, { cwd : repo, title : 'pull all origins for ' + repo }]
, { cwd: instanceroot + '/' + repo, title : 'pull all origins for ' + repo }]
}
else {
var pullCmd = [gitInstallDir
var pullCmd = [gitbash
, ['-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 }]
, { cwd: instanceroot + '/' + repo, title: 'pull all origins for ' + repo }]
}
// var pullCmd = ['pullall', [], { cwd : repo }]
if (useGitPull) pullCmd = ['git', ['pull'], {
@@ -90,11 +101,27 @@ var getPullCmd = (repo, branch) => {
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)) {
console.log('pulling ' + instanceroot + '/' + repo + 'branch ' + branch)
var branchprint = branch ? ' branch :' + branch : '';
console.log('pulling ' + instanceroot + '/' + repo + branchprint )
return nodeShellExec.apply(null, getPullCmd(repo, branch)).then(() => {
if (__isElevated) {
if (__isElevated) {
fs.writeFileSync('run.log', ', ' + JSON.stringify({ repo, success: true }), { 'flag': 'a+' })
}
else statuslog.statuslog(null, repo)
@@ -114,7 +141,8 @@ var performPull = (repo, branch) => {
return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'],
{
inherit: true, shell: true,
env: process.env
env: process.env
, cwd : instanceroot
, runas: processedArgs.runas
}).catch((e) => {
throw e;
@@ -124,7 +152,7 @@ var performPull = (repo, branch) => {
{
inherit: true, shell: true,
env: process.env
, cwd: repo
, cwd: instanceroot + '/' + repo
, runas: processedArgs.runas
, title: `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
})
@@ -199,8 +227,8 @@ var dbForLabel = function (label) {
}

// SAM : TODO Use nodeshellexec where to detect git installation dir
var gitInstallDir = "C:\\Program Files\\Git\\bin\\sh.exe"
// var gitInstallDir = "G:\\Installed\\Git\\bin\\sh.exe"
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 = [];
@@ -209,7 +237,13 @@ var configPromise = null

// elxr cli operations
var op = {
'h': () => { console.log(elxr.info()); return '-h' }
'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': () => {

@@ -415,10 +449,10 @@ var op = {
, env: process.env
}]
// PB : TODO -- Do this conditionally only...
, ['git', ['remote', 'rename', 'origin', 'githubclone'], { cwd: `${repo}` }, (err) => {
, ['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: `${repo}` }]
, ['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.
]
@@ -432,7 +466,7 @@ var op = {
}
}

sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd: `${repo}` }])
sequentialTaskShellCommands.push(['git', ['push', 'origin', 'master'], { cwd: `${instanceroot + '/' + repo}` }])
// console.dir(sequentialTaskShellCommands);

var tasks = [];
@@ -455,11 +489,11 @@ var op = {
remotename = remotename || processedArgs._[1]
url = url || processedArgs._[2]
var serial_perform_git_seturl = (repo) => {
var options = { cwd: repo }
var options = { cwd: instanceroot + '/' + repo }
// console.log(repo)
if (pushable) {
return [
['git', ['remote', 'set-url', remotename, url + '/' + repo], { cwd: repo }]
['git', ['remote', 'set-url', remotename, url + '/' + repo], { cwd: instanceroot + '/' + repo }]
]
}
else {
@@ -476,14 +510,6 @@ var op = {
})
}

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) => {
@@ -492,22 +518,22 @@ var op = {
url = url || processedArgs._[2]
branch = branch || processedArgs._[3]
var serial_perform_git_add = (repo) => {
var options = { cwd: repo }
var options = { cwd: instanceroot + '/' + repo }
// console.log(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 }]
['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: 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 }]
['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 }]
]
}

@@ -549,10 +575,10 @@ var op = {
, 'remove': (remotename) => {
remotename = remotename || processedArgs._[1]
var serial_perform_git_remove = (repo) => {
var options = { cwd: repo }
var options = { cwd: instanceroot + '/' + repo }
// console.log(repo)
return [
['git', ['remote', 'remove', remotename], { cwd: repo }]
['git', ['remote', 'remove', remotename], { cwd: instanceroot + '/' + repo }]
]
}
var x = (args) => {
@@ -593,12 +619,12 @@ var op = {
user = user || processedArgs._[1]
if (!user) throw 'User name required'
var serial_perform_init_gitea = (repo) => {
var options = { cwd: repo }
var options = { cwd: instanceroot + '/' + repo }
// console.log(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 }]
['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) => {
@@ -643,7 +669,6 @@ var op = {
// console.dir(env)
console.log('Running exlr pull : ' + path.dirname(__dirname))

// SAM : TODO Exclude data repo
if (!processedArgs.runas) gitRepos.map((repo) => performPull(repo, 'master'))
if (__isElevated) {
return any(elevatedRunasRepos.map((repo) => performPull(repo, 'master'))).then(() => {
@@ -722,13 +747,18 @@ var op = {
, '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: repo
, cwd: instanceroot + '/' + repo
, env: process.env
, title: `npm i for ${repo}`
}).catch((e) => {
@@ -737,7 +767,7 @@ var op = {
console.log(`--------------------npm run build for ${repo}--------------------`)
return nodeShellExec('npm', ['run build'], {
inherit: true, shell: true
, cwd: repo
, cwd: instanceroot + '/' + repo
, env: process.env
, title: `npm run build for ${repo}`
}).then(Tasq.then).catch(Tasq.catch)
@@ -750,48 +780,84 @@ var op = {
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('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) })
})
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'], {
var p = nodeShellExec('npm', ['i', '--force'], {
inherit: true, shell: true
, cwd: repo
, env: process.env
, cwd: instanceroot + '/' + repo
, env
, title: `npm i for ${repo}`
}).then(Tasq.then).catch(Tasq.catch)
}).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(() => {
var p = nodeShellExec('bower', ['install'], {
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: repo
, 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 => {

@@ -812,7 +878,7 @@ var op = {
shell: true,
detached: true,
stdio: 'ignore',
cwd: 'elixir-server'
cwd: instanceroot + '/' + 'elixir-server'
, env: env
})

@@ -847,7 +913,7 @@ var op = {
shell: true,
detached: true,
stdio: 'ignore',
cwd: 'client'
cwd: instanceroot + '/' + 'client'
, env: env
})
// .catch(e=>console.error(e))
@@ -938,11 +1004,11 @@ var op = {
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: repo
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}`
}).catch((e) => { console.error(e); return { error: true, message: 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) }

@@ -950,7 +1016,7 @@ var op = {
if (excludeCheckouts[repo]) return Promise.resolve({ 'skipped': true })
return nodeShellExec('git', ['pull', '--all'], {
// inherit : true, shell: true,
cwd: repo
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 } })
@@ -970,13 +1036,13 @@ var op = {
if (exludeMergeRepos[repo]) return Promise.resolve({ 'skipped': true })
return nodeShellExec('git', ['merge', mergeSource], {
inherit: true, shell: true,
cwd: repo
cwd: instanceroot + '/' + repo
, runas: processedArgs.runas
}).catch((e) => { console.error(e) })
}
if (runconfig.NODE_ENV === 'development') performMerge = () => { return Promise.resolve(true) }

any(tasks).then(() => {
return any(tasks).then(() => {
if (!processedArgs.runas) return op['runas']()

tasks = [
@@ -984,6 +1050,7 @@ var op = {
// 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;
@@ -995,6 +1062,7 @@ var op = {
() => {
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;
@@ -1380,7 +1448,7 @@ var acquireConfig = function (selected, chessinstances) {
})
}

var launchpath = process.cwd()
var launchpath = process.cwd().replace(/\\/, '/')
var thisscriptdir = __dirname
var instanceroot = launchpath;
var detectInstance = function () {
@@ -1400,13 +1468,13 @@ var detectInstance = function () {
// 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 + '../');
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.
// Probably in the right place.
root = path.normalize(launchpath + '/..');
}
else {
@@ -1417,7 +1485,7 @@ var detectInstance = function () {
}
}
}
instanceroot = root;
instanceroot = root.replace(/\\/, '/');
var __default = {

// Default is public server only.
@@ -1585,8 +1653,8 @@ var maintask = () => {
}
catch (e) {
console.error(e)
console.error('No local instances found in current root = ' + selectedinstance.root);
console.log('We will create a new one with the instance and environment chosen...')
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 {

Loading…
取消
儲存