2022-06-19 00:25:21 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
import log from "../log";
|
|
|
|
import colors from "chalk";
|
|
|
|
import fs from "fs";
|
|
|
|
import Helper from "../helper";
|
|
|
|
import Config from "../config";
|
|
|
|
import path from "path";
|
|
|
|
import {spawn} from "child_process";
|
|
|
|
let home: string;
|
2017-08-21 05:49:32 +00:00
|
|
|
|
|
|
|
class Utils {
|
2022-06-19 00:25:21 +00:00
|
|
|
static extraHelp(this: void) {
|
2017-08-21 05:49:32 +00:00
|
|
|
[
|
|
|
|
"",
|
2020-03-15 11:53:09 +00:00
|
|
|
"Environment variable:",
|
|
|
|
` THELOUNGE_HOME Path for all configuration files and folders. Defaults to ${colors.green(
|
2019-07-17 09:33:59 +00:00
|
|
|
Helper.expandHome(Utils.defaultHome())
|
2020-03-15 11:53:09 +00:00
|
|
|
)}`,
|
2017-08-21 05:49:32 +00:00
|
|
|
"",
|
2017-12-09 07:11:05 +00:00
|
|
|
].forEach((e) => log.raw(e));
|
2017-08-21 05:49:32 +00:00
|
|
|
}
|
2017-08-21 06:03:40 +00:00
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
static defaultHome() {
|
|
|
|
if (home) {
|
|
|
|
return home;
|
2017-08-21 06:03:40 +00:00
|
|
|
}
|
2017-11-19 18:21:37 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const distConfig = Utils.getFileFromRelativeToRoot(".thelounge_home");
|
2017-08-21 06:03:40 +00:00
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
home = fs.readFileSync(distConfig, "utf-8").trim();
|
2017-08-21 06:03:40 +00:00
|
|
|
|
2017-11-19 18:21:37 +00:00
|
|
|
return home;
|
2017-08-21 06:03:40 +00:00
|
|
|
}
|
2017-12-09 20:06:41 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
static getFileFromRelativeToRoot(...fileName: string[]) {
|
|
|
|
// e.g. /thelounge/server/command-line/utils.ts
|
|
|
|
if (process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development") {
|
|
|
|
return path.resolve(path.join(__dirname, "..", "..", ...fileName));
|
|
|
|
}
|
|
|
|
|
|
|
|
// e.g. /thelounge/dist/server/command-line/utils.ts
|
|
|
|
return path.resolve(path.join(__dirname, "..", "..", "..", ...fileName));
|
|
|
|
}
|
|
|
|
|
2017-12-09 20:06:41 +00:00
|
|
|
// Parses CLI options such as `-c public=true`, `-c debug.raw=true`, etc.
|
2022-06-19 00:25:21 +00:00
|
|
|
static parseConfigOptions(this: void, val: string, memo?: any) {
|
2017-12-09 20:06:41 +00:00
|
|
|
// Invalid option that is not of format `key=value`, do nothing
|
|
|
|
if (!val.includes("=")) {
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
2017-12-09 20:06:41 +00:00
|
|
|
return memo;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const parseValue = (value: string) => {
|
|
|
|
switch (value) {
|
|
|
|
case "true":
|
|
|
|
return true;
|
|
|
|
case "false":
|
|
|
|
return false;
|
|
|
|
case "undefined":
|
|
|
|
return undefined;
|
|
|
|
case "null":
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
if (/^-?[0-9]+$/.test(value)) {
|
|
|
|
// Numbers like port
|
|
|
|
return parseInt(value, 10);
|
|
|
|
} else if (/^\[.*\]$/.test(value)) {
|
|
|
|
// Arrays
|
|
|
|
// Supporting arrays `[a,b]` and `[a, b]`
|
|
|
|
const array = value.slice(1, -1).split(/,\s*/);
|
|
|
|
|
|
|
|
// If [] is given, it will be parsed as `[ "" ]`, so treat this as empty
|
|
|
|
if (array.length === 1 && array[0] === "") {
|
|
|
|
return [];
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
return array.map(parseValue) as Array<Record<string, string>>; // Re-parses all values of the array
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
return value;
|
|
|
|
}
|
2017-12-09 20:06:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// First time the option is parsed, memo is not set
|
|
|
|
if (memo === undefined) {
|
|
|
|
memo = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: If passed `-c foo="bar=42"` (with single or double quotes), `val`
|
|
|
|
// will always be passed as `foo=bar=42`, never with quotes.
|
|
|
|
const position = val.indexOf("="); // Only split on the first = found
|
|
|
|
const key = val.slice(0, position);
|
|
|
|
const value = val.slice(position + 1);
|
|
|
|
const parsedValue = parseValue(value);
|
|
|
|
|
|
|
|
if (_.has(memo, key)) {
|
|
|
|
log.warn(`Configuration key ${colors.bold(key)} was already specified, ignoring...`);
|
|
|
|
} else {
|
|
|
|
memo = _.set(memo, key, parsedValue);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
2017-12-09 20:06:41 +00:00
|
|
|
return memo;
|
|
|
|
}
|
2018-04-24 18:38:54 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
static executeYarnCommand(command: string, ...parameters: string[]) {
|
2019-01-24 16:52:15 +00:00
|
|
|
const yarn = require.resolve("yarn/bin/yarn.js");
|
2022-05-01 19:12:39 +00:00
|
|
|
const packagesPath = Config.getPackagesPath();
|
2019-01-16 10:51:52 +00:00
|
|
|
const cachePath = path.join(packagesPath, "package_manager_cache");
|
|
|
|
|
|
|
|
const staticParameters = [
|
|
|
|
"--cache-folder",
|
|
|
|
cachePath,
|
|
|
|
"--cwd",
|
|
|
|
packagesPath,
|
|
|
|
"--json",
|
|
|
|
"--ignore-scripts",
|
|
|
|
"--non-interactive",
|
|
|
|
];
|
|
|
|
|
2019-12-13 15:45:10 +00:00
|
|
|
const env = {
|
|
|
|
// We only ever operate in production mode
|
|
|
|
NODE_ENV: "production",
|
|
|
|
|
|
|
|
// If The Lounge runs from a user that does not have a home directory,
|
|
|
|
// yarn may fail when it tries to read certain folders,
|
|
|
|
// we give it an existing folder so the reads do not throw a permission error.
|
|
|
|
// Yarn uses os.homedir() to figure out the path, which internally reads
|
|
|
|
// from the $HOME env on unix. On Windows it uses $USERPROFILE, but
|
|
|
|
// the user folder should always exist on Windows, so we don't set it.
|
|
|
|
HOME: cachePath,
|
|
|
|
};
|
|
|
|
|
2018-04-24 18:38:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let success = false;
|
2022-06-19 00:25:21 +00:00
|
|
|
const add = spawn(
|
2019-12-13 15:45:10 +00:00
|
|
|
process.execPath,
|
|
|
|
[yarn, command, ...staticParameters, ...parameters],
|
|
|
|
{env: env}
|
|
|
|
);
|
2018-04-24 18:38:54 +00:00
|
|
|
|
|
|
|
add.stdout.on("data", (data) => {
|
2019-07-17 09:33:59 +00:00
|
|
|
data.toString()
|
|
|
|
.trim()
|
|
|
|
.split("\n")
|
|
|
|
.forEach((line) => {
|
2020-02-11 09:48:02 +00:00
|
|
|
try {
|
|
|
|
line = JSON.parse(line);
|
|
|
|
|
|
|
|
if (line.type === "success") {
|
|
|
|
success = true;
|
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
2020-02-11 09:48:02 +00:00
|
|
|
// Stdout buffer has limitations and yarn may print
|
|
|
|
// big package trees, for example in the upgrade command
|
|
|
|
// See https://github.com/thelounge/thelounge/issues/3679
|
2019-07-17 09:33:59 +00:00
|
|
|
}
|
|
|
|
});
|
2018-04-24 18:38:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
add.stderr.on("data", (data) => {
|
2019-07-17 09:33:59 +00:00
|
|
|
data.toString()
|
|
|
|
.trim()
|
|
|
|
.split("\n")
|
2022-06-19 00:25:21 +00:00
|
|
|
.forEach((line: string) => {
|
2019-07-17 09:33:59 +00:00
|
|
|
const json = JSON.parse(line);
|
|
|
|
|
|
|
|
if (json.type === "error") {
|
|
|
|
log.error(json.data);
|
|
|
|
}
|
|
|
|
});
|
2018-04-24 18:38:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
add.on("error", (e) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
log.error(`${e.message}:`, e.stack || "");
|
2018-04-24 18:38:54 +00:00
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
add.on("close", (code) => {
|
|
|
|
if (!success || code !== 0) {
|
|
|
|
return reject(code);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
resolve(true);
|
2018-04-24 18:38:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-08-21 05:49:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default Utils;
|