2022-06-19 00:25:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
import log from "../log";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import colors from "chalk";
|
|
|
|
import {Command} from "commander";
|
|
|
|
import Helper from "../helper";
|
|
|
|
import Config from "../config";
|
|
|
|
import Utils from "./utils";
|
|
|
|
|
|
|
|
const program = new Command("thelounge");
|
2019-07-17 09:33:59 +00:00
|
|
|
program
|
|
|
|
.version(Helper.getVersion(), "-v, --version")
|
2017-12-09 20:06:41 +00:00
|
|
|
.option(
|
|
|
|
"-c, --config <key=value>",
|
|
|
|
"override entries of the configuration file, must be specified for each entry that needs to be overriden",
|
|
|
|
Utils.parseConfigOptions
|
|
|
|
)
|
2017-12-10 21:57:26 +00:00
|
|
|
.on("--help", Utils.extraHelp);
|
|
|
|
|
|
|
|
// Parse options from `argv` returning `argv` void of these options.
|
|
|
|
const argvWithoutOptions = program.parseOptions(process.argv);
|
2016-05-09 16:19:16 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.setHome(process.env.THELOUNGE_HOME || Utils.defaultHome());
|
2014-10-03 23:33:44 +00:00
|
|
|
|
2018-06-19 13:17:42 +00:00
|
|
|
// Check config file owner and warn if we're running under a different user
|
2019-10-22 12:00:05 +00:00
|
|
|
try {
|
|
|
|
verifyFileOwner();
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
2019-10-22 12:00:05 +00:00
|
|
|
// We do not care about failures of these checks
|
|
|
|
// fs.statSync will throw if config.js does not exist (e.g. first run)
|
2018-06-19 13:17:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 18:25:02 +00:00
|
|
|
// Create packages/package.json
|
|
|
|
createPackagesFolder();
|
|
|
|
|
2017-12-09 20:06:41 +00:00
|
|
|
// Merge config key-values passed as CLI options into the main config
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.merge(program.opts().config);
|
2017-12-09 20:06:41 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
program.addCommand(require("./start").default);
|
|
|
|
program.addCommand(require("./install").default);
|
|
|
|
program.addCommand(require("./uninstall").default);
|
|
|
|
program.addCommand(require("./upgrade").default);
|
|
|
|
program.addCommand(require("./outdated").default);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!Config.values.public) {
|
2022-06-19 00:25:21 +00:00
|
|
|
require("./users").default.forEach((command: Command) => {
|
|
|
|
if (command) {
|
|
|
|
program.addCommand(command);
|
|
|
|
}
|
|
|
|
});
|
2017-12-07 02:28:21 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-10 21:57:26 +00:00
|
|
|
// `parse` expects to be passed `process.argv`, but we need to remove to give it
|
|
|
|
// a version of `argv` that does not contain options already parsed by
|
|
|
|
// `parseOptions` above.
|
|
|
|
// This is done by giving it the updated `argv` that `parseOptions` returned,
|
2020-03-15 11:53:09 +00:00
|
|
|
// except it returns an object with `operands`/`unknown`, so we need to concat them.
|
2017-12-10 21:57:26 +00:00
|
|
|
// See https://github.com/tj/commander.js/blob/fefda77f463292/index.js#L686-L763
|
2020-03-15 11:53:09 +00:00
|
|
|
program.parse(argvWithoutOptions.operands.concat(argvWithoutOptions.unknown));
|
2019-10-22 12:00:05 +00:00
|
|
|
|
2019-12-13 18:25:02 +00:00
|
|
|
function createPackagesFolder() {
|
2022-05-01 19:12:39 +00:00
|
|
|
const packagesPath = Config.getPackagesPath();
|
2019-12-13 18:25:02 +00:00
|
|
|
const packagesConfig = path.join(packagesPath, "package.json");
|
|
|
|
|
|
|
|
// Create node_modules folder, otherwise yarn will start walking upwards to find one
|
2020-03-16 11:53:48 +00:00
|
|
|
fs.mkdirSync(path.join(packagesPath, "node_modules"), {recursive: true});
|
2019-12-13 18:25:02 +00:00
|
|
|
|
|
|
|
// Create package.json with private set to true, if it doesn't exist already
|
|
|
|
if (!fs.existsSync(packagesConfig)) {
|
|
|
|
fs.writeFileSync(
|
|
|
|
packagesConfig,
|
|
|
|
JSON.stringify(
|
|
|
|
{
|
|
|
|
private: true,
|
|
|
|
description:
|
2020-03-16 11:53:48 +00:00
|
|
|
"Packages for The Lounge. Use `thelounge install <package>` command to add a package.",
|
2019-12-13 18:25:02 +00:00
|
|
|
dependencies: {},
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
"\t"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 12:00:05 +00:00
|
|
|
function verifyFileOwner() {
|
|
|
|
if (!process.getuid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uid = process.getuid();
|
|
|
|
|
|
|
|
if (uid === 0) {
|
|
|
|
log.warn(
|
|
|
|
`You are currently running The Lounge as root. ${colors.bold.red(
|
|
|
|
"We highly discourage running as root!"
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const configStat = fs.statSync(path.join(Config.getHomePath(), "config.js"));
|
2019-10-22 12:00:05 +00:00
|
|
|
|
|
|
|
if (configStat && configStat.uid !== uid) {
|
|
|
|
log.warn(
|
|
|
|
"Config file owner does not match the user you are currently running The Lounge as."
|
|
|
|
);
|
|
|
|
log.warn(
|
|
|
|
"To prevent any issues, please run thelounge commands " +
|
|
|
|
"as the correct user that owns the config folder."
|
|
|
|
);
|
|
|
|
log.warn(
|
|
|
|
"See https://thelounge.chat/docs/usage#using-the-correct-system-user for more information."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
|
|
|
|
export default program;
|