Skip to content

Commit

Permalink
Refactor config out of Helper (#4558)
Browse files Browse the repository at this point in the history
* Remove config from Helper

Helper is the usual util grab bag of useful stuff.
Somehow the config ended up there historically but
structurally that doesn't make any sense.

* Add cert folder to prettier ignore file
  • Loading branch information
brunnre8 committed May 1, 2022
1 parent 38f1352 commit d4cc2dd
Show file tree
Hide file tree
Showing 44 changed files with 457 additions and 453 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
root: true

parserOptions:
ecmaVersion: 2020
ecmaVersion: 2022

env:
es6: true
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coverage/
public/
test/fixtures/.thelounge/logs/
test/fixtures/.thelounge/certificates/
test/fixtures/.thelounge/storage/

*.log
Expand Down
12 changes: 6 additions & 6 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Chan = require("./models/chan");
const crypto = require("crypto");
const Msg = require("./models/msg");
const Network = require("./models/network");
const Helper = require("./helper");
const Config = require("./config");
const UAParser = require("ua-parser-js");
const {v4: uuidv4} = require("uuid");
const escapeRegExp = require("lodash/escapeRegExp");
Expand Down Expand Up @@ -72,13 +72,13 @@ function Client(manager, name, config = {}) {
client.config.log = Boolean(client.config.log);
client.config.password = String(client.config.password);

if (!Helper.config.public && client.config.log) {
if (Helper.config.messageStorage.includes("sqlite")) {
if (!Config.values.public && client.config.log) {
if (Config.values.messageStorage.includes("sqlite")) {
client.messageProvider = new MessageStorage(client);
client.messageStorage.push(client.messageProvider);
}

if (Helper.config.messageStorage.includes("text")) {
if (Config.values.messageStorage.includes("text")) {
client.messageStorage.push(new TextFileMessageStorage(client));
}

Expand Down Expand Up @@ -236,7 +236,7 @@ Client.prototype.connect = function (args, isStartup = false) {
const network = new Network({
uuid: args.uuid,
name: String(
args.name || (Helper.config.lockNetwork ? Helper.config.defaults.name : "") || ""
args.name || (Config.values.lockNetwork ? Config.values.defaults.name : "") || ""
),
host: String(args.host || ""),
port: parseInt(args.port, 10),
Expand Down Expand Up @@ -759,7 +759,7 @@ Client.prototype.unregisterPushSubscription = function (token) {

Client.prototype.save = _.debounce(
function SaveClient() {
if (Helper.config.public) {
if (Config.values.public) {
return;
}

Expand Down
22 changes: 11 additions & 11 deletions src/clientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require("fs");
const path = require("path");
const Auth = require("./plugins/auth");
const Client = require("./client");
const Helper = require("./helper");
const Config = require("./config");
const WebPush = require("./plugins/webpush");

module.exports = ClientManager;
Expand All @@ -22,12 +22,12 @@ ClientManager.prototype.init = function (identHandler, sockets) {
this.identHandler = identHandler;
this.webPush = new WebPush();

if (!Helper.config.public) {
if (!Config.values.public) {
this.loadUsers();

// LDAP does not have user commands, and users are dynamically
// created upon logon, so we don't need to watch for new files
if (!Helper.config.ldap.enable) {
if (!Config.values.ldap.enable) {
this.autoloadUsers();
}
}
Expand Down Expand Up @@ -81,7 +81,7 @@ ClientManager.prototype.loadUsers = function () {

ClientManager.prototype.autoloadUsers = function () {
fs.watch(
Helper.getUsersPath(),
Config.getUsersPath(),
_.debounce(
() => {
const loaded = this.clients.map((c) => c.name);
Expand Down Expand Up @@ -145,12 +145,12 @@ ClientManager.prototype.loadUser = function (name) {
};

ClientManager.prototype.getUsers = function () {
if (!fs.existsSync(Helper.getUsersPath())) {
if (!fs.existsSync(Config.getUsersPath())) {
return [];
}

return fs
.readdirSync(Helper.getUsersPath())
.readdirSync(Config.getUsersPath())
.filter((file) => file.endsWith(".json"))
.map((file) => file.slice(0, -5));
};
Expand All @@ -160,7 +160,7 @@ ClientManager.prototype.addUser = function (name, password, enableLog) {
throw new Error(`${name} is an invalid username.`);
}

const userPath = Helper.getUserConfigPath(name);
const userPath = Config.getUserConfigPath(name);

if (fs.existsSync(userPath)) {
log.error(`User ${colors.green(name)} already exists.`);
Expand All @@ -182,7 +182,7 @@ ClientManager.prototype.addUser = function (name, password, enableLog) {
}

try {
const userFolderStat = fs.statSync(Helper.getUsersPath());
const userFolderStat = fs.statSync(Config.getUsersPath());
const userFileStat = fs.statSync(userPath);

if (
Expand Down Expand Up @@ -231,7 +231,7 @@ ClientManager.prototype.saveUser = function (client, callback) {
return;
}

const pathReal = Helper.getUserConfigPath(client.name);
const pathReal = Config.getUserConfigPath(client.name);
const pathTemp = pathReal + ".tmp";

try {
Expand All @@ -253,7 +253,7 @@ ClientManager.prototype.saveUser = function (client, callback) {
};

ClientManager.prototype.removeUser = function (name) {
const userPath = Helper.getUserConfigPath(name);
const userPath = Config.getUserConfigPath(name);

if (!fs.existsSync(userPath)) {
log.error(`Tried to remove non-existing user ${colors.green(name)}.`);
Expand All @@ -266,7 +266,7 @@ ClientManager.prototype.removeUser = function (name) {
};

function readUserConfig(name) {
const userPath = Helper.getUserConfigPath(name);
const userPath = Config.getUserConfigPath(name);

if (!fs.existsSync(userPath)) {
log.error(`Tried to read non-existing user ${colors.green(name)}`);
Expand Down
11 changes: 6 additions & 5 deletions src/command-line/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require("path");
const colors = require("chalk");
const program = require("commander");
const Helper = require("../helper");
const Config = require("../config");
const Utils = require("./utils");

program
Expand All @@ -20,7 +21,7 @@ program
// Parse options from `argv` returning `argv` void of these options.
const argvWithoutOptions = program.parseOptions(process.argv);

Helper.setHome(process.env.THELOUNGE_HOME || Utils.defaultHome());
Config.setHome(process.env.THELOUNGE_HOME || Utils.defaultHome());

// Check config file owner and warn if we're running under a different user
try {
Expand All @@ -34,11 +35,11 @@ try {
createPackagesFolder();

// Merge config key-values passed as CLI options into the main config
Helper.mergeConfig(Helper.config, program.opts().config);
Config.merge(program.opts().config);

require("./start");

if (!Helper.config.public) {
if (!Config.values.public) {
require("./users");
}

Expand All @@ -56,7 +57,7 @@ require("./outdated");
program.parse(argvWithoutOptions.operands.concat(argvWithoutOptions.unknown));

function createPackagesFolder() {
const packagesPath = Helper.getPackagesPath();
const packagesPath = Config.getPackagesPath();
const packagesConfig = path.join(packagesPath, "package.json");

// Create node_modules folder, otherwise yarn will start walking upwards to find one
Expand Down Expand Up @@ -95,7 +96,7 @@ function verifyFileOwner() {
);
}

const configStat = fs.statSync(path.join(Helper.getHomePath(), "config.js"));
const configStat = fs.statSync(path.join(Config.getHomePath(), "config.js"));

if (configStat && configStat.uid !== uid) {
log.warn(
Expand Down
5 changes: 3 additions & 2 deletions src/command-line/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const colors = require("chalk");
const semver = require("semver");
const program = require("commander");
const Helper = require("../helper");
const Config = require("../config");
const Utils = require("./utils");

program
Expand All @@ -17,8 +18,8 @@ program
const path = require("path");
const packageJson = require("package-json");

if (!fs.existsSync(Helper.getConfigPath())) {
log.error(`${Helper.getConfigPath()} does not exist.`);
if (!fs.existsSync(Config.getConfigPath())) {
log.error(`${Config.getConfigPath()} does not exist.`);
return;
}

Expand Down
14 changes: 7 additions & 7 deletions src/command-line/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const colors = require("chalk");
const fs = require("fs");
const path = require("path");
const program = require("commander");
const Helper = require("../helper");
const Config = require("../config");
const Utils = require("./utils");

program
Expand All @@ -21,15 +21,15 @@ program
});

function initalizeConfig() {
if (!fs.existsSync(Helper.getConfigPath())) {
fs.mkdirSync(Helper.getHomePath(), {recursive: true});
fs.chmodSync(Helper.getHomePath(), "0700");
if (!fs.existsSync(Config.getConfigPath())) {
fs.mkdirSync(Config.getHomePath(), {recursive: true});
fs.chmodSync(Config.getHomePath(), "0700");
fs.copyFileSync(
path.resolve(path.join(__dirname, "..", "..", "defaults", "config.js")),
Helper.getConfigPath()
Config.getConfigPath()
);
log.info(`Configuration file created at ${colors.green(Helper.getConfigPath())}.`);
log.info(`Configuration file created at ${colors.green(Config.getConfigPath())}.`);
}

fs.mkdirSync(Helper.getUsersPath(), {recursive: true, mode: 0o700});
fs.mkdirSync(Config.getUsersPath(), {recursive: true, mode: 0o700});
}
4 changes: 2 additions & 2 deletions src/command-line/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const log = require("../log");
const colors = require("chalk");
const program = require("commander");
const Helper = require("../helper");
const Config = require("../config");
const Utils = require("./utils");

program
Expand All @@ -14,7 +14,7 @@ program
const fs = require("fs");
const path = require("path");

const packagesConfig = path.join(Helper.getPackagesPath(), "package.json");
const packagesConfig = path.join(Config.getPackagesPath(), "package.json");
const packages = JSON.parse(fs.readFileSync(packagesConfig, "utf-8"));

if (
Expand Down
4 changes: 2 additions & 2 deletions src/command-line/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const log = require("../log");
const colors = require("chalk");
const program = require("commander");
const Helper = require("../helper");
const Config = require("../config");
const Utils = require("./utils");

program
Expand All @@ -15,7 +15,7 @@ program
const path = require("path");

// Get paths to the location of packages directory
const packagesConfig = path.join(Helper.getPackagesPath(), "package.json");
const packagesConfig = path.join(Config.getPackagesPath(), "package.json");
const packagesList = JSON.parse(fs.readFileSync(packagesConfig, "utf-8")).dependencies;
const argsList = ["upgrade", "--latest"];

Expand Down
7 changes: 4 additions & 3 deletions src/command-line/users/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const colors = require("chalk");
const program = require("commander");
const fs = require("fs");
const Helper = require("../../helper");
const Config = require("../../config");
const Utils = require("../utils");

program
Expand All @@ -14,8 +15,8 @@ program
.option("--password [password]", "new password, will be prompted if not specified")
.option("--save-logs", "if password is specified, this enables saving logs to disk")
.action(function (name, cmdObj) {
if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`);
if (!fs.existsSync(Config.getUsersPath())) {
log.error(`${Config.getUsersPath()} does not exist.`);
return;
}

Expand Down Expand Up @@ -76,5 +77,5 @@ function add(manager, name, password, enableLog) {
manager.addUser(name, hash, enableLog);

log.info(`User ${colors.bold(name)} created.`);
log.info(`User file located at ${colors.green(Helper.getUserConfigPath(name))}.`);
log.info(`User file located at ${colors.green(Config.getUserConfigPath(name))}.`);
}
12 changes: 6 additions & 6 deletions src/command-line/users/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const program = require("commander");
const child = require("child_process");
const colors = require("chalk");
const fs = require("fs");
const Helper = require("../../helper");
const Config = require("../../config");
const Utils = require("../utils");

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

Expand All @@ -33,12 +33,12 @@ program

const child_spawn = child.spawn(
process.env.EDITOR || "vi",
[Helper.getUserConfigPath(name)],
[Config.getUserConfigPath(name)],
{stdio: "inherit"}
);
child_spawn.on("error", function () {
log.error(
`Unable to open ${colors.green(Helper.getUserConfigPath(name))}. ${colors.bold(
`Unable to open ${colors.green(Config.getUserConfigPath(name))}. ${colors.bold(
"$EDITOR"
)} is not set, and ${colors.bold("vi")} was not found.`
);
Expand Down
2 changes: 1 addition & 1 deletion src/command-line/users/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

if (!require("../../helper").config.ldap.enable) {
if (!require("../../config").values.ldap.enable) {
require("./add");
require("./reset");
}
Expand Down
6 changes: 3 additions & 3 deletions src/command-line/users/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const log = require("../../log");
const colors = require("chalk");
const program = require("commander");
const fs = require("fs");
const Helper = require("../../helper");
const Config = require("../../config");
const Utils = require("../utils");

program
.command("remove <name>")
.description("Remove an existing user")
.on("--help", Utils.extraHelp)
.action(function (name) {
if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`);
if (!fs.existsSync(Config.getUsersPath())) {
log.error(`${Config.getUsersPath()} does not exist.`);
return;
}

Expand Down
Loading

0 comments on commit d4cc2dd

Please sign in to comment.