From 2b08b1777f6efcb2012ad0b59892be0071069cd5 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Wed, 12 Jul 2017 12:49:38 +0800 Subject: [PATCH] config: add an env var to generate config or not 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: https://github.com/akyuujs/akyuu/pull/25 Reviewed-By: Duan Pengfei --- lib/config.js | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/config.js b/lib/config.js index 94db0a5..6a6b842 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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");