Skip to content

Commit

Permalink
Load session config from session folder (#21)
Browse files Browse the repository at this point in the history
* Using a better merge package

* Load session config from session folder

* hmm

* Try cwd

* Checking in file
  • Loading branch information
alexjpaz authored Jan 22, 2021
1 parent 29c04b0 commit 30763e3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist/
node_modules/
CurrentRoms/
config.json
./config.json
*.exe
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 3 additions & 0 deletions sessions/default/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sessionName": "default"
}
69 changes: 53 additions & 16 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = () => {
Expand All @@ -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;
};
Expand All @@ -76,4 +111,6 @@ console.log(JSON.stringify(config, null, 2));

module.exports = config;

module.exports.load


35 changes: 26 additions & 9 deletions src/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
Expand All @@ -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");
});
});

0 comments on commit 30763e3

Please sign in to comment.