Skip to content

Instantly share code, notes, and snippets.

@rike422
Created October 9, 2018 07:34
Show Gist options
  • Save rike422/48c4ac7d74a36a9f608af380bed60717 to your computer and use it in GitHub Desktop.
Save rike422/48c4ac7d74a36a9f608af380bed60717 to your computer and use it in GitHub Desktop.
const yaml = require("js-yaml");
const fs = require("fs");
const path = require("path");
const setting = {};
const CONFIG_DIR = path.join(__dirname, "../", "config");
const ENVIRONMENTS_CONFIG_DIR = path.join(CONFIG_DIR, "settings");
const LOCAL_CONFIG_YAML = path.join(CONFIG_DIR, "settings.local.yml");
const CONFIG_YAML = path.join(CONFIG_DIR, "settings.yml");
function readYAML(path) {
return yaml.safeLoad(fs.readFileSync(path, "utf8"));
}
const baseConfig = readYAML(CONFIG_YAML);
function environmentsConfig() {
const env = process.env.NODE_ENV || "development";
let file = "";
if (env == "development") {
file = "development.yml";
} else if (env == "production") {
file = "production.yml";
} else {
file = "test.yml";
}
return readYAML(path.join(ENVIRONMENTS_CONFIG_DIR, file));
}
function localConfig() {
if (fs.existsSync(LOCAL_CONFIG_YAML)) {
return readYAML(LOCAL_CONFIG_YAML);
} else {
return {};
}
}
exports.config = Object.assign(
{},
baseConfig,
environmentsConfig(),
localConfig()
);
exports.private = {
environmentsConfig,
localConfig,
readYAML
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment