hardlounge/server/command-line/users/add.ts

84 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

import log from "../../log";
import colors from "chalk";
import {Command} from "commander";
import fs from "fs";
import Helper from "../../helper";
import Config from "../../config";
import Utils from "../utils";
const program = new Command("add");
2014-09-13 21:29:45 +00:00
program
.description("Add a new user")
.on("--help", Utils.extraHelp)
.option("--password [password]", "new password, will be prompted if not specified")
.option("--save-logs", "if password is specified, this enables saving logs to disk")
.argument("<name>", "name of the user")
.action(function (name, cmdObj) {
if (!fs.existsSync(Config.getUsersPath())) {
log.error(`${Config.getUsersPath()} does not exist.`);
return;
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ClientManager = require("../../clientManager").default;
const manager = new ClientManager();
const users = manager.getUsers();
2019-07-17 09:33:59 +00:00
if (users === undefined) {
// There was an error, already logged
return;
}
if (users.includes(name)) {
log.error(`User ${colors.bold(name)} already exists.`);
2014-09-13 21:29:45 +00:00
return;
}
if (cmdObj.password) {
add(manager, name, cmdObj.password, !!cmdObj.saveLogs);
return;
}
2019-07-17 09:33:59 +00:00
log.prompt(
{
text: "Enter password:",
silent: true,
},
function (err, password) {
2019-07-17 09:33:59 +00:00
if (!password) {
log.error("Password cannot be empty.");
return;
}
2019-07-17 09:33:59 +00:00
if (!err) {
log.prompt(
{
text: "Save logs to disk?",
default: "yes",
},
function (err2, enableLog) {
2019-07-17 09:33:59 +00:00
if (!err2) {
add(
manager,
name,
password,
enableLog.charAt(0).toLowerCase() === "y"
);
}
}
);
}
}
2019-07-17 09:33:59 +00:00
);
2014-09-13 21:29:45 +00:00
});
2014-09-15 21:31:25 +00:00
function add(manager, name, password, enableLog) {
2018-01-11 11:33:36 +00:00
const hash = Helper.password.hash(password);
manager.addUser(name, hash, enableLog);
log.info(`User ${colors.bold(name)} created.`);
log.info(`User file located at ${colors.green(Config.getUserConfigPath(name))}.`);
2014-09-16 17:33:15 +00:00
}
export default program;