Browse Source

Refactor for elevated IPC

pull/9/head
pb 2 years ago
parent
commit
efaa8f0bc4
1 changed files with 68 additions and 39 deletions
  1. 68
    39
      index.js

+ 68
- 39
index.js View File

global.env = process.env
global.env.NODE_ENV = global.env.NODE_ENV || 'development'
var __g = require('global-this');
__g.env = process.env
__g.env.NODE_ENV = __g.env.NODE_ENV || 'development'



const fs = require('fs') const fs = require('fs')
var path = require('path'); var path = require('path');
var utils = require('bbhverse'); var utils = require('bbhverse');



const cliargs = utils.cliargs;
const processedArgs = cliargs(process.argv.slice(2));
if(!processedArgs.wd){
if(process.env.wd){
}
else {
process.env.wd = process.cwd();
if(process.cwd() === __dirname) {
// This script was launched in the same dir.
// We do not know if it was copied to some random location. Check if we have elxr installed in current location. Or subidr.
process.env.wd = path.dirname( process.env.wd ); // Default to the parent.
}
}
}
else process.env.wd = processedArgs.wd
var ENV = Object.assign({}, process.env); // Shallow clone it.

const __ALIAS__STAMP__ = '9e7bebe0-1f57-11ec-8f88-778ffeea9d1b'
const BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]';
const runtimestamp = processedArgs.runtimestamp ? (new Date(processedArgs.runtimestamp)).getTime() : (new Date()).getTime();
function getVersion() { return BUILD_VERSION; }
console.log(getVersion());


var selectedinstance = (function() {
var __pvt = { root : process.env.wd || process.env.PWD }
var __pub = {
get root(){ return __pvt.root }
, set root(val){
throw 'root has changed'
// if the root changes for this run we need to relocate all the necessary files to that folder for this run.
if(__pvt.root === val){
fsMove()
return __pvt.root = val
}
else __pvt.root
}
}

Object.defineProperty(__g, 'selectedinstance', {
get(){ return __pub}
, set( inst ){ __pvt = inst; return __pub }
})
return __pub;
})()

var any = utils.any; var any = utils.any;
var Tasq = utils.Tasq var Tasq = utils.Tasq
var statuslog = utils.Traq var statuslog = utils.Traq
var Traq = utils.Traq var Traq = utils.Traq
Tasq.addlistener(statuslog.statuslog) Tasq.addlistener(statuslog.statuslog)
// fs.writeFileSync('run.log', ', ' + JSON.stringify( { m : 'Hello'} ), { 'flag': 'a+' })
Tasq.addlistener((e)=>{ fs.writeFileSync('run.log', ', ' + JSON.stringify( e ), { 'flag': 'a+' }) })

// Instead of waiting for the root to be establised we start working at the current locatoin and then relocate when root changes.
var runlogjson = `${selectedinstance.root}/.elxr/run-${runtimestamp}/run.log`
ensureDirectoryExistence(runlogjson)
fs.writeFileSync(runlogjson, JSON.stringify( `Started ${runtimestamp}`)) // Initialize a new log file with "logrotate" for every run
Tasq.addlistener((e)=>{ fs.writeFileSync(runlogjson, ', ' + JSON.stringify( e ), { 'flag': 'a+' }) })
var cli = require('./cliverse') var cli = require('./cliverse')
function isWin(){ return /^win/.test(process.platform) } function isWin(){ return /^win/.test(process.platform) }
if(isWin()) { if(isWin()) {
var shell_verse = lin_verse; var shell_verse = lin_verse;
} }




var nodeShellExec = cli.nodeShellExec; var nodeShellExec = cli.nodeShellExec;
var chalk = require('chalk') var chalk = require('chalk')
const homedir = require('os').homedir(); const homedir = require('os').homedir();



function sysAddPathVar(addpath){ function sysAddPathVar(addpath){
// Object.assign({ // Object.assign({
// inherit: true, shell: true, env: ENV, title: `${command} ${args}` // inherit: true, shell: true, env: ENV, title: `${command} ${args}`
$newpath = $path + ';${addpath}' $newpath = $path + ';${addpath}'
[Environment]::SetEnvironmentVariable("PATH", $newpath, 'Machine')` [Environment]::SetEnvironmentVariable("PATH", $newpath, 'Machine')`
]).then(() => { ]).then(() => {
fs.writeFileSync('run.done', 'success')
fs.writeFileSync('run.done', 'success') // PB : TODO -- IPC through files is needed only for windows.
}).catch(() => { }).catch(() => {
fs.writeFileSync('run.done', 'error') fs.writeFileSync('run.done', 'error')
}) })
return any(tasks); return any(tasks);
} }


const __ALIAS__STAMP__ = '9e7bebe0-1f57-11ec-8f88-778ffeea9d1b'
const BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]';
const runtimestamp = (new Date()).getTime();
function getVersion() { return BUILD_VERSION; }
console.log(getVersion());

// 'use strict'; // 'use strict';


// PB : TODO -- make sure folder context is proper coz we can now run elxr from anywhere. // PB : TODO -- make sure folder context is proper coz we can now run elxr from anywhere.
} }
const existsFolderSync = existsSync; const existsFolderSync = existsSync;


const cliargs = utils.cliargs;
const processedArgs = cliargs(process.argv.slice(2));
if(!processedArgs.wd){
if(process.env.wd){
}
else {
process.env.wd = process.cwd();
if(process.cwd() === __dirname) {
// This script was launched in the same dir.
// We do not know if it was copied to some random location. Check if we have elxr installed in current location. Or subidr.
process.env.wd = path.dirname( process.env.wd ); // Default to the parent.
}
}
}
else process.env.wd = processedArgs.wd
var ENV = Object.assign({}, process.env); // Shallow clone it.






} }
]; ];
return any(tasks).then(() => { return any(tasks).then(() => {
fs.writeFileSync('run.done', 'success')
fs.writeFileSync('run.done', 'success') // PB : TODO -- IPC through files is needed only for windows.
}).catch(() => { }).catch(() => {
fs.writeFileSync('run.done', 'error') fs.writeFileSync('run.done', 'error')
}) })
selectedinstance.instanceName && runconfig.use === selectedinstance.instanceName) { selectedinstance.instanceName && runconfig.use === selectedinstance.instanceName) {


console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`) console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`)
if (processedArgs.runas) { fs.writeFileSync('run.done', 'success') }
if (processedArgs.runas) { fs.writeFileSync('run.done', 'success') } // PB : TODO -- IPC through files is needed only for windows.
if (existsSync('config') && existsSync('data')){ if (existsSync('config') && existsSync('data')){
return return
} }
console.warn('elevated repo manifest has obsolete format. Attempting upgrade.') console.warn('elevated repo manifest has obsolete format. Attempting upgrade.')
selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } }) selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } })
} }
chessinstances[selected.instanceName][selected.node_env] = selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
chessinstances[selected.instanceName][selected.node_env] = selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
, selected, selectedinstance) , selected, selectedinstance)
chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers)) chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers))
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...
console.warn('elevated repo manifest has obsolete format. Attempting upgrade.') console.warn('elevated repo manifest has obsolete format. Attempting upgrade.')
selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } }) selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } })
} }
chessinstances[selected.instanceName][selected.node_env] = selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
chessinstances[selected.instanceName][selected.node_env] = selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
, selected, selectedinstance) , selected, selectedinstance)
chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers)) chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers))
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...




// function updateselection(selected) { selectedinstance = utils.assign(selectedinstance, selected) } // function updateselection(selected) { selectedinstance = utils.assign(selectedinstance, selected) }
var selectedinstance = { root : process.env.wd };
var runconfig = null; var runconfig = null;


var chessinstances = { current_run : {} }; var chessinstances = { current_run : {} };
// PB : TODO -- We now have options that can be passed into assign_core to control this behavior. // PB : TODO -- We now have options that can be passed into assign_core to control this behavior.
if(selected_overrides.node_env === undefined) delete selected_overrides.node_env if(selected_overrides.node_env === undefined) delete selected_overrides.node_env
selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
, __default , __default
, chessinstances[instanceName][node_env] , chessinstances[instanceName][node_env]
, clioverrides , clioverrides
var timer = function(){ var timer = function(){
l('.'); l('.');
if(fso.FileExists("run.done")) { if(fso.FileExists("run.done")) {
fso.DeleteFile('run.done')
fso.DeleteFile('run.done') // PB : TODO -- IPC through files is needed only for windows we need to do it per run...
close(); close();
} }
else window.setTimeout(timer, 1000); else window.setTimeout(timer, 1000);
var cmdprompts = cmdinstance.getPossiblePrompts() var cmdprompts = cmdinstance.getPossiblePrompts()
selectedinstance.node_env ? selectedinstance.node_env : selectedinstance.node_env = clioverrides.node_env selectedinstance.node_env ? selectedinstance.node_env : selectedinstance.node_env = clioverrides.node_env
// PB : TODO -- Most recent should be at the tip ! at index 0 so utils.reverseassign is required !!! // PB : TODO -- Most recent should be at the tip ! at index 0 so utils.reverseassign is required !!!
selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
, ...detectedinstanceoptions.slice(-2), promptkeys ) , ...detectedinstanceoptions.slice(-2), promptkeys )
// promptkeys = utils.assign(promptkeys, clioverrides) // promptkeys = utils.assign(promptkeys, clioverrides)



Loading…
Cancel
Save