Update modifyConfigurationForPlugin.js
parent
bc36458b86
commit
c9cb08b5bf
|
@ -1,11 +1,42 @@
|
||||||
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 = () => {
|
||||||
|
let chosenLocation
|
||||||
|
try{
|
||||||
|
chosenLocation = __dirname + `/../plugins/${targetedPlugin}/`
|
||||||
|
fs.statSync(chosenLocation)
|
||||||
|
}catch(err){
|
||||||
|
chosenLocation = __dirname + `/`
|
||||||
|
}
|
||||||
|
return chosenLocation
|
||||||
|
}
|
||||||
|
const mergeDeep = function(...objects) {
|
||||||
|
const isObject = obj => obj && typeof obj === 'object';
|
||||||
|
|
||||||
|
return objects.reduce((prev, obj) => {
|
||||||
|
Object.keys(obj).forEach(key => {
|
||||||
|
const pVal = prev[key];
|
||||||
|
const oVal = obj[key];
|
||||||
|
|
||||||
|
if (Array.isArray(pVal) && Array.isArray(oVal)) {
|
||||||
|
prev[key] = pVal.concat(...oVal);
|
||||||
|
}
|
||||||
|
else if (isObject(pVal) && isObject(oVal)) {
|
||||||
|
prev[key] = mergeDeep(pVal, oVal);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
prev[key] = oVal;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
var anError = function(message,dontShowExample){
|
var anError = function(message,dontShowExample){
|
||||||
console.log(message)
|
console.log(message)
|
||||||
if(!dontShowExample){
|
if(!dontShowExample){
|
||||||
console.log('Example of usage :')
|
console.log('Example of usage :')
|
||||||
console.log('node tool/modifyConfigurationForPlugin.js tensorflow key=1234asdfg port=8080')
|
console.log('node tools/modifyConfigurationForPlugin.js tensorflow key=1234asdfg port=8080')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var testValueForObject = function(jsonString){
|
var testValueForObject = function(jsonString){
|
||||||
|
@ -28,11 +59,20 @@ var targetedPlugin = process.argv[2]
|
||||||
if(!targetedPlugin || targetedPlugin === '' || targetedPlugin.indexOf('=') > -1){
|
if(!targetedPlugin || targetedPlugin === '' || targetedPlugin.indexOf('=') > -1){
|
||||||
return anError('Specify a plugin folder name as the first argument.')
|
return anError('Specify a plugin folder name as the first argument.')
|
||||||
}
|
}
|
||||||
var pluginLocation = __dirname + `/../plugins/${targetedPlugin}/`
|
var pluginLocation = getConfLocation()
|
||||||
fs.stat(pluginLocation,function(err){
|
fs.stat(pluginLocation,function(err){
|
||||||
if(!err){
|
if(!err){
|
||||||
var configLocation = `${pluginLocation}conf.json`
|
var configLocation = `${pluginLocation}conf.json`
|
||||||
var config = jsonfile.readFileSync(configLocation);
|
try{
|
||||||
|
var config = JSON.parse(fs.readFileSync(configLocation))
|
||||||
|
}catch(err){
|
||||||
|
try{
|
||||||
|
var config = fs.readFileSync(`${pluginLocation}conf.sample.json`,'utf8')
|
||||||
|
fs.writeFileSync(`${pluginLocation}conf.json`,JSON.stringify(config,null,3),'utf8')
|
||||||
|
}catch(err){
|
||||||
|
var config = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
var processArgv = process.argv.splice(3,process.argv.length)
|
var processArgv = process.argv.splice(3,process.argv.length)
|
||||||
var arguments = {};
|
var arguments = {};
|
||||||
if(processArgv.length === 0){
|
if(processArgv.length === 0){
|
||||||
|
@ -45,7 +85,7 @@ fs.stat(pluginLocation,function(err){
|
||||||
if(index.indexOf('addToConfig') > -1 || index == 'addToConfig'){
|
if(index.indexOf('addToConfig') > -1 || index == 'addToConfig'){
|
||||||
try{
|
try{
|
||||||
value = JSON.parse(value)
|
value = JSON.parse(value)
|
||||||
config = Object.assign(config,value)
|
config = mergeDeep(config,value)
|
||||||
}catch(err){
|
}catch(err){
|
||||||
anError('Not a valid Data set. "addToConfig" value must be a JSON string. You may need to wrap it in singles quotes.')
|
anError('Not a valid Data set. "addToConfig" value must be a JSON string. You may need to wrap it in singles quotes.')
|
||||||
}
|
}
|
||||||
|
@ -57,9 +97,17 @@ fs.stat(pluginLocation,function(err){
|
||||||
config[index] = JSON.parse(value);
|
config[index] = JSON.parse(value);
|
||||||
}else{
|
}else{
|
||||||
if(index === 'key'){
|
if(index === 'key'){
|
||||||
console.log(`Modifying main conf.json with updated key.`)
|
const modifyMainFileLocation = `${__dirname}/modifyConfiguration.js`
|
||||||
execSync(`node ${__dirname}/modifyConfiguration.js addToConfig='{"pluginKeys":{"${config.plug}":"${value + ''}"}}'`,function(err){
|
fs.stat(modifyMainFileLocation,(err) => {
|
||||||
console.log(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 + ''
|
config[index] = value + ''
|
||||||
}else{
|
}else{
|
||||||
|
@ -71,7 +119,7 @@ fs.stat(pluginLocation,function(err){
|
||||||
console.log(index + ': ' + value);
|
console.log(index + ': ' + value);
|
||||||
});
|
});
|
||||||
|
|
||||||
jsonfile.writeFile(configLocation,config,{spaces: 2},function(){
|
fs.writeFile(configLocation,JSON.stringify(config,null,3),function(){
|
||||||
console.log('Changes Complete. Here is what it is now.')
|
console.log('Changes Complete. Here is what it is now.')
|
||||||
console.log(JSON.stringify(config,null,2))
|
console.log(JSON.stringify(config,null,2))
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue