2017-09-18 18:57:24 +03:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-15 23:31:06 +03:00
|
|
|
const log = require("../log");
|
2018-03-02 20:28:54 +02:00
|
|
|
const colors = require("chalk");
|
2020-08-21 12:26:35 +01:00
|
|
|
const semver = require("semver");
|
2017-09-18 18:57:24 +03:00
|
|
|
const program = require("commander");
|
|
|
|
const Helper = require("../helper");
|
|
|
|
const Utils = require("./utils");
|
|
|
|
|
|
|
|
program
|
|
|
|
.command("install <package>")
|
|
|
|
.description("Install a theme or a package")
|
|
|
|
.on("--help", Utils.extraHelp)
|
2020-03-21 22:55:36 +02:00
|
|
|
.action(function (packageName) {
|
2017-09-18 18:57:24 +03:00
|
|
|
const fs = require("fs");
|
2021-06-02 00:43:53 +02:00
|
|
|
const fspromises = fs.promises;
|
|
|
|
const path = require("path");
|
2017-09-18 18:57:24 +03:00
|
|
|
const packageJson = require("package-json");
|
|
|
|
|
2017-12-06 22:54:54 -05:00
|
|
|
if (!fs.existsSync(Helper.getConfigPath())) {
|
|
|
|
log.error(`${Helper.getConfigPath()} does not exist.`);
|
2017-09-18 18:57:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-27 11:05:34 +03:00
|
|
|
log.info("Retrieving information about the package...");
|
2021-06-02 00:43:53 +02:00
|
|
|
let readFile = null;
|
|
|
|
let isLocalFile = false;
|
2017-09-27 11:05:34 +03:00
|
|
|
|
2021-06-02 00:43:53 +02:00
|
|
|
if (packageName.startsWith("file:")) {
|
|
|
|
isLocalFile = true;
|
|
|
|
readFile = fspromises
|
|
|
|
.readFile(path.join(packageName.substr("file:".length), "package.json"), "utf-8")
|
|
|
|
.then((data) => JSON.parse(data));
|
|
|
|
} else {
|
|
|
|
const split = packageName.split("@");
|
|
|
|
packageName = split[0];
|
|
|
|
const packageVersion = split[1] || "latest";
|
2018-03-07 05:40:21 +02:00
|
|
|
|
2021-06-02 00:43:53 +02:00
|
|
|
readFile = packageJson(packageName, {
|
|
|
|
fullMetadata: true,
|
|
|
|
version: packageVersion,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile
|
2019-07-17 10:33:59 +01:00
|
|
|
.then((json) => {
|
2021-06-02 00:43:53 +02:00
|
|
|
const humanVersion = isLocalFile ? packageName : `${json.name} v${json.version}`;
|
|
|
|
|
2019-07-17 10:33:59 +01:00
|
|
|
if (!("thelounge" in json)) {
|
2021-06-02 00:43:53 +02:00
|
|
|
log.error(`${colors.red(humanVersion)} does not have The Lounge metadata.`);
|
2017-09-18 18:57:24 +03:00
|
|
|
|
2019-07-17 10:33:59 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-09-18 18:57:24 +03:00
|
|
|
|
2020-08-21 12:26:35 +01:00
|
|
|
if (
|
|
|
|
json.thelounge.supports &&
|
|
|
|
!semver.satisfies(Helper.getVersionNumber(), json.thelounge.supports)
|
|
|
|
) {
|
|
|
|
log.error(
|
|
|
|
`${colors.red(
|
2021-06-02 00:43:53 +02:00
|
|
|
humanVersion
|
2020-08-21 12:26:35 +01:00
|
|
|
)} does not support The Lounge v${Helper.getVersionNumber()}. Supported version(s): ${
|
|
|
|
json.thelounge.supports
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
2021-06-02 00:43:53 +02:00
|
|
|
log.info(`Installing ${colors.green(humanVersion)}...`);
|
|
|
|
const yarnVersion = isLocalFile ? packageName : `${json.name}@${json.version}`;
|
|
|
|
return Utils.executeYarnCommand("add", "--exact", yarnVersion)
|
2019-07-17 10:33:59 +01:00
|
|
|
.then(() => {
|
2021-06-02 00:43:53 +02: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 10:33:59 +01:00
|
|
|
})
|
|
|
|
.catch((code) => {
|
2021-06-02 00:43:53 +02:00
|
|
|
throw `Failed to install ${colors.red(humanVersion)}. Exit code: ${code}`;
|
2019-07-17 10:33:59 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
log.error(`${e}`);
|
|
|
|
process.exit(1);
|
2017-09-18 18:57:24 +03:00
|
|
|
});
|
|
|
|
});
|