2019-11-17 02:27:32 +00:00
|
|
|
const path = require("path");
|
|
|
|
const bluebird = require("bluebird");
|
|
|
|
const fs = bluebird.promisifyAll(require("fs"));
|
|
|
|
|
|
|
|
const outDir = path.resolve("./dist/" || process.env.OUT_DIR);
|
|
|
|
const configPath = path.join(outDir, "config.json");
|
|
|
|
|
|
|
|
const defaultConfigPath = path.resolve(`${__dirname}/default/config.json`);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to read file from out dir,
|
|
|
|
* if not present returns default file contents
|
|
|
|
*/
|
2019-12-11 17:10:27 +00:00
|
|
|
async function getFileWithDefaults(file, defaultFile) {
|
2020-01-12 09:38:32 +00:00
|
|
|
try {
|
|
|
|
await fs.accessAsync(file, fs.constants.F_OK);
|
|
|
|
} catch (error) {
|
|
|
|
const defaultData = await fs.readFileAsync(defaultFile);
|
|
|
|
return JSON.parse(defaultData);
|
|
|
|
}
|
2020-01-11 16:48:56 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
const data = await fs.readFileAsync(file);
|
|
|
|
return JSON.parse(data);
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 17:10:27 +00:00
|
|
|
async function getConfig() {
|
2020-01-12 09:38:32 +00:00
|
|
|
return getFileWithDefaults(configPath, defaultConfigPath);
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-01-12 09:38:32 +00:00
|
|
|
outDir,
|
|
|
|
getConfig
|
2019-12-11 17:10:27 +00:00
|
|
|
};
|