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:
Jérémie Astori 2017-12-06 22:54:54 -05:00
parent f9be519c2f
commit 0482747781
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
12 changed files with 72 additions and 49 deletions

View File

@ -43,7 +43,7 @@ ClientManager.prototype.autoloadUsers = function() {
users.forEach((name) => this.loadUser(name)); 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 loaded = this.clients.map((c) => c.name);
const updatedUsers = this.getUsers(); const updatedUsers = this.getUsers();
@ -96,7 +96,7 @@ ClientManager.prototype.loadUser = function(name) {
ClientManager.prototype.getUsers = function() { ClientManager.prototype.getUsers = function() {
return fs return fs
.readdirSync(Helper.USERS_PATH) .readdirSync(Helper.getUsersPath())
.filter((file) => file.endsWith(".json")) .filter((file) => file.endsWith(".json"))
.map((file) => file.slice(0, -5)); .map((file) => file.slice(0, -5));
}; };

View File

@ -11,8 +11,8 @@ program
.description("Add a new user") .description("Add a new user")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
if (!fs.existsSync(Helper.USERS_PATH)) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.USERS_PATH} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
} }

View File

@ -9,20 +9,20 @@ const Utils = require("./utils");
program program
.command("config") .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) .on("--help", Utils.extraHelp)
.action(function() { .action(function() {
if (!fs.existsSync(Helper.CONFIG_PATH)) { if (!fs.existsSync(Helper.getConfigPath())) {
log.error(`${Helper.CONFIG_PATH} does not exist.`); log.error(`${Helper.getConfigPath()} does not exist.`);
return; return;
} }
var child_spawn = child.spawn( var child_spawn = child.spawn(
process.env.EDITOR || "vi", process.env.EDITOR || "vi",
[Helper.CONFIG_PATH], [Helper.getConfigPath()],
{stdio: "inherit"} {stdio: "inherit"}
); );
child_spawn.on("error", function() { 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.`);
}); });
}); });

View File

@ -12,8 +12,8 @@ program
.description(`Edit user file located at ${colors.green(Helper.getUserConfigPath("<name>"))}.`) .description(`Edit user file located at ${colors.green(Helper.getUserConfigPath("<name>"))}.`)
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
if (!fs.existsSync(Helper.USERS_PATH)) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.USERS_PATH} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
} }

View File

@ -16,8 +16,8 @@ program
const child = require("child_process"); const child = require("child_process");
const packageJson = require("package-json"); const packageJson = require("package-json");
if (!fs.existsSync(Helper.CONFIG_PATH)) { if (!fs.existsSync(Helper.getConfigPath())) {
log.error(`${Helper.CONFIG_PATH} does not exist.`); log.error(`${Helper.getConfigPath()} does not exist.`);
return; return;
} }

View File

@ -11,8 +11,8 @@ program
.description("List all users") .description("List all users")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function() { .action(function() {
if (!fs.existsSync(Helper.USERS_PATH)) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.USERS_PATH} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
} }

View File

@ -11,8 +11,8 @@ program
.description("Remove an existing user") .description("Remove an existing user")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
if (!fs.existsSync(Helper.USERS_PATH)) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.USERS_PATH} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
} }

View File

@ -11,8 +11,8 @@ program
.description("Reset user password") .description("Reset user password")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
if (!fs.existsSync(Helper.USERS_PATH)) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.USERS_PATH} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
} }

View File

@ -38,18 +38,18 @@ program
}); });
function initalizeConfig() { function initalizeConfig() {
if (!fs.existsSync(Helper.CONFIG_PATH)) { if (!fs.existsSync(Helper.getConfigPath())) {
fsextra.ensureDirSync(Helper.HOME); fsextra.ensureDirSync(Helper.getHomePath());
fs.chmodSync(Helper.HOME, "0700"); fs.chmodSync(Helper.getHomePath(), "0700");
fsextra.copySync(path.resolve(path.join( fsextra.copySync(path.resolve(path.join(
__dirname, __dirname,
"..", "..",
"..", "..",
"defaults", "defaults",
"config.js" "config.js"
)), Helper.CONFIG_PATH); )), Helper.getConfigPath());
log.info(`Configuration file created at ${colors.green(Helper.CONFIG_PATH)}.`); log.info(`Configuration file created at ${colors.green(Helper.getConfigPath())}.`);
} }
fsextra.ensureDirSync(Helper.USERS_PATH); fsextra.ensureDirSync(Helper.getUsersPath());
} }

View File

@ -9,18 +9,27 @@ var net = require("net");
var bcrypt = require("bcryptjs"); var bcrypt = require("bcryptjs");
const colors = require("colors/safe"); const colors = require("colors/safe");
var Helper = { let homePath;
let configPath;
let usersPath;
let storagePath;
let packagesPath;
const Helper = {
config: null, config: null,
expandHome: expandHome, expandHome,
getPackagesPath: getPackagesPath, getHomePath,
getPackageModulePath: getPackageModulePath, getPackagesPath,
getStoragePath: getStoragePath, getPackageModulePath,
getUserConfigPath: getUserConfigPath, getStoragePath,
getUserLogsPath: getUserLogsPath, getConfigPath,
setHome: setHome, getUsersPath,
getVersion: getVersion, getUserConfigPath,
getGitCommit: getGitCommit, getUserLogsPath,
ip2hex: ip2hex, setHome,
getVersion,
getGitCommit,
ip2hex,
password: { password: {
hash: passwordHash, hash: passwordHash,
@ -61,14 +70,16 @@ function getGitCommit() {
} }
} }
function setHome(homePath) { function setHome(newPath) {
this.HOME = expandHome(homePath); homePath = expandHome(newPath);
this.CONFIG_PATH = path.join(this.HOME, "config.js"); configPath = path.join(homePath, "config.js");
this.USERS_PATH = path.join(this.HOME, "users"); usersPath = path.join(homePath, "users");
storagePath = path.join(homePath, "storage");
packagesPath = path.join(homePath, "packages", "node_modules");
// Reload config from new home location // Reload config from new home location
if (fs.existsSync(this.CONFIG_PATH)) { if (fs.existsSync(configPath)) {
var userConfig = require(this.CONFIG_PATH); var userConfig = require(configPath);
this.config = _.merge(this.config, userConfig); this.config = _.merge(this.config, userConfig);
} }
@ -91,26 +102,38 @@ function setHome(homePath) {
// TODO: Remove in future release // TODO: Remove in future release
// Backwards compatibility for old way of specifying themes in settings // Backwards compatibility for old way of specifying themes in settings
if (this.config.theme.includes(".css")) { 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 { } else {
this.config.theme = `themes/${this.config.theme}.css`; this.config.theme = `themes/${this.config.theme}.css`;
} }
} }
function getHomePath() {
return homePath;
}
function getConfigPath() {
return configPath;
}
function getUsersPath() {
return usersPath;
}
function getUserConfigPath(name) { function getUserConfigPath(name) {
return path.join(this.USERS_PATH, name + ".json"); return path.join(usersPath, name + ".json");
} }
function getUserLogsPath(name, network) { function getUserLogsPath(name, network) {
return path.join(this.HOME, "logs", name, network); return path.join(homePath, "logs", name, network);
} }
function getStoragePath() { function getStoragePath() {
return path.join(this.HOME, "storage"); return storagePath;
} }
function getPackagesPath() { function getPackagesPath() {
return path.join(this.HOME, "packages", "node_modules"); return packagesPath;
} }
function getPackageModulePath(packageName) { function getPackageModulePath(packageName) {

View File

@ -8,7 +8,7 @@ const Helper = require("../helper");
class WebPush { class WebPush {
constructor() { constructor() {
const vapidPath = path.join(Helper.HOME, "vapid.json"); const vapidPath = path.join(Helper.getHomePath(), "vapid.json");
if (fs.existsSync(vapidPath)) { if (fs.existsSync(vapidPath)) {
const data = fs.readFileSync(vapidPath, "utf-8"); const data = fs.readFileSync(vapidPath, "utf-8");

View File

@ -30,7 +30,7 @@ var manager = null;
module.exports = function() { module.exports = function() {
log.info(`The Lounge ${colors.green(Helper.getVersion())} \ log.info(`The Lounge ${colors.green(Helper.getVersion())} \
(Node.js ${colors.green(process.versions.node)} on ${colors.green(process.platform)} ${process.arch})`); (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() var app = express()
.disable("x-powered-by") .disable("x-powered-by")