hardlounge/src/helper.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

var _ = require("lodash");
2014-10-04 23:22:23 +00:00
var path = require("path");
var os = require("os");
2014-10-04 23:22:23 +00:00
2016-05-09 16:19:16 +00:00
var Helper = {
config: null,
expandHome: expandHome,
getUserConfigPath: getUserConfigPath,
2016-05-15 21:13:51 +00:00
getUserLogsPath: getUserLogsPath,
2016-05-09 16:19:16 +00:00
setHome: setHome,
};
2016-05-09 16:19:16 +00:00
module.exports = Helper;
Helper.config = require(path.resolve(path.join(
__dirname,
"..",
"defaults",
"config.js"
)));
2016-05-09 16:19:16 +00:00
function setHome(homePath) {
this.HOME = expandHome(homePath || "~/.lounge");
this.CONFIG_PATH = path.join(this.HOME, "config.js");
this.USERS_PATH = path.join(this.HOME, "users");
// Reload config from new home location
var userConfig = require(this.CONFIG_PATH);
this.config = _.extend(this.config, userConfig);
2016-05-09 16:19:16 +00:00
}
function getUserConfigPath(name) {
return path.join(this.USERS_PATH, name + ".json");
}
2016-05-15 21:13:51 +00:00
function getUserLogsPath(name, network) {
return path.join(this.HOME, "logs", name, network);
}
function expandHome(shortenedPath) {
var home;
if (os.homedir) {
home = os.homedir();
}
if (!home) {
home = process.env.HOME || "";
}
home = home.replace("$", "$$$$");
return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
}