Browse Source

Added dedup for array merge.

master
chess 3 years ago
parent
commit
5769618cb0
1 changed files with 33 additions and 14 deletions
  1. 33
    14
      index.js

+ 33
- 14
index.js View File

// Or a single repo ( Either Elevated or normal. ) // Or a single repo ( Either Elevated or normal. )




def = (def ? (
// def.requiresElevation = true,
// def.repos = def.repos,
// def.elevatedRepos = def.elevatedRunasRepos,
def

) : false) || {
def = def || {
repos : repomanifest.repos, repos : repomanifest.repos,
elevatedRepos : repomanifest.elevated
elevated : repomanifest.elevated
} }


var elevatedpulltasks = null; var elevatedpulltasks = null;


var useGitPull = processedArgs.useGitPull || false; var useGitPull = processedArgs.useGitPull || false;


if(def.elevatedRepos || def.repos) {
if(def.elevatedRepos) def.requiresElevation = true;
if(def.elevated || def.repos) {
if(def.elevated) def.requiresElevation = true;
else delete def.requiresElevation; else delete def.requiresElevation;
} }
else { else {
if(def.requiresElevation) def.elevatedRepos = [def]
if(def.requiresElevation) def.elevated = [def]
else def.repos = [def] else def.repos = [def]
} }


var regularpulltasks = function(){ return Promise.resolve(true) } var regularpulltasks = function(){ return Promise.resolve(true) }
if(def.elevatedRepos){
if(def.elevated){
elevatedpulltasks = function() { elevatedpulltasks = function() {
return any(def.elevatedRepos.map((def) => performPull(def.repo))).then(() => {
return any(def.elevated.map((def) => performPull(def.repo))).then(() => {
return true; return true;
}).catch((e) => { }).catch((e) => {
console.error(e) console.error(e)
}) })
} }


var mergeObjByKey = function(arrOfObjs, keyName) {

var keyedDistinct = {}
var distinctArrOfObjs = []

arrOfObjs.forEach( o => {
if(o) (keyedDistinct[o[keyName]] || (keyedDistinct[o[keyName]] = []) ).push(o)
})
Object.keys(keyedDistinct).forEach(key => {
distinctArrOfObjs.push( utils.assign( ...keyedDistinct[key] ) ) // PB : TODO -- Shallow use utils.assign
})

return distinctArrOfObjs;
}

var cacheWriteInstanceConfig = function(chessinstances){
var instanceName = chessinstances['current_run'].instanceName;
var node_env = chessinstances['current_run'].node_env;
chessinstances[instanceName][node_env].repos = mergeObjByKey(chessinstances[instanceName][node_env].repos, 'repo') ;
chessinstances[instanceName][node_env].elevated = mergeObjByKey(chessinstances[instanceName][node_env].elevated, 'repo') ;
fs.writeFileSync(instanceroot + '/chessinstances.js', 'module.exports = ' + JSON.stringify(chessinstances, null, 2) + '', { 'flag': 'w' })
}

var acquireConfig = function (selected, chessinstances) { var acquireConfig = function (selected, chessinstances) {


var configrepo = selected.instanceName + '-config-' + selected.node_env; var configrepo = selected.instanceName + '-config-' + selected.node_env;
chessinstances[selected.instanceName][selected.node_env] = chessinstances[selected.instanceName][selected.node_env] || {} chessinstances[selected.instanceName][selected.node_env] = chessinstances[selected.instanceName][selected.node_env] || {}
chessinstances['current_run'] = { instanceName: selected.instanceName, node_env: 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. // Config from server always override merges into selection ecept for the current selection.
// PB : TODO -- utils.assign Array merges are non-distinct...
selectedinstance = utils.assign(chessinstances[selected.instanceName][selected.node_env], selected, repomanifest) selectedinstance = utils.assign(chessinstances[selected.instanceName][selected.node_env], selected, repomanifest)
// chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance; // chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance;
fs.writeFileSync(instanceroot + '/chessinstances.js', 'module.exports = ' + JSON.stringify(chessinstances, null, 2) + '', { 'flag': 'w' })
cacheWriteInstanceConfig(chessinstances)
selectedinstance = chessinstances[selected.instanceName][selected.node_env]
// PB : TODO -- We should probably write the new server config also... // PB : TODO -- We should probably write the new server config also...
selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online... selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online...
ENV.NODE_ENV = selectedinstance.node_env; ENV.NODE_ENV = selectedinstance.node_env;

Loading…
Cancel
Save