Bläddra i källkod

promt enhancements

production
chess 3 år sedan
förälder
incheckning
d853b4ee5d
1 ändrade filer med 161 tillägg och 119 borttagningar
  1. 161
    119
      index.js

+ 161
- 119
index.js Visa fil

@@ -138,7 +138,7 @@ var performPull = (repo, branch) => {
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'],
return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', selectedinstance.reposerver + `/${defaultRepoOwner}/` + repo + '.git'],
{
inherit: true, shell: true,
env: process.env
@@ -154,7 +154,7 @@ var performPull = (repo, branch) => {
env: process.env
, cwd: instanceroot + '/' + repo
, runas: processedArgs.runas
, title: `git core.symlinks --replace-all true for ${defaultRepoServer + `/${defaultRepoOwner}/` + repo + '.git'}`
, title: `git core.symlinks --replace-all true for ${selectedinstance.reposerver + `/${defaultRepoOwner}/` + repo + '.git'}`
})
.then(() => {
if (__isElevated) {
@@ -210,7 +210,6 @@ var currentGitAuthUser; // nodeShellExec('git', ['config', 'user.email']) ... PB
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

@@ -432,7 +431,7 @@ var op = {
var sequentialTaskShellCommands = [];
if (!existsSync(`Z:/${repo}.git`)) {
sequentialTaskShellCommands = [
// ['net', ['use', 'Z:', defaultRepoServer.replace('/','\\')], {
// ['net', ['use', 'Z:', selectedinstance.reposerver.replace('/','\\')], {
// inherit : true, shell: true
// , env: process.env
// }]
@@ -452,12 +451,12 @@ var op = {
, ['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}` }]
, ['git', ['remote', 'add', 'origin', `${selectedinstance.reposerver}/${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, '\\')], {
sequentialTaskShellCommands.splice(0, 0, ['net', ['use', 'Z:', selectedinstance.reposerver.replace(/\//gm, '\\')], {
inherit: true, shell: true
, env: process.env
}])
@@ -622,9 +621,9 @@ var op = {
var options = { cwd: instanceroot + '/' + repo }
// console.log(repo)
return [
['git', ['remote', 'add', 'chess', `${defaultRepoServer}/${user}/${repo}.git`], { cwd: instanceroot + '/' + repo }]
['git', ['remote', 'add', 'chess', `${selectedinstance.reposerver}/${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 }]
, ['git', ['remote', 'set-url', 'origin', `${selectedinstance.reposerver}/${user}/${repo}.git`], { cwd: instanceroot + '/' + repo }]
]
}
var x = (args) => {
@@ -1347,7 +1346,7 @@ 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;
var prompt = cli.prompter;
return prompt.ask(`Choose an option :
d) Install the default chess instance.
=> elxr i chess node_env=development --default
@@ -1366,7 +1365,7 @@ We will run your choice of default or create your own at the next prompt.
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'
selectedinstance.reposerver = '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..
}
@@ -1376,14 +1375,14 @@ We will run your choice of default or create your own at the next prompt.
console.log(elxr.help()); process.exit()
}
else if (choice === 'n' || choice === 'i') {
var p1 = cli.prompt;
var p1 = cli.prompter;
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'
selectedinstance.reposerver = '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();
@@ -1391,12 +1390,12 @@ We will run your choice of default or create your own at the next prompt.

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'
selectedinstance.reposerver = reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
})
})
})
} else if (choice === 'c') {
var p1 = cli.prompt;
var p1 = cli.prompter;
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) {
@@ -1410,7 +1409,7 @@ We will run your choice of default or create your own at the next prompt.
}
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'
selectedinstance.reposerver = reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
p1.close()
})
})
@@ -1428,24 +1427,29 @@ We will run your choice of default or create your own at the next prompt.
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.')
return (async ()=>{return await __default.reposerver})().then(()=>{

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, null, 2) + '', { 'flag': 'w' })
// PB : TODO -- We should probably write the new server config also...
selectedinstance.reposerver = 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(/\\/, '/')
@@ -1486,10 +1490,11 @@ var detectInstance = function () {
}
}
instanceroot = root.replace(/\\/, '/');
return Object.assign({ root }, __default);
__default.root = root;
return Promise.resolve(__default);
}

//
var __default = {

// Default is public server only.
@@ -1515,6 +1520,27 @@ var __default = {
, node_env: 'development'
}

var __interactve_promts = {
get reposerver(){
return cli.prompt(this.reposervers, 'git default repo').then(reposerver => {
Object.defineProperty(this, 'reposerver', {
value: reposerver,
writable: false,
configurable : true
});
return reposerver
})
}
, set reposerver(reposerver){
Object.defineProperty(this, 'reposerver', {
value: reposerver,
writable: false,
configurable : true
});
return reposerver
}
}

var downloadsdir = '../Downloads';
var prerequisites = [
{
@@ -1527,7 +1553,7 @@ var prerequisites = [
// , '/MERGETASKS=!runcode' // This is required only for vscode...
]]
, preinstallsteps: function() {
var prompt = cli.prompt;
var prompt = cli.prompter;
var steps = [
() => {
if (!existsSync(downloadsdir + '/' + this.installer)) {
@@ -1537,7 +1563,7 @@ var prerequisites = [
}
]
var prompts = [
() => prompt.ask(`git user name : ( <= ${gitUser} )`).then((user) => { gitUser = user; })
() => 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() })
@@ -1547,7 +1573,7 @@ var prerequisites = [
}
, postinstallsteps: function(){
var prompt = cli.prompt;
var prompt = cli.prompter;
var gitUser = 'guest';
var gitEmail = 'guest@bbh.org.in';
@@ -1555,26 +1581,14 @@ var prerequisites = [
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];
})
return cli.prompt(choices, 'git user name').then(gituser => gitUser = gituser)
}
)

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 cli.prompt(choices, 'git user email').then(gitemail => gitEmail = gitemail)
}
)

@@ -1681,85 +1695,113 @@ function verifyAndInstallPrerequisites() {
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 {
// function updateselection(selected) { selectedinstance = utils.assign(selectedinstance, selected) }
var selectedinstance = null;
var chessinstances = { current_run : {} };
acquireElevationState().then(() => {
var skipprerequisites = false;
var clioverrides = { }
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] || {}

// Override sequence.
// __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts
selectedinstance = Object.assign(
selected
, chessinstances[chessinstances.current_run.instanceName][chessinstances.current_run.node_env]
, clioverrides
// , __interactve_promts -- Cant just override. Also need selectedinstance to be ready...
);
repomanifest = selectedinstance
return chessinstances
}
function acquirelocalinstances(selected){
var chessinstances = utils.assign(require(path.normalize(selected.root + '/chessinstances.js')));
return chessinstances
}

var maintask = () => {
// 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);
// selectedinstance.reposerver = repomanifest.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available...
return prerequisites.git.verifyAndInstall().then(()=>{
var e = { message : 'verifyAndInstall', success : true}
return acquireConfig(selectedinstance, chessinstances).catch((err) => {
e = err;
console.error('Chosen cofiguraton failed or not found. Fix config and rerun or chose another.')
console.error(err)
}).then(() => { return elxrworker(true) })
});
// .finally(()=>{
// fs.writeFileSync('run.log', ', ' + JSON.stringify({ error: e.message }), { 'flag': 'a+' })
// if(!e.success) fs.writeFileSync('run.done', 'error');
// // return process.exit()
// })
})
}
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)

return detectInstance().then((detectedInstance)=>{

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 = Object.assign(detectedInstance, clioverrides);
var todo = Promise.resolve(true);
if(processedArgs._[0] && selectedinstance.node_env && selectedinstance.instanceName){
// Weve been told what to do.
todo = Promise.resolve(true)
try {
chessinstances = acquirelocalinstances(selectedinstance);
initinstances(chessinstances, selectedinstance)
}
else todo = acquireChoices(selectedinstance)
catch (e) {
console.error(e)
// No local instances config found. We acquire user choices and proceed to reattempt.
initinstances(chessinstances, 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()
var prompts = [];
Object.keys(__interactve_promts).forEach(k => {
if(!selectedinstance[k]) {
prompts.push(async ()=>{
Object.defineProperty(selectedinstance, k, Object.getOwnPropertyDescriptor(__interactve_promts, k));
return await selectedinstance[k]
})
})
}
})
todo = any(prompts).then(()=>{ return selectedinstance })
if(!processedArgs._[0] || !selectedinstance.node_env || !selectedinstance.instanceName){
// Weve been told what to do.
todo = acquireChoices(selectedinstance)
}
})
}
}
todo = todo.then(() => {
try {
chessinstances = acquirelocalinstances(selectedinstance)
initinstances(chessinstances, selectedinstance)
}
catch (e) {
console.error(e)
console.log('No local instances config found in current root = ' + selectedinstance.root);
console.log('A config will be createed with the instance and environment chosen...')
// return (async ()=>{return await __default.reposerver})().then(()=>{
// // selectedinstance = Object.assign(detectedInstance, clioverrides);
// return selectedinstance = Object.assign(__default, selectedinstance);
// })
return selectedinstance
}
})
}

acquireElevationState().then(() => {
var skipprerequisites = false
if (!skipprerequisites && !__isElevated) mainTasks.push(verifyAndInstallPrerequisites);
mainTasks.push(maintask)
return any(mainTasks);
return todo
}).then(()=>{
if (!skipprerequisites && !__isElevated) mainTasks.push(verifyAndInstallPrerequisites);
mainTasks.push(maintask)
return any(mainTasks);
})
})

// detect if alread installed -> Take no action.

Laddar…
Avbryt
Spara