2017-08-21 05:49:32 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-12-09 20:06:41 +00:00
|
|
|
const _ = require("lodash");
|
2017-08-21 05:49:32 +00:00
|
|
|
const colors = require("colors/safe");
|
2017-08-21 06:03:40 +00:00
|
|
|
const fs = require("fs");
|
2017-12-08 04:00:27 +00:00
|
|
|
const Helper = require("../helper");
|
2017-08-21 06:03:40 +00:00
|
|
|
const path = require("path");
|
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
let home;
|
2017-08-21 05:49:32 +00:00
|
|
|
|
|
|
|
class Utils {
|
|
|
|
static extraHelp() {
|
|
|
|
[
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
" Environment variable:",
|
|
|
|
"",
|
2017-12-08 04:00:27 +00:00
|
|
|
` THELOUNGE_HOME Path for all configuration files and folders. Defaults to ${colors.green(Helper.expandHome(Utils.defaultHome()))}.`,
|
2017-08-21 05:49:32 +00:00
|
|
|
"",
|
2017-12-09 07:11:05 +00:00
|
|
|
].forEach((e) => log.raw(e));
|
2017-08-21 05:49:32 +00:00
|
|
|
}
|
2017-08-21 06:03:40 +00:00
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
static defaultHome() {
|
|
|
|
if (home) {
|
|
|
|
return home;
|
2017-08-21 06:03:40 +00:00
|
|
|
}
|
2017-11-19 18:21:37 +00:00
|
|
|
|
2018-02-19 18:43:53 +00:00
|
|
|
const distConfig = path.resolve(path.join(
|
2017-08-21 06:03:40 +00:00
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
2018-02-19 18:43:53 +00:00
|
|
|
".thelounge_home"
|
2017-08-21 06:03:40 +00:00
|
|
|
));
|
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
home = fs.readFileSync(distConfig, "utf-8").trim();
|
2017-08-21 06:03:40 +00:00
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
return home;
|
2017-08-21 06:03:40 +00:00
|
|
|
}
|
2017-12-09 20:06:41 +00:00
|
|
|
|
|
|
|
// Parses CLI options such as `-c public=true`, `-c debug.raw=true`, etc.
|
|
|
|
static parseConfigOptions(val, memo) {
|
|
|
|
// Invalid option that is not of format `key=value`, do nothing
|
|
|
|
if (!val.includes("=")) {
|
|
|
|
return memo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parseValue = (value) => {
|
|
|
|
if (value === "true") {
|
|
|
|
return true;
|
|
|
|
} else if (value === "false") {
|
|
|
|
return false;
|
|
|
|
} else if (value === "undefined") {
|
|
|
|
return undefined;
|
|
|
|
} else if (value === "null") {
|
|
|
|
return null;
|
|
|
|
} else if (/^\[.*\]$/.test(value)) { // Arrays
|
|
|
|
// Supporting arrays `[a,b]` and `[a, b]`
|
|
|
|
const array = value.slice(1, -1).split(/,\s*/);
|
|
|
|
// If [] is given, it will be parsed as `[ "" ]`, so treat this as empty
|
|
|
|
if (array.length === 1 && array[0] === "") {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return array.map(parseValue); // Re-parses all values of the array
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// First time the option is parsed, memo is not set
|
|
|
|
if (memo === undefined) {
|
|
|
|
memo = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: If passed `-c foo="bar=42"` (with single or double quotes), `val`
|
|
|
|
// will always be passed as `foo=bar=42`, never with quotes.
|
|
|
|
const position = val.indexOf("="); // Only split on the first = found
|
|
|
|
const key = val.slice(0, position);
|
|
|
|
const value = val.slice(position + 1);
|
|
|
|
const parsedValue = parseValue(value);
|
|
|
|
|
|
|
|
if (_.has(memo, key)) {
|
|
|
|
log.warn(`Configuration key ${colors.bold(key)} was already specified, ignoring...`);
|
|
|
|
} else {
|
|
|
|
memo = _.set(memo, key, parsedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return memo;
|
|
|
|
}
|
2017-08-21 05:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Utils;
|