From 30763e3fd94e4ff2058381cd3a9dff588f4afd35 Mon Sep 17 00:00:00 2001 From: Alexander Paz Date: Thu, 21 Jan 2021 19:54:55 -0500 Subject: [PATCH] Load session config from session folder (#21) * Using a better merge package * Load session config from session folder * hmm * Try cwd * Checking in file --- .gitignore | 2 +- package-lock.json | 5 +++ package.json | 1 + sessions/default/config.json | 3 ++ src/config.js | 69 +++++++++++++++++++++++++++--------- src/config.spec.js | 35 +++++++++++++----- 6 files changed, 89 insertions(+), 26 deletions(-) create mode 100644 sessions/default/config.json diff --git a/.gitignore b/.gitignore index 4960c4f..6c60768 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ dist/ node_modules/ CurrentRoms/ -config.json +./config.json *.exe diff --git a/package-lock.json b/package-lock.json index 67caea3..bac047a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -888,6 +888,11 @@ "p-locate": "^5.0.0" } }, + "lodash": { + "version": "4.17.20", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" + }, "lodash.get": { "version": "4.4.2", "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", diff --git a/package.json b/package.json index faa548b..9be999b 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "dependencies": { "comfy.js": "^1.1.6", + "lodash": "^4.17.20", "open": "^7.3.0", "pkg": "^4.4.9" }, diff --git a/sessions/default/config.json b/sessions/default/config.json new file mode 100644 index 0000000..8eb7b47 --- /dev/null +++ b/sessions/default/config.json @@ -0,0 +1,3 @@ +{ + "sessionName": "default" +} diff --git a/src/config.js b/src/config.js index b00d7f8..afd2dad 100644 --- a/src/config.js +++ b/src/config.js @@ -1,7 +1,25 @@ const logger = console; +const merge = require('lodash/merge'); + let config = null; +const path = require('path'); +const fs = require('fs'); + +const loadConfigFromFile = (configPath) => { + let newConfig = {}; + + try { + newConfig = JSON.parse(fs.readFileSync(configPath).toString()); + } catch(e) { + logger.warn("Failed to load config from file", e.message); + } + + return newConfig; + +}; + const getEnvironmentConfig = () => { let environmentConfig = { port: process.env.PORT, @@ -16,15 +34,7 @@ const getEnvironmentConfig = () => { }; const getUserConfig = () => { - let userConfig = {}; - - try { - userConfig = require(process.cwd()+'/config.json'); - } catch(e) { - // - } - - return userConfig; + return loadConfigFromFile(path.join(process.cwd(), "config.json")); }; const getDefaultConfig = () => { @@ -49,23 +59,48 @@ const getDefaultConfig = () => { return defaults; }; +const getSessionConfig = ({ session }) => { + if(!session) { + return {}; + } + + const configPath = path.join(process.cwd(), 'sessions', session, 'config.json'); + return loadConfigFromFile(configPath); +}; + +function filterObject(obj) { + const ret = {}; + Object.keys(obj) + .filter((key) => obj[key] !== undefined) + .forEach((key) => ret[key] = obj[key]); + return ret; +} + const resetConfig = ( - defaultConfig = getDefaultConfig(), - environmentConfig = getEnvironmentConfig(), - userConfig = getUserConfig(), + defaultConfigProvider = () => getDefaultConfig(), + environmentConfigProvider = () => getEnvironmentConfig(), + userConfigProvider = () => getUserConfig(), + sessionConfigProvider = (s) => getSessionConfig(s), ) => { config = {}; - Object.assign(config, - defaultConfig, - environmentConfig, - userConfig, + merge( + config, + defaultConfigProvider(), + environmentConfigProvider(), + userConfigProvider(), + ); + + merge( + config, + getSessionConfig(config), ); config.resetConfig = resetConfig; config.getDefaultConfig = getDefaultConfig; config.getUserConfig = getUserConfig; config.getEnvironmentConfig = getEnvironmentConfig; + config.getSessionConfig = getSessionConfig; return config; }; @@ -76,4 +111,6 @@ console.log(JSON.stringify(config, null, 2)); module.exports = config; +module.exports.load + diff --git a/src/config.spec.js b/src/config.spec.js index fba4ce3..3ea0da0 100644 --- a/src/config.spec.js +++ b/src/config.spec.js @@ -28,17 +28,23 @@ describe('config', () => { c: 1, }; + let sessionConfig = { + d: 1, + }; + expect(config).to.be.ok; let newConfig = config.resetConfig( - defaultConfig, - environmentConfig, - userConfig, + () => defaultConfig, + () => environmentConfig, + () => userConfig, + () => sessionConfig, ); expect(newConfig.a).to.eql(1); expect(newConfig.b).to.eql(1); expect(newConfig.c).to.eql(1); + expect(newConfig.a).to.eql(1); }); it('should order the config correctly', () => { @@ -59,9 +65,9 @@ describe('config', () => { expect(config).to.be.ok; let newConfig = config.resetConfig( - defaultConfig, - environmentConfig, - userConfig, + () => defaultConfig, + () => environmentConfig, + () => userConfig, ); expect(newConfig.a).to.eql(1); @@ -84,13 +90,24 @@ describe('config', () => { expect(config).to.be.ok; let newConfig = config.resetConfig( - defaultConfig, - environmentConfig, - userConfig, + () => defaultConfig, + () => environmentConfig, + () => userConfig, ); expect(newConfig.a).to.eql(undefined); expect(newConfig.b).to.eql(2); expect(newConfig.c).to.eql(3); }); + + it('should load a session config from the folder', () => { + let c = config.resetConfig( + () => ({ session: "default" }), + () => ({ session: "default" }), + () => ({ session: "default" }), + ); + + expect(c.session).to.eql("default"); + expect(c.sessionName).to.eql("default"); + }); });