Update modifyConfigurationForPlugin.js

install-with-shinobicctv-user-instead-of-root
Moe 2020-08-28 19:34:37 -07:00
parent bc36458b86
commit c9cb08b5bf
1 changed files with 129 additions and 81 deletions

View File

@ -1,81 +1,129 @@
var fs = require('fs'); var fs = require('fs');
var jsonfile = require("jsonfile"); var execSync = require('child_process').execSync;
var execSync = require('child_process').execSync; const getConfLocation = () => {
var anError = function(message,dontShowExample){ let chosenLocation
console.log(message) try{
if(!dontShowExample){ chosenLocation = __dirname + `/../plugins/${targetedPlugin}/`
console.log('Example of usage :') fs.statSync(chosenLocation)
console.log('node tool/modifyConfigurationForPlugin.js tensorflow key=1234asdfg port=8080') }catch(err){
} chosenLocation = __dirname + `/`
} }
var testValueForObject = function(jsonString){ return chosenLocation
var newValue = jsonString + '' }
try{ const mergeDeep = function(...objects) {
newValue = JSON.parse(jsonString) const isObject = obj => obj && typeof obj === 'object';
}catch(err){
return objects.reduce((prev, obj) => {
} Object.keys(obj).forEach(key => {
if(typeof newValue === 'object'){ const pVal = prev[key];
return true const oVal = obj[key];
}
return false if (Array.isArray(pVal) && Array.isArray(oVal)) {
} prev[key] = pVal.concat(...oVal);
process.on('uncaughtException', function (err) { }
console.error('Uncaught Exception occured!'); else if (isObject(pVal) && isObject(oVal)) {
console.error(err.stack); prev[key] = mergeDeep(pVal, oVal);
}); }
var targetedPlugin = process.argv[2] else {
if(!targetedPlugin || targetedPlugin === '' || targetedPlugin.indexOf('=') > -1){ prev[key] = oVal;
return anError('Specify a plugin folder name as the first argument.') }
} });
var pluginLocation = __dirname + `/../plugins/${targetedPlugin}/`
fs.stat(pluginLocation,function(err){ return prev;
if(!err){ }, {});
var configLocation = `${pluginLocation}conf.json` }
var config = jsonfile.readFileSync(configLocation); var anError = function(message,dontShowExample){
var processArgv = process.argv.splice(3,process.argv.length) console.log(message)
var arguments = {}; if(!dontShowExample){
if(processArgv.length === 0){ console.log('Example of usage :')
return anError('No changes made. Add arguments to add or modify.') console.log('node tools/modifyConfigurationForPlugin.js tensorflow key=1234asdfg port=8080')
} }
processArgv.forEach(function(val) { }
var theSplit = val.split('='); var testValueForObject = function(jsonString){
var index = (theSplit[0] || '').trim(); var newValue = jsonString + ''
var value = theSplit[1]; try{
if(index.indexOf('addToConfig') > -1 || index == 'addToConfig'){ newValue = JSON.parse(jsonString)
try{ }catch(err){
value = JSON.parse(value)
config = Object.assign(config,value) }
}catch(err){ if(typeof newValue === 'object'){
anError('Not a valid Data set. "addToConfig" value must be a JSON string. You may need to wrap it in singles quotes.') return true
} }
}else{ return false
if(value==='DELETE'){ }
delete(config[index]) process.on('uncaughtException', function (err) {
}else{ console.error('Uncaught Exception occured!');
if(testValueForObject(value)){ console.error(err.stack);
config[index] = JSON.parse(value); });
}else{ var targetedPlugin = process.argv[2]
if(index === 'key'){ if(!targetedPlugin || targetedPlugin === '' || targetedPlugin.indexOf('=') > -1){
console.log(`Modifying main conf.json with updated key.`) return anError('Specify a plugin folder name as the first argument.')
execSync(`node ${__dirname}/modifyConfiguration.js addToConfig='{"pluginKeys":{"${config.plug}":"${value + ''}"}}'`,function(err){ }
console.log(err) var pluginLocation = getConfLocation()
}) fs.stat(pluginLocation,function(err){
config[index] = value + '' if(!err){
}else{ var configLocation = `${pluginLocation}conf.json`
config[index] = value try{
} var config = JSON.parse(fs.readFileSync(configLocation))
} }catch(err){
} try{
} var config = fs.readFileSync(`${pluginLocation}conf.sample.json`,'utf8')
console.log(index + ': ' + value); fs.writeFileSync(`${pluginLocation}conf.json`,JSON.stringify(config,null,3),'utf8')
}); }catch(err){
var config = {}
jsonfile.writeFile(configLocation,config,{spaces: 2},function(){ }
console.log('Changes Complete. Here is what it is now.') }
console.log(JSON.stringify(config,null,2)) var processArgv = process.argv.splice(3,process.argv.length)
}) var arguments = {};
}else{ if(processArgv.length === 0){
anError(`Plugin "${targetedPlugin}" not found.`) return anError('No changes made. Add arguments to add or modify.')
} }
}) processArgv.forEach(function(val) {
var theSplit = val.split('=');
var index = (theSplit[0] || '').trim();
var value = theSplit[1];
if(index.indexOf('addToConfig') > -1 || index == 'addToConfig'){
try{
value = JSON.parse(value)
config = mergeDeep(config,value)
}catch(err){
anError('Not a valid Data set. "addToConfig" value must be a JSON string. You may need to wrap it in singles quotes.')
}
}else{
if(value==='DELETE'){
delete(config[index])
}else{
if(testValueForObject(value)){
config[index] = JSON.parse(value);
}else{
if(index === 'key'){
const modifyMainFileLocation = `${__dirname}/modifyConfiguration.js`
fs.stat(modifyMainFileLocation,(err) => {
if(!err){
console.log(`Updating main conf.json with new key.`)
execSync(`node ${modifyMainFileLocation} addToConfig='{"pluginKeys":{"${config.plug}":"${value + ''}"}}'`,function(err){
console.log(err)
})
}else{
console.log(`Didn't find main conf.json. You may need to update it manually.`)
console.log(`Docker users using the official Ninja-Docker install method don't need to complete any other configuration.`)
}
})
config[index] = value + ''
}else{
config[index] = value
}
}
}
}
console.log(index + ': ' + value);
});
fs.writeFile(configLocation,JSON.stringify(config,null,3),function(){
console.log('Changes Complete. Here is what it is now.')
console.log(JSON.stringify(config,null,2))
})
}else{
anError(`Plugin "${targetedPlugin}" not found.`)
}
})