Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add an env var to generate config or not #25

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,46 @@ const path = require("path");

const _ = require("lodash");

const configDir = process.env.NODE_CONFIG_DIR || path.join(require.main.filename, "..", "/config");
const currentDir = path.join(__dirname, "config");
process.env.NODE_CONFIG_DIR = currentDir;

let dirs;
try {
dirs = fs.readdirSync(configDir);
} catch(e) {
// ...
}
dirs = dirs || [];

const template = fs.readFileSync(`${__dirname}/config/_template`, { encoding: "utf8" });
const currentDir = path.join(__dirname, "config");

function copy(_path, type) {
const js = _.template(template)({ path: _path, type: type });
fs.writeFileSync(`${__dirname}/config/${type}.js`, js, { encoding: "utf8" });
}

for(let i = 0; i < dirs.length; i++) {
let stat;
if(process.env.AKYUU_NO_GENERATE_CONFIG !== "true") {
const configDir = process.env.NODE_CONFIG_DIR || path.join(require.main.filename, "..", "/config");

let dirs;
try {
stat = fs.statSync(path.join(configDir, dirs[i]));
dirs = fs.readdirSync(configDir);
} catch(e) {
stat = null;
// ...
}
dirs = dirs || [];

for(let i = 0; i < dirs.length; i++) {
let stat;
try {
stat = fs.statSync(path.join(configDir, dirs[i]));
} catch(e) {
stat = null;
}

if(!stat) continue;
if(stat.isDirectory()) {
copy(configDir, dirs[i]);
} else if(stat.isFile()) {
if(dirs[i].endsWith(".js")) {
copy(configDir, dirs[i].substr(dirs[i].length - 3));
} else if(dirs[i].endsWith(".json")) {
copy(configDir, dirs[i].substr(dirs[i].length - 5));
if(!stat) continue;
if(stat.isDirectory()) {
copy(configDir, dirs[i]);
} else if(stat.isFile()) {
if(dirs[i].endsWith(".js")) {
copy(configDir, dirs[i].substr(dirs[i].length - 3));
} else if(dirs[i].endsWith(".json")) {
copy(configDir, dirs[i].substr(dirs[i].length - 5));
}
}
}
}

process.env.NODE_CONFIG_DIR = currentDir;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of process.env.NODE_CONFIG_DIR = currentDir;?

Copy link
Contributor Author

@XadillaX XadillaX Jul 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node-config reads configuration files in the ./config directory for the running process, typically the application root. This can be overridden by setting the $NODE_CONFIG_DIR environment variable to the directory containing your configuration files.

$NODE_CONFIG_DIR can be a full path from your root directory, or a relative path from the process if the value begins with ./ or ../.

Refs: https://github.com/lorenwest/node-config/wiki/Configuration-Files#config-directory

We uses config as our configuration base package.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌


module.exports = require("config");