Skip to content

Commit

Permalink
deep merges config
Browse files Browse the repository at this point in the history
  • Loading branch information
evolutionleo committed Aug 15, 2022
1 parent 86f0926 commit bf2d885
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 9 deletions.
14 changes: 10 additions & 4 deletions JavascriptServer/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// get the command line arguments
import { mergeDeep } from '#util/deep_merge';
import trace from '#util/logging';
import chalk from 'chalk';
import minimist from 'minimist';
Expand Down Expand Up @@ -58,6 +59,11 @@ const prod_config = {
port: args.port || 1337,
ws_port: args.ws_port || 3000,


room: {
rooms_path: './rooms'
},

ssl_enabled: false,
ssl_cert_path: '/etc/letsencrypt/live/example.com/cert.pem',
ssl_key_path: '/etc/letsencrypt/live/example.com/privkey.pem',
Expand Down Expand Up @@ -99,16 +105,16 @@ const env = args.env || 'dev';


const config = {};
Object.assign(config, common_config);
mergeDeep(config, common_config);

if (env === 'production' || env === 'prod' || args.prod) {
Object.assign(config, prod_config);
mergeDeep(config, prod_config);
}
else if (env === 'development' || env === 'dev' || args.dev) {
Object.assign(config, dev_config);
mergeDeep(config, dev_config);
}
else {
Object.assign(config, default_config);
mergeDeep(config, default_config);
}


Expand Down
36 changes: 36 additions & 0 deletions JavascriptServer/internal/util/deep_merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copied from stack overflow lmao
// (https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge)

/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}

/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
export function mergeDeep(target, ...sources) {
if (!sources.length)
return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key])
Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
}
Binary file modified Release/GMClient.zip
Binary file not shown.
Binary file modified Release/JSServer.zip
Binary file not shown.
Binary file modified Release/TSServer.zip
Binary file not shown.
Binary file modified Release/Warp.yymps
Binary file not shown.
16 changes: 11 additions & 5 deletions TypescriptServer/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// get the command line arguments
import { mergeDeep } from '#util/deep_merge';
import trace from '#util/logging';
import chalk from 'chalk';
import minimist from 'minimist';
Expand All @@ -22,7 +23,7 @@ const common_config = {

room: {
// .yy room loading
rooms_path: '../Client/rooms',
rooms_path: '../Client/rooms', // (overriden in prod config)
warn_on_unknown_entity: true,

starting_room: 'Test Room',
Expand Down Expand Up @@ -58,6 +59,11 @@ const prod_config = {
port: args.port || 1337,
ws_port: args.ws_port || 3000,


room: {
rooms_path: './rooms'
},

ssl_enabled: false,
ssl_cert_path: '/etc/letsencrypt/live/example.com/cert.pem',
ssl_key_path: '/etc/letsencrypt/live/example.com/privkey.pem',
Expand Down Expand Up @@ -110,16 +116,16 @@ const env = args.env || 'dev'


const config:any = {};
Object.assign(config, common_config);
mergeDeep(config, common_config);

if (env === 'production' || env === 'prod' || args.prod) {
Object.assign(config, prod_config);
mergeDeep(config, prod_config);
}
else if (env === 'development' || env === 'dev' || args.dev) {
Object.assign(config, dev_config);
mergeDeep(config, dev_config);
}
else {
Object.assign(config, default_config);
mergeDeep(config, default_config);
}


Expand Down
34 changes: 34 additions & 0 deletions TypescriptServer/src/internal/util/deep_merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copied from stack overflow lmao
// (https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge)

/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}

/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
export function mergeDeep(target:object, ...sources) {
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
}

0 comments on commit bf2d885

Please sign in to comment.