cli: don't error if the user folder doesn't exist (#4508)

The user folder gets created on demand, thelounge list should not
fail if the folder doesn't exist.
This just means that no users are present, so report that instead.
This commit is contained in:
Reto 2022-04-12 02:46:29 +02:00 committed by GitHub
parent 37d7de7671
commit 815319810c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View file

@ -145,6 +145,10 @@ ClientManager.prototype.loadUser = function (name) {
}; };
ClientManager.prototype.getUsers = function () { ClientManager.prototype.getUsers = function () {
if (!fs.existsSync(Helper.getUsersPath())) {
return [];
}
return fs return fs
.readdirSync(Helper.getUsersPath()) .readdirSync(Helper.getUsersPath())
.filter((file) => file.endsWith(".json")) .filter((file) => file.endsWith(".json"))

View file

@ -3,8 +3,6 @@
const log = require("../../log"); const log = require("../../log");
const colors = require("chalk"); const colors = require("chalk");
const program = require("commander"); const program = require("commander");
const fs = require("fs");
const Helper = require("../../helper");
const Utils = require("../utils"); const Utils = require("../utils");
program program
@ -12,11 +10,6 @@ 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.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`);
return;
}
const ClientManager = require("../../clientManager"); const ClientManager = require("../../clientManager");
const users = new ClientManager().getUsers(); const users = new ClientManager().getUsers();
@ -25,16 +18,17 @@ program
return; return;
} }
if (users.length > 0) { if (users.length === 0) {
log.info("Users:");
users.forEach((user, i) => {
log.info(`${i + 1}. ${colors.bold(user)}`);
});
} else {
log.info( log.info(
`There are currently no users. Create one with ${colors.bold( `There are currently no users. Create one with ${colors.bold(
"thelounge add <name>" "thelounge add <name>"
)}.` )}.`
); );
return;
} }
log.info("Users:");
users.forEach((user, i) => {
log.info(`${i + 1}. ${colors.bold(user)}`);
});
}); });