2022-06-19 00:25:21 +00:00
|
|
|
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";
|
2014-09-13 17:51:10 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const program = new Command("add");
|
2014-09-13 21:29:45 +00:00
|
|
|
program
|
|
|
|
.description("Add a new user")
|
2017-08-21 05:49:32 +00:00
|
|
|
.on("--help", Utils.extraHelp)
|
2020-10-04 15:03:26 +00:00
|
|
|
.option("--password [password]", "new password, will be prompted if not specified")
|
|
|
|
.option("--save-logs", "if password is specified, this enables saving logs to disk")
|
2022-06-19 00:25:21 +00:00
|
|
|
.argument("<name>", "name of the user")
|
2020-10-04 15:03:26 +00:00
|
|
|
.action(function (name, cmdObj) {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!fs.existsSync(Config.getUsersPath())) {
|
|
|
|
log.error(`${Config.getUsersPath()} does not exist.`);
|
2017-07-18 09:36:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2022-06-21 07:51:24 +00:00
|
|
|
const ClientManager = require("../../clientManager").default;
|
2017-08-23 05:15:01 +00:00
|
|
|
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
|
2017-08-23 05:42:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 06:39:32 +00:00
|
|
|
if (users.includes(name)) {
|
2016-12-15 06:13:43 +00:00
|
|
|
log.error(`User ${colors.bold(name)} already exists.`);
|
2014-09-13 21:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-23 05:15:01 +00:00
|
|
|
|
2020-10-04 15:03:26 +00:00
|
|
|
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,
|
|
|
|
},
|
2020-03-21 20:55:36 +00:00
|
|
|
function (err, password) {
|
2019-07-17 09:33:59 +00:00
|
|
|
if (!password) {
|
|
|
|
log.error("Password cannot be empty.");
|
|
|
|
return;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (!err) {
|
|
|
|
log.prompt(
|
|
|
|
{
|
|
|
|
text: "Save logs to disk?",
|
|
|
|
default: "yes",
|
|
|
|
},
|
2020-03-21 20:55:36 +00:00
|
|
|
function (err2, enableLog) {
|
2019-07-17 09:33:59 +00:00
|
|
|
if (!err2) {
|
|
|
|
add(
|
|
|
|
manager,
|
|
|
|
name,
|
|
|
|
password,
|
|
|
|
enableLog.charAt(0).toLowerCase() === "y"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-05-01 09:41:17 +00:00
|
|
|
}
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
2014-09-15 21:31:25 +00:00
|
|
|
|
2017-01-29 19:33:57 +00:00
|
|
|
function add(manager, name, password, enableLog) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const hash = Helper.password.hash(password);
|
2017-01-29 19:33:57 +00:00
|
|
|
manager.addUser(name, hash, enableLog);
|
2016-12-15 06:13:43 +00:00
|
|
|
|
|
|
|
log.info(`User ${colors.bold(name)} created.`);
|
2022-05-01 19:12:39 +00:00
|
|
|
log.info(`User file located at ${colors.green(Config.getUserConfigPath(name))}.`);
|
2014-09-16 17:33:15 +00:00
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
|
|
|
|
export default program;
|