Browse Source

updates for prompts

master
chess 3 years ago
parent
commit
ee769f2e49
1 changed files with 115 additions and 101 deletions
  1. 115
    101
      index.js

+ 115
- 101
index.js View File

function acquireChoices(selectedinstance) { function acquireChoices(selectedinstance) {


var hasconfig = false; var hasconfig = false;
console.warn(chalk.yellow(`
console.warn(chalk.cyan(`
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Warning : Cannot locate your preferred configuration since it was not specified Warning : Cannot locate your preferred configuration since it was not specified
You should fork the default chess configuration to customize and make it You should fork the default chess configuration to customize and make it
} }
else processedArgs._[0] = cmd else processedArgs._[0] = cmd
return p1.ask(`Enter preferred repo server ( <= ${selectedinstance.reposerver || selectedinstance.reposervers[0]} ) : `).then(function (reposerver) { return p1.ask(`Enter preferred repo server ( <= ${selectedinstance.reposerver || selectedinstance.reposervers[0]} ) : `).then(function (reposerver) {
selectedinstance.reposerver = reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
selectedinstance.reposerver = reposerver || selectedinstance.reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
}) })
}) })
}) })


} }


var __interactve_promts = function( target ){
var getBoundEachPrompt = function(target, mustPrompt, promptables, choices) {
return function(prompts, k, i, a){
// No local instances config found. We use a default initialized instance available in selectedinstance
// Confirm those that were not supplied as user choices in runtime args and proceed to reattempt.
// PB : TODO -- selectedinstance === __default check to prompt everything...
if( (mustPrompt[k] !== undefined && mustPrompt[k] !== null) && target[k] !== mustPrompt[k]
|| (mustPrompt[k] === undefined || mustPrompt[k] === null) && (target[k] === undefined || target[k] === null)
|| reconfirm[k]) {
prompts.push(async ()=>{
// PB : NOTE -- Important in async cases when this needs to be in the same state as when it was invoked.
// We take a snapshot... Shallow.. !! If required deep should be used based on use case.
// If latest altered state is required we can reerence this directly.
// var asyncthis = Object.assign(this);
promptables[k].choices = choices[k]
Object.defineProperty(target, k, getPromptableAsyncPropDescriptor(k, promptables[k]));
return await target[k]
})
}
delete mustPrompt[k]
return prompts
}
}


return {
var __interactve_promts = function( target, choices ){

Object.defineProperty(target, 'node_env', { get : function(){ return this.instanceType } });

var interactionpoints = {
runchoice : { runchoice : {
label : label :
`Choose an option : `Choose an option :
, instanceType : { label : `Enter Instance Type ( <= ${target.instanceType || 'development'} ) : `, choices : [], defaultchoice : 'development'} , instanceType : { label : `Enter Instance Type ( <= ${target.instanceType || 'development'} ) : `, choices : [], defaultchoice : 'development'}
, reposerver : { label : `Enter Instance Name ( <= ${target.reposerver || 'https://git.bbh.org.in'} ) : `, choices : [], defaultchoice : 'https://git.bbh.org.in'} , reposerver : { label : `Enter Instance Name ( <= ${target.reposerver || 'https://git.bbh.org.in'} ) : `, choices : [], defaultchoice : 'https://git.bbh.org.in'}
} }

var eachPrompt = getBoundEachPrompt( target, promptkeys, interactionpoints, choices)
return Object.keys(interactionpoints).reduce(eachPrompt, [])
} }




return { return {
get (){ get (){
return cli.prompt( promptable.choices, promptable.label, promptable.defaultchoice ).then(propValue => { return cli.prompt( promptable.choices, promptable.label, promptable.defaultchoice ).then(propValue => {
var asyncprop = Promise.resolve(propValue)
if(promptable.interpret){ if(promptable.interpret){
return propName = promptable.interpret(propValue).then(
()=>{
Object.defineProperty(this, propName, {
value: propValue,
writable: true,
configurable : true,
enumerable : true
});
return propValue
}
)
asyncprop = promptable.interpret(propValue)
} }
else return propValue

return asyncprop.then(
()=>{
Object.defineProperty(this, propName, {
value: propValue,
writable: true,
configurable : true,
enumerable : true
});
return propValue
}
)
}) })
} }
// , set (propValue){ // , set (propValue){


acquireElevationState().then(() => { acquireElevationState().then(() => {
var skipprerequisites = false; var skipprerequisites = false;
function initinstances(selected) {
// PB : TODO -- processedArgs should already be part of selected.
var instanceName = processedArgs._[1]
function initinstances(selected_overrides) {
var root = selected_overrides.root // We should always have this coz we are running at some place which is fixed by detectinstances.

var instanceName = selected_overrides.instanceName
|| clioverrides.instanceName
// || processedArgs._[1]
|| chessinstances.current_run.instanceName || chessinstances.current_run.instanceName
|| selected.instanceName
|| clioverrides.instanceName;
var node_env = processedArgs.node_env
|| chessinstances.current_run.node_env
|| selected.node_env
|| clioverrides.node_env;
var node_env = selected_overrides.node_env
|| clioverrides.node_env
// || processedArgs.node_env
|| chessinstances.current_run.node_env

var reposerver = selected_overrides.reposerver
|| clioverrides.reposerver
// || processedArgs.node_env
|| chessinstances.current_run.reposerver
if(!instanceName) { if(!instanceName) {
promptkeys['instanceName'] = instanceName = chessinstances.current_run.instanceName = promptkeys['instanceName'] || __default.instanceName; promptkeys['instanceName'] = instanceName = chessinstances.current_run.instanceName = promptkeys['instanceName'] || __default.instanceName;
promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env; promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env;
promptkeys['reposerver'] = promptkeys['reposerver'] || __default.reposervers[0];
promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0];
} }
if(!node_env) { if(!node_env) {
promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env; promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env;
promptkeys['reposerver'] = promptkeys['reposerver'] || __default.reposervers[0];
promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0];
}

if(!reposerver) {
promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0];
} }


chessinstances[instanceName] = chessinstances[instanceName] || createChessInsance( { chessinstances[instanceName] = chessinstances[instanceName] || createChessInsance( {
instanceName, node_env, root : selected.root, reposerver : promptkeys['reposerver'] } );
chessinstances['current_run'] = { instanceName: instanceName, node_env: node_env }
instanceName, node_env, root : selected_overrides.root, reposerver : promptkeys['reposerver'] } );
chessinstances['current_run'] = { instanceName: instanceName, node_env, reposerver, root }


if(path.normalize(selected.root) !== path.normalize(chessinstances[instanceName][node_env].root)) {
throw "Mismatched chessinstances config found " + chessinstances[instanceName][node_env].root + ' does not match ' + selected.root
if(path.normalize(selected_overrides.root) !== path.normalize(chessinstances[instanceName][node_env].root)) {
throw "instanceName and instanceType specified doesn't match whats already present do you want to continue " + chessinstances[instanceName][node_env].root + ' does not match ' + selected_overrides.root
} }
// Override sequence. // Override sequence.
// __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts // __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts
selectedinstance = utils.assign( selectedinstance = utils.assign(
selected
, chessinstances[instanceName][node_env]
chessinstances[instanceName][node_env]
, clioverrides , clioverrides
, selected_overrides
// , __interactve_promts -- Cant just override. Also need selectedinstance to be ready... // , __interactve_promts -- Cant just override. Also need selectedinstance to be ready...
); );


// Config from server always override merges into selection except for the current selection. // Config from server always override merges into selection except for the current selection.
// PB : TODO -- utils.assign Array merges are non-distinct... // PB : TODO -- utils.assign Array merges are non-distinct...
chessinstances[instanceName][node_env] = selectedinstance = utils.assign(selected, selectedinstance)
chessinstances[instanceName][node_env] = selectedinstance;
// chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance; // chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance;
cacheWriteInstanceConfig(chessinstances) cacheWriteInstanceConfig(chessinstances)
// PB : TODO -- We should probably write the new server config also... // PB : TODO -- We should probably write the new server config also...
return elxrworker() return elxrworker()
} }


return detectinstances().then((detectedinstanceoptions)=>{

var getchoices = function(){
detectedinstanceoptions.splice(0,0, __default)

var instances = []
var reposervers = [];
var instnaceNames = []
var instanceTypes = ['development', 'production'];
Object.keys( chessinstances).forEach(instanceName => {
if(instanceName === 'current_run') return;
Object.keys( chessinstances[instanceName] ).forEach(node_env=>{
var instance = chessinstances[instanceName][node_env];
reposervers = reposervers.concat(instance.reposervers)
if(instance.reposerver) reposervers.push(instance.reposerver)
instances.push(instance)
instanceTypes.push(instance.node_env)
instnaceNames.push(instance.instanceName)
})
var getchoices = function(detectedinstanceoptions, promptkeys){

var instances = []
var reposervers = [];
var instnaceNames = []
var instanceTypes = ['development', 'production'];
Object.keys( chessinstances).forEach(instanceName => {
if(instanceName === 'current_run') return;
Object.keys( chessinstances[instanceName] ).forEach(node_env=>{
var instance = chessinstances[instanceName][node_env];
reposervers = reposervers.concat(instance.reposervers)
if(instance.reposerver) reposervers.push(instance.reposerver)
instances.push(instance)
instanceTypes.push(instance.node_env)
instnaceNames.push(instance.instanceName)
}) })
instances = instances.concat(detectedinstanceoptions)

if(promptkeys['instanceName']) instnaceNames.push(selectedinstance['instanceName'])
if(promptkeys['instanceName']) instnaceNames.push(promptkeys['instanceName'])
if(selectedinstance['reposervers']) reposervers = reposervers.concat(selectedinstance['reposervers'])
var choices = {
'instanceName' : Array.from( new Set(instnaceNames) )
, 'reposerver' : Array.from( new Set(reposervers) )
, 'instanceType' : Array.from( new Set(instanceTypes) )
}
})
instances = instances.concat(detectedinstanceoptions)


return choices;
if(promptkeys['instanceName']) instnaceNames.push(selectedinstance['instanceName'])
if(promptkeys['instanceName']) instnaceNames.push(promptkeys['instanceName'])
if(selectedinstance['reposervers']) reposervers = reposervers.concat(selectedinstance['reposervers'])
var choices = {
'instanceName' : Array.from( new Set(instnaceNames) )
, 'reposerver' : Array.from( new Set(reposervers) )
, 'instanceType' : Array.from( new Set(instanceTypes) )
} }


return choices;
}

return detectinstances().then((detectedinstanceoptions)=>{
detectedinstanceoptions.splice(0,0, __default)

// 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( ...detectedinstanceoptions.slice(-2) ) selectedinstance = utils.assign( ...detectedinstanceoptions.slice(-2) )
promptkeys = utils.assign(promptkeys, clioverrides) promptkeys = utils.assign(promptkeys, clioverrides)
var reconfirm = { 'instanceName' : selectedinstance['instanceName'] === 'chess' } var reconfirm = { 'instanceName' : selectedinstance['instanceName'] === 'chess' }
} }
else { var reconfirm = {}; } else { var reconfirm = {}; }
var prompts = [];

var eachPrompt = function(k, i, a){
// No local instances config found. We use a default initialized instance available in selectedinstance
// Confirm those that were not supplied as user choices in runtime args and proceed to reattempt.
// PB : TODO -- selectedinstance === __default check to prompt everything...
if( (promptkeys[k] !== undefined && promptkeys[k] !== null) && selectedinstance[k] !== promptkeys[k]
|| (promptkeys[k] === undefined || promptkeys[k] === null) && (selectedinstance[k] === undefined || selectedinstance[k] === null)
|| reconfirm[k]) {
prompts.push(async ()=>{
// PB : NOTE -- Important in async cases when this needs to be in the same state as when it was invoked.
// We take a snapshot... Shallow.. !! If required deep should be used based on use case.
// If latest altered state is required we can reerence this directly.
// var asyncthis = Object.assign(this);
promptables[k].choices = choices[k]
Object.defineProperty(selectedinstance, k, getPromptableAsyncPropDescriptor(k, promptables[k]));
return await selectedinstance[k]
})
}
delete promptkeys[k]
}
try { try {


chessinstances = acquirelocalinstances(selectedinstance); chessinstances = acquirelocalinstances(selectedinstance);
findlocalinstances(chessinstances, detectedinstanceoptions) findlocalinstances(chessinstances, detectedinstanceoptions)
var choices = getchoices()

var promptables = __interactve_promts(selectedinstance)
Object.keys(promptables).forEach(eachPrompt, selectedinstance)
var todo = Promise.resolve(true);

todo = any(prompts).then(()=>{
var choices = getchoices(detectedinstanceoptions, promptkeys)
var todo = any( __interactve_promts(selectedinstance, choices) ).then(()=>{
return initinstances(selectedinstance) return initinstances(selectedinstance)
}) })
} }
catch (e) { catch (e) {
// PB : TODO -- verbose mode warning.. console.warn(e) // Missing chessinstances is not an error... // PB : TODO -- verbose mode warning.. console.warn(e) // Missing chessinstances is not an error...
var choices = getchoices()
var choices = getchoices(detectedinstanceoptions, promptkeys)
var todo = any( __interactve_promts(selectedinstance, choices) ).then(()=>{
return initinstances(selectedinstance)
})


var promptables = __interactve_promts(selectedinstance)
Object.keys(promptables).forEach(eachPrompt, selectedinstance)
todo = any(prompts).then(()=>{ return initinstances(selectedinstance) })
if(!processedArgs._[0] || !selectedinstance.node_env || !selectedinstance.instanceName){ if(!processedArgs._[0] || !selectedinstance.node_env || !selectedinstance.instanceName){
// Weve not been told what to do. // Weve not been told what to do.
todo = todo.then(() => { return acquireChoices(selectedinstance) }) todo = todo.then(() => { return acquireChoices(selectedinstance) })

Loading…
Cancel
Save