2016-06-08 11:26:24 +02:00
|
|
|
var _ = require("lodash");
|
2014-10-05 01:22:23 +02:00
|
|
|
var path = require("path");
|
2016-04-27 10:13:25 +02:00
|
|
|
var os = require("os");
|
2016-07-04 22:15:30 +02:00
|
|
|
var fs = require("fs");
|
2014-10-05 01:22:23 +02:00
|
|
|
|
2016-05-09 18:19:16 +02:00
|
|
|
var Helper = {
|
2016-06-08 11:26:24 +02:00
|
|
|
config: null,
|
2016-04-27 10:13:25 +02:00
|
|
|
expandHome: expandHome,
|
2016-05-08 08:21:31 +02:00
|
|
|
getUserConfigPath: getUserConfigPath,
|
2016-05-15 23:13:51 +02:00
|
|
|
getUserLogsPath: getUserLogsPath,
|
2016-05-09 18:19:16 +02:00
|
|
|
setHome: setHome,
|
2014-09-13 14:23:17 +02:00
|
|
|
};
|
|
|
|
|
2016-05-09 18:19:16 +02:00
|
|
|
module.exports = Helper;
|
|
|
|
|
2016-06-08 11:26:24 +02:00
|
|
|
Helper.config = require(path.resolve(path.join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"defaults",
|
|
|
|
"config.js"
|
|
|
|
)));
|
|
|
|
|
2016-05-09 18:19:16 +02: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");
|
2016-06-08 11:26:24 +02:00
|
|
|
|
|
|
|
// Reload config from new home location
|
2016-07-04 22:15:30 +02:00
|
|
|
if (fs.existsSync(this.CONFIG_PATH)) {
|
|
|
|
var userConfig = require(this.CONFIG_PATH);
|
|
|
|
this.config = _.extend(this.config, userConfig);
|
|
|
|
}
|
2016-05-09 18:19:16 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 08:21:31 +02:00
|
|
|
function getUserConfigPath(name) {
|
|
|
|
return path.join(this.USERS_PATH, name + ".json");
|
|
|
|
}
|
|
|
|
|
2016-05-15 23:13:51 +02:00
|
|
|
function getUserLogsPath(name, network) {
|
|
|
|
return path.join(this.HOME, "logs", name, network);
|
|
|
|
}
|
|
|
|
|
2016-05-08 08:21:31 +02:00
|
|
|
function expandHome(shortenedPath) {
|
2016-04-27 10:13:25 +02:00
|
|
|
var home;
|
|
|
|
|
|
|
|
if (os.homedir) {
|
|
|
|
home = os.homedir();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!home) {
|
|
|
|
home = process.env.HOME || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
home = home.replace("$", "$$$$");
|
|
|
|
|
2016-05-08 08:21:31 +02:00
|
|
|
return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
|
2016-04-27 10:13:25 +02:00
|
|
|
}
|