Skip to content

Commit

Permalink
config: add an env var to generate config or not
Browse files Browse the repository at this point in the history
If an environment variable `AKYUU_NO_GENERATE_CONFIG` is set, Akyuu.js
won't regenerate the temporary config files.

This may avoid multi-times generation of the config files for
cluster-worker mode.

PR-URL: #25
Reviewed-By: Duan Pengfei <duanpengfei1993@qq.com>
  • Loading branch information
XadillaX committed Jul 28, 2017
1 parent 4b68cef commit c848e9b
Showing 1 changed file with 27 additions and 24 deletions.
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;

module.exports = require("config");

0 comments on commit c848e9b

Please sign in to comment.