2022-06-19 00:25:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
|
|
import log from "../log";
|
|
|
|
import colors from "chalk";
|
|
|
|
import semver from "semver";
|
|
|
|
import Helper from "../helper";
|
|
|
|
import Config from "../config";
|
|
|
|
import Utils from "./utils";
|
|
|
|
import {Command} from "commander";
|
|
|
|
import {FullMetadata} from "package-json";
|
2017-09-18 15:57:24 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
type CustomMetadata = FullMetadata & {
|
|
|
|
thelounge: {
|
|
|
|
supports: string;
|
|
|
|
};
|
|
|
|
};
|
2017-09-18 15:57:24 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const program = new Command("install");
|
2017-09-18 15:57:24 +00:00
|
|
|
program
|
2022-07-23 14:27:56 +00:00
|
|
|
.argument(
|
|
|
|
"<package>",
|
|
|
|
"package to install. Use `file:$path_to_package_dir` to install a local package"
|
|
|
|
)
|
2017-09-18 15:57:24 +00:00
|
|
|
.description("Install a theme or a package")
|
|
|
|
.on("--help", Utils.extraHelp)
|
2022-06-19 00:25:21 +00:00
|
|
|
.action(async function (packageName: string) {
|
|
|
|
const fs = await import("fs");
|
2021-06-01 22:43:53 +00:00
|
|
|
const fspromises = fs.promises;
|
2022-06-19 00:25:21 +00:00
|
|
|
const path = await import("path");
|
|
|
|
const packageJson = await import("package-json");
|
2017-09-18 15:57:24 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!fs.existsSync(Config.getConfigPath())) {
|
|
|
|
log.error(`${Config.getConfigPath()} does not exist.`);
|
2017-09-18 15:57:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-27 08:05:34 +00:00
|
|
|
log.info("Retrieving information about the package...");
|
2022-06-19 00:25:21 +00:00
|
|
|
// TODO: type
|
|
|
|
let readFile: any = null;
|
2021-06-01 22:43:53 +00:00
|
|
|
let isLocalFile = false;
|
2017-09-27 08:05:34 +00:00
|
|
|
|
2021-06-01 22:43:53 +00:00
|
|
|
if (packageName.startsWith("file:")) {
|
|
|
|
isLocalFile = true;
|
2022-07-23 14:40:28 +00:00
|
|
|
// our yarn invocation sets $HOME to the cachedir, so we must expand ~ now
|
|
|
|
// else the path will be invalid when npm expands it.
|
|
|
|
packageName = expandTildeInLocalPath(packageName);
|
2021-06-01 22:43:53 +00:00
|
|
|
readFile = fspromises
|
2022-07-23 14:40:28 +00:00
|
|
|
.readFile(path.join(packageName.substring("file:".length), "package.json"), "utf-8")
|
2022-06-19 00:25:21 +00:00
|
|
|
.then((data) => JSON.parse(data) as typeof packageJson);
|
2021-06-01 22:43:53 +00:00
|
|
|
} else {
|
|
|
|
const split = packageName.split("@");
|
|
|
|
packageName = split[0];
|
|
|
|
const packageVersion = split[1] || "latest";
|
2018-03-07 03:40:21 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
readFile = packageJson.default(packageName, {
|
2021-06-01 22:43:53 +00:00
|
|
|
fullMetadata: true,
|
|
|
|
version: packageVersion,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!readFile) {
|
|
|
|
// no-op, error should've been thrown before this point
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-01 22:43:53 +00:00
|
|
|
readFile
|
2022-06-19 00:25:21 +00:00
|
|
|
.then((json: CustomMetadata) => {
|
2021-06-01 22:43:53 +00:00
|
|
|
const humanVersion = isLocalFile ? packageName : `${json.name} v${json.version}`;
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (!("thelounge" in json)) {
|
2023-10-09 10:28:14 +00:00
|
|
|
log.error(`${colors.red(humanVersion)} does not have Hard Lounge metadata.`);
|
2017-09-18 15:57:24 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-09-18 15:57:24 +00:00
|
|
|
|
2020-08-21 11:26:35 +00:00
|
|
|
if (
|
|
|
|
json.thelounge.supports &&
|
2023-06-01 12:03:07 +00:00
|
|
|
!semver.satisfies(Helper.getVersionNumber(), json.thelounge.supports, {
|
|
|
|
includePrerelease: true,
|
|
|
|
})
|
2020-08-21 11:26:35 +00:00
|
|
|
) {
|
|
|
|
log.error(
|
|
|
|
`${colors.red(
|
2021-06-01 22:43:53 +00:00
|
|
|
humanVersion
|
2023-10-09 10:28:14 +00:00
|
|
|
)} does not support Hard Lounge v${Helper.getVersionNumber()}. Supported version(s): ${
|
2020-08-21 11:26:35 +00:00
|
|
|
json.thelounge.supports
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
2021-06-01 22:43:53 +00:00
|
|
|
log.info(`Installing ${colors.green(humanVersion)}...`);
|
|
|
|
const yarnVersion = isLocalFile ? packageName : `${json.name}@${json.version}`;
|
|
|
|
return Utils.executeYarnCommand("add", "--exact", yarnVersion)
|
2019-07-17 09:33:59 +00:00
|
|
|
.then(() => {
|
2021-06-01 22:43:53 +00:00
|
|
|
log.info(`${colors.green(humanVersion)} has been successfully installed.`);
|
|
|
|
|
|
|
|
if (isLocalFile) {
|
|
|
|
// yarn v1 is buggy if a local filepath is used and doesn't update
|
|
|
|
// the lockfile properly. We need to run an install in that case
|
|
|
|
// even though that's supposed to be done by the add subcommand
|
|
|
|
return Utils.executeYarnCommand("install").catch((err) => {
|
|
|
|
throw `Failed to update lockfile after package install ${err}`;
|
|
|
|
});
|
|
|
|
}
|
2019-07-17 09:33:59 +00:00
|
|
|
})
|
|
|
|
.catch((code) => {
|
2021-06-01 22:43:53 +00:00
|
|
|
throw `Failed to install ${colors.red(humanVersion)}. Exit code: ${code}`;
|
2019-07-17 09:33:59 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
log.error(`${e}`);
|
|
|
|
process.exit(1);
|
2017-09-18 15:57:24 +00:00
|
|
|
});
|
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
|
2022-07-23 14:40:28 +00:00
|
|
|
function expandTildeInLocalPath(packageName: string): string {
|
|
|
|
const path = packageName.substring("file:".length);
|
|
|
|
return "file:" + Helper.expandHome(path);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default program;
|