Merge pull request #1811 from thelounge/astorije/improve-helper
Clean up path helpers, expand defaults location in `thelounge --help`, add tests for `expandHome`
This commit is contained in:
commit
844ca1fbe6
@ -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));
|
||||||
};
|
};
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const colors = require("colors/safe");
|
const colors = require("colors/safe");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const Helper = require("../helper");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
let home;
|
let home;
|
||||||
@ -13,7 +14,7 @@ class Utils {
|
|||||||
"",
|
"",
|
||||||
" Environment variable:",
|
" Environment variable:",
|
||||||
"",
|
"",
|
||||||
` THELOUNGE_HOME Path for all configuration files and folders. Defaults to ${colors.green(Utils.defaultHome())}.`,
|
` THELOUNGE_HOME Path for all configuration files and folders. Defaults to ${colors.green(Helper.expandHome(Utils.defaultHome()))}.`,
|
||||||
"",
|
"",
|
||||||
].forEach((e) => console.log(e)); // eslint-disable-line no-console
|
].forEach((e) => console.log(e)); // eslint-disable-line no-console
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
@ -134,6 +157,8 @@ function ip2hex(address) {
|
|||||||
}).join("");
|
}).join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expand ~ into the current user home dir.
|
||||||
|
// This does *not* support `~other_user/tmp` => `/home/other_user/tmp`.
|
||||||
function expandHome(shortenedPath) {
|
function expandHome(shortenedPath) {
|
||||||
if (!shortenedPath) {
|
if (!shortenedPath) {
|
||||||
return "";
|
return "";
|
||||||
|
@ -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");
|
||||||
|
@ -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")
|
||||||
|
39
test/src/helperTest.js
Normal file
39
test/src/helperTest.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const expect = require("chai").expect;
|
||||||
|
const os = require("os");
|
||||||
|
const Helper = require("../../src/helper");
|
||||||
|
|
||||||
|
describe("Helper", function() {
|
||||||
|
describe("#expandHome", function() {
|
||||||
|
it("should correctly expand a Unix path", function() {
|
||||||
|
expect([`${os.homedir()}/tmp`, `${os.homedir()}\\tmp`])
|
||||||
|
.to.include(Helper.expandHome("~/tmp"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should correctly expand a Windows path", function() {
|
||||||
|
expect(Helper.expandHome("~\\tmp")).to.equal(`${os.homedir()}\\tmp`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should correctly expand when not given a specific path", function() {
|
||||||
|
expect(Helper.expandHome("~")).to.equal(os.homedir());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not expand paths not starting with tilde", function() {
|
||||||
|
expect(Helper.expandHome("/tmp")).to.match(/^\/tmp|[A-Z]:\\tmp$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not expand a tilde in the middle of a string", function() {
|
||||||
|
expect(Helper.expandHome("/tmp/~foo"))
|
||||||
|
.to.match(/^\/tmp\/~foo|[A-Z]:\\tmp\\~foo$/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return an empty string when given an empty string", function() {
|
||||||
|
expect(Helper.expandHome("")).to.equal("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return an empty string when given undefined", function() {
|
||||||
|
expect(Helper.expandHome(undefined)).to.equal("");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user