thelounge/server/command-line/users/edit.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

import log from "../../log";
import {Command} from "commander";
import child from "child_process";
import colors from "chalk";
import fs from "fs";
import Config from "../../config";
import Utils from "../utils";
2014-08-26 20:00:12 +02:00
const program = new Command("edit");
2014-08-25 02:19:03 +02:00
program
.description(`Edit user file located at ${colors.green(Config.getUserConfigPath("<name>"))}`)
.argument("<name>", "name of the user")
.on("--help", Utils.extraHelp)
.action(function (name) {
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 users = new ClientManager().getUsers();
2019-07-17 11:33:59 +02:00
if (users === undefined) {
// There was an error, already logged
return;
}
if (!users.includes(name)) {
log.error(`User ${colors.bold(name)} does not exist.`);
2014-08-25 02:19:03 +02:00
return;
}
2018-01-11 12:33:36 +01:00
const child_spawn = child.spawn(
process.env.EDITOR || "vi",
[Config.getUserConfigPath(name)],
2014-08-25 02:19:03 +02:00
{stdio: "inherit"}
);
child_spawn.on("error", function () {
2019-07-17 11:33:59 +02:00
log.error(
`Unable to open ${colors.green(Config.getUserConfigPath(name))}. ${colors.bold(
2019-07-17 11:33:59 +02:00
"$EDITOR"
)} is not set, and ${colors.bold("vi")} was not found.`
);
});
2014-08-25 02:19:03 +02:00
});
export default program;