mirror of
https://github.com/thelounge/thelounge
synced 2024-11-22 20:13:07 +00:00
Only use helpers and not shared variables around path helpers
This refactor has a few benefits, for example there cannot be a rogue update of `Helper.CONFIG_PATH` or something.
This commit is contained in:
parent
f9be519c2f
commit
0482747781
12 changed files with 72 additions and 49 deletions
|
@ -43,7 +43,7 @@ ClientManager.prototype.autoloadUsers = function() {
|
|||
|
||||
users.forEach((name) => this.loadUser(name));
|
||||
|
||||
fs.watch(Helper.USERS_PATH, _.debounce(() => {
|
||||
fs.watch(Helper.getUsersPath(), _.debounce(() => {
|
||||
const loaded = this.clients.map((c) => c.name);
|
||||
const updatedUsers = this.getUsers();
|
||||
|
||||
|
@ -96,7 +96,7 @@ ClientManager.prototype.loadUser = function(name) {
|
|||
|
||||
ClientManager.prototype.getUsers = function() {
|
||||
return fs
|
||||
.readdirSync(Helper.USERS_PATH)
|
||||
.readdirSync(Helper.getUsersPath())
|
||||
.filter((file) => file.endsWith(".json"))
|
||||
.map((file) => file.slice(0, -5));
|
||||
};
|
||||
|
|
|
@ -11,8 +11,8 @@ program
|
|||
.description("Add a new user")
|
||||
.on("--help", Utils.extraHelp)
|
||||
.action(function(name) {
|
||||
if (!fs.existsSync(Helper.USERS_PATH)) {
|
||||
log.error(`${Helper.USERS_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getUsersPath())) {
|
||||
log.error(`${Helper.getUsersPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,20 +9,20 @@ const Utils = require("./utils");
|
|||
|
||||
program
|
||||
.command("config")
|
||||
.description(`Edit configuration file located at ${colors.green(Helper.CONFIG_PATH)}.`)
|
||||
.description(`Edit configuration file located at ${colors.green(Helper.getConfigPath())}.`)
|
||||
.on("--help", Utils.extraHelp)
|
||||
.action(function() {
|
||||
if (!fs.existsSync(Helper.CONFIG_PATH)) {
|
||||
log.error(`${Helper.CONFIG_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getConfigPath())) {
|
||||
log.error(`${Helper.getConfigPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
var child_spawn = child.spawn(
|
||||
process.env.EDITOR || "vi",
|
||||
[Helper.CONFIG_PATH],
|
||||
[Helper.getConfigPath()],
|
||||
{stdio: "inherit"}
|
||||
);
|
||||
child_spawn.on("error", function() {
|
||||
log.error(`Unable to open ${colors.green(Helper.CONFIG_PATH)}. ${colors.bold("$EDITOR")} is not set, and ${colors.bold("vi")} was not found.`);
|
||||
log.error(`Unable to open ${colors.green(Helper.getConfigPath())}. ${colors.bold("$EDITOR")} is not set, and ${colors.bold("vi")} was not found.`);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -12,8 +12,8 @@ program
|
|||
.description(`Edit user file located at ${colors.green(Helper.getUserConfigPath("<name>"))}.`)
|
||||
.on("--help", Utils.extraHelp)
|
||||
.action(function(name) {
|
||||
if (!fs.existsSync(Helper.USERS_PATH)) {
|
||||
log.error(`${Helper.USERS_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getUsersPath())) {
|
||||
log.error(`${Helper.getUsersPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,8 @@ program
|
|||
const child = require("child_process");
|
||||
const packageJson = require("package-json");
|
||||
|
||||
if (!fs.existsSync(Helper.CONFIG_PATH)) {
|
||||
log.error(`${Helper.CONFIG_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getConfigPath())) {
|
||||
log.error(`${Helper.getConfigPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@ program
|
|||
.description("List all users")
|
||||
.on("--help", Utils.extraHelp)
|
||||
.action(function() {
|
||||
if (!fs.existsSync(Helper.USERS_PATH)) {
|
||||
log.error(`${Helper.USERS_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getUsersPath())) {
|
||||
log.error(`${Helper.getUsersPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@ program
|
|||
.description("Remove an existing user")
|
||||
.on("--help", Utils.extraHelp)
|
||||
.action(function(name) {
|
||||
if (!fs.existsSync(Helper.USERS_PATH)) {
|
||||
log.error(`${Helper.USERS_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getUsersPath())) {
|
||||
log.error(`${Helper.getUsersPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@ program
|
|||
.description("Reset user password")
|
||||
.on("--help", Utils.extraHelp)
|
||||
.action(function(name) {
|
||||
if (!fs.existsSync(Helper.USERS_PATH)) {
|
||||
log.error(`${Helper.USERS_PATH} does not exist.`);
|
||||
if (!fs.existsSync(Helper.getUsersPath())) {
|
||||
log.error(`${Helper.getUsersPath()} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -38,18 +38,18 @@ program
|
|||
});
|
||||
|
||||
function initalizeConfig() {
|
||||
if (!fs.existsSync(Helper.CONFIG_PATH)) {
|
||||
fsextra.ensureDirSync(Helper.HOME);
|
||||
fs.chmodSync(Helper.HOME, "0700");
|
||||
if (!fs.existsSync(Helper.getConfigPath())) {
|
||||
fsextra.ensureDirSync(Helper.getHomePath());
|
||||
fs.chmodSync(Helper.getHomePath(), "0700");
|
||||
fsextra.copySync(path.resolve(path.join(
|
||||
__dirname,
|
||||
"..",
|
||||
"..",
|
||||
"defaults",
|
||||
"config.js"
|
||||
)), Helper.CONFIG_PATH);
|
||||
log.info(`Configuration file created at ${colors.green(Helper.CONFIG_PATH)}.`);
|
||||
)), Helper.getConfigPath());
|
||||
log.info(`Configuration file created at ${colors.green(Helper.getConfigPath())}.`);
|
||||
}
|
||||
|
||||
fsextra.ensureDirSync(Helper.USERS_PATH);
|
||||
fsextra.ensureDirSync(Helper.getUsersPath());
|
||||
}
|
||||
|
|
|
@ -9,18 +9,27 @@ var net = require("net");
|
|||
var bcrypt = require("bcryptjs");
|
||||
const colors = require("colors/safe");
|
||||
|
||||
var Helper = {
|
||||
let homePath;
|
||||
let configPath;
|
||||
let usersPath;
|
||||
let storagePath;
|
||||
let packagesPath;
|
||||
|
||||
const Helper = {
|
||||
config: null,
|
||||
expandHome: expandHome,
|
||||
getPackagesPath: getPackagesPath,
|
||||
getPackageModulePath: getPackageModulePath,
|
||||
getStoragePath: getStoragePath,
|
||||
getUserConfigPath: getUserConfigPath,
|
||||
getUserLogsPath: getUserLogsPath,
|
||||
setHome: setHome,
|
||||
getVersion: getVersion,
|
||||
getGitCommit: getGitCommit,
|
||||
ip2hex: ip2hex,
|
||||
expandHome,
|
||||
getHomePath,
|
||||
getPackagesPath,
|
||||
getPackageModulePath,
|
||||
getStoragePath,
|
||||
getConfigPath,
|
||||
getUsersPath,
|
||||
getUserConfigPath,
|
||||
getUserLogsPath,
|
||||
setHome,
|
||||
getVersion,
|
||||
getGitCommit,
|
||||
ip2hex,
|
||||
|
||||
password: {
|
||||
hash: passwordHash,
|
||||
|
@ -61,14 +70,16 @@ function getGitCommit() {
|
|||
}
|
||||
}
|
||||
|
||||
function setHome(homePath) {
|
||||
this.HOME = expandHome(homePath);
|
||||
this.CONFIG_PATH = path.join(this.HOME, "config.js");
|
||||
this.USERS_PATH = path.join(this.HOME, "users");
|
||||
function setHome(newPath) {
|
||||
homePath = expandHome(newPath);
|
||||
configPath = path.join(homePath, "config.js");
|
||||
usersPath = path.join(homePath, "users");
|
||||
storagePath = path.join(homePath, "storage");
|
||||
packagesPath = path.join(homePath, "packages", "node_modules");
|
||||
|
||||
// Reload config from new home location
|
||||
if (fs.existsSync(this.CONFIG_PATH)) {
|
||||
var userConfig = require(this.CONFIG_PATH);
|
||||
if (fs.existsSync(configPath)) {
|
||||
var userConfig = require(configPath);
|
||||
this.config = _.merge(this.config, userConfig);
|
||||
}
|
||||
|
||||
|
@ -91,26 +102,38 @@ function setHome(homePath) {
|
|||
// TODO: Remove in future release
|
||||
// Backwards compatibility for old way of specifying themes in settings
|
||||
if (this.config.theme.includes(".css")) {
|
||||
log.warn(`Referring to CSS files in the ${colors.green("theme")} setting of ${colors.green(Helper.CONFIG_PATH)} is ${colors.bold("deprecated")} and will be removed in a future version.`);
|
||||
log.warn(`Referring to CSS files in the ${colors.green("theme")} setting of ${colors.green(configPath)} is ${colors.bold("deprecated")} and will be removed in a future version.`);
|
||||
} else {
|
||||
this.config.theme = `themes/${this.config.theme}.css`;
|
||||
}
|
||||
}
|
||||
|
||||
function getHomePath() {
|
||||
return homePath;
|
||||
}
|
||||
|
||||
function getConfigPath() {
|
||||
return configPath;
|
||||
}
|
||||
|
||||
function getUsersPath() {
|
||||
return usersPath;
|
||||
}
|
||||
|
||||
function getUserConfigPath(name) {
|
||||
return path.join(this.USERS_PATH, name + ".json");
|
||||
return path.join(usersPath, name + ".json");
|
||||
}
|
||||
|
||||
function getUserLogsPath(name, network) {
|
||||
return path.join(this.HOME, "logs", name, network);
|
||||
return path.join(homePath, "logs", name, network);
|
||||
}
|
||||
|
||||
function getStoragePath() {
|
||||
return path.join(this.HOME, "storage");
|
||||
return storagePath;
|
||||
}
|
||||
|
||||
function getPackagesPath() {
|
||||
return path.join(this.HOME, "packages", "node_modules");
|
||||
return packagesPath;
|
||||
}
|
||||
|
||||
function getPackageModulePath(packageName) {
|
||||
|
|
|
@ -8,7 +8,7 @@ const Helper = require("../helper");
|
|||
|
||||
class WebPush {
|
||||
constructor() {
|
||||
const vapidPath = path.join(Helper.HOME, "vapid.json");
|
||||
const vapidPath = path.join(Helper.getHomePath(), "vapid.json");
|
||||
|
||||
if (fs.existsSync(vapidPath)) {
|
||||
const data = fs.readFileSync(vapidPath, "utf-8");
|
||||
|
|
|
@ -30,7 +30,7 @@ var manager = null;
|
|||
module.exports = function() {
|
||||
log.info(`The Lounge ${colors.green(Helper.getVersion())} \
|
||||
(Node.js ${colors.green(process.versions.node)} on ${colors.green(process.platform)} ${process.arch})`);
|
||||
log.info(`Configuration file: ${colors.green(Helper.CONFIG_PATH)}`);
|
||||
log.info(`Configuration file: ${colors.green(Helper.getConfigPath())}`);
|
||||
|
||||
var app = express()
|
||||
.disable("x-powered-by")
|
||||
|
|
Loading…
Reference in a new issue