mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-05-05 06:37:10 -04:00
Added tree config.
This commit is contained in:
parent
8ca9621166
commit
81a9ef924f
2 changed files with 108 additions and 12 deletions
|
@ -28,6 +28,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {MapArrayType} from "../types/MapType";
|
import {MapArrayType} from "../types/MapType";
|
||||||
|
import {SettingsNode, SettingsTree} from "./SettingsTree";
|
||||||
|
|
||||||
const absolutePaths = require('./AbsolutePaths');
|
const absolutePaths = require('./AbsolutePaths');
|
||||||
const deepEqual = require('fast-deep-equal/es6');
|
const deepEqual = require('fast-deep-equal/es6');
|
||||||
|
@ -601,7 +602,7 @@ const coerceValue = (stringValue:string) => {
|
||||||
* see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
|
* see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
|
||||||
*/
|
*/
|
||||||
const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
|
const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
|
||||||
function replaceEnvs(obj: MapArrayType<any>) {
|
const replaceEnvs = (obj: MapArrayType<any>)=> {
|
||||||
for (let [key, value] of Object.entries(obj)) {
|
for (let [key, value] of Object.entries(obj)) {
|
||||||
/*
|
/*
|
||||||
* the first invocation of replacer() is with an empty key. Just go on, or
|
* the first invocation of replacer() is with an empty key. Just go on, or
|
||||||
|
@ -620,13 +621,13 @@ const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
|
||||||
* The environment variable expansion syntax "${ENV_VAR}" is just a string
|
* The environment variable expansion syntax "${ENV_VAR}" is just a string
|
||||||
* of specific form, after all.
|
* of specific form, after all.
|
||||||
*/
|
*/
|
||||||
if (typeof value !== 'string' && typeof value !== 'object') {
|
if ((typeof value !== 'string' && typeof value !== 'object') || value === null) {
|
||||||
obj[key] = value;
|
obj[key] = value;
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === "object") {
|
if (typeof obj[key] === "object") {
|
||||||
replaceEnvs(value);
|
replaceEnvs(obj[key]);
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -685,20 +686,47 @@ const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
|
||||||
|
|
||||||
obj[key] = coerceValue(envVarValue!);
|
obj[key] = coerceValue(envVarValue!);
|
||||||
}
|
}
|
||||||
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
replaceEnvs(obj);
|
replaceEnvs(obj);
|
||||||
|
|
||||||
|
const envVars:MapArrayType<any> = {}
|
||||||
|
|
||||||
// Add plugin ENV variables
|
// Add plugin ENV variables
|
||||||
for (const key of Object.keys(process.env)) {
|
|
||||||
if (key.startsWith('EP_')) {
|
/**
|
||||||
// Add to object
|
* If the key contains a double underscore, it's a plugin variable
|
||||||
obj[key] = process.env[key];
|
* E.g.
|
||||||
}
|
*/
|
||||||
|
let treeEntries = new Map<string, string|undefined>
|
||||||
|
const root = new SettingsNode("EP")
|
||||||
|
|
||||||
|
for (let [env,envVal] of Object.entries(process.env)) {
|
||||||
|
if(!env.startsWith("EP")) continue
|
||||||
|
treeEntries.set(env, envVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(obj)
|
treeEntries.forEach((value, key) => {
|
||||||
|
let pathToKey = key.split("__")
|
||||||
|
let currentNode = root
|
||||||
|
let depth = 0
|
||||||
|
depth++
|
||||||
|
currentNode.addChild(pathToKey, value!)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log("Root is", JSON.stringify(root, (k,v)=>{
|
||||||
|
if(v instanceof Map) {
|
||||||
|
return {
|
||||||
|
dataType: 'Map',
|
||||||
|
value: Array.from(v.entries()), // or with spread: value: [...value]
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
obj = Object.assign(obj, envVars)
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -892,4 +920,3 @@ exports.exportedForTestingOnly = {
|
||||||
|
|
||||||
// initially load settings
|
// initially load settings
|
||||||
exports.reloadSettings();
|
exports.reloadSettings();
|
||||||
|
|
||||||
|
|
69
src/node/utils/SettingsTree.ts
Normal file
69
src/node/utils/SettingsTree.ts
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
export class SettingsTree {
|
||||||
|
private children: Map<string, SettingsNode>;
|
||||||
|
constructor() {
|
||||||
|
this.children = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public addChild(key: string, value: string) {
|
||||||
|
this.children.set(key, new SettingsNode(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public removeChild(key: string) {
|
||||||
|
this.children.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getChild(key: string) {
|
||||||
|
return this.children.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public hasChild(key: string) {
|
||||||
|
return this.children.has(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class SettingsNode {
|
||||||
|
private readonly key: string;
|
||||||
|
private value: string|undefined;
|
||||||
|
private children: Map<string, SettingsNode>;
|
||||||
|
|
||||||
|
constructor(key: string, value?: string) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
this.children = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public addChild(key: string[], value?: string) {
|
||||||
|
let depth = 0
|
||||||
|
|
||||||
|
while (depth < key.length) {
|
||||||
|
const k = key[depth];
|
||||||
|
const slicedKey = key.slice(depth + 1)
|
||||||
|
depth++;
|
||||||
|
if(this.key === k) {
|
||||||
|
console.log("same key")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (this.children.has(k)) {
|
||||||
|
console.log("has child", k)
|
||||||
|
this.children.get(k)!.addChild(slicedKey, value);
|
||||||
|
} else {
|
||||||
|
const newNode = new SettingsNode(k);
|
||||||
|
this.children.set(k, newNode);
|
||||||
|
if(slicedKey.length > 0)
|
||||||
|
newNode.addChild(slicedKey, value);
|
||||||
|
else
|
||||||
|
newNode.value = value;
|
||||||
|
this.children.get(k)!.addChild(slicedKey, undefined);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getChild(key: string) {
|
||||||
|
return this.children.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public hasChild(key: string) {
|
||||||
|
return this.children.has(key);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue