Merge pull request #4251 from brunnre8/localPlugins

install: allow installation of local packages
This commit is contained in:
Max Leiter 2021-06-05 10:30:18 -07:00 committed by GitHub
commit d5b6a8521f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,8 @@ program
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function (packageName) { .action(function (packageName) {
const fs = require("fs"); const fs = require("fs");
const fspromises = fs.promises;
const path = require("path");
const packageJson = require("package-json"); const packageJson = require("package-json");
if (!fs.existsSync(Helper.getConfigPath())) { if (!fs.existsSync(Helper.getConfigPath())) {
@ -21,22 +23,31 @@ program
} }
log.info("Retrieving information about the package..."); log.info("Retrieving information about the package...");
let readFile = null;
let isLocalFile = false;
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("@"); const split = packageName.split("@");
packageName = split[0]; packageName = split[0];
const packageVersion = split[1] || "latest"; const packageVersion = split[1] || "latest";
packageJson(packageName, { readFile = packageJson(packageName, {
fullMetadata: true, fullMetadata: true,
version: packageVersion, version: packageVersion,
}) });
}
readFile
.then((json) => { .then((json) => {
const humanVersion = isLocalFile ? packageName : `${json.name} v${json.version}`;
if (!("thelounge" in json)) { if (!("thelounge" in json)) {
log.error( log.error(`${colors.red(humanVersion)} does not have The Lounge metadata.`);
`${colors.red(
json.name + " v" + json.version
)} does not have The Lounge metadata.`
);
process.exit(1); process.exit(1);
} }
@ -47,7 +58,7 @@ program
) { ) {
log.error( log.error(
`${colors.red( `${colors.red(
json.name + " v" + json.version humanVersion
)} does not support The Lounge v${Helper.getVersionNumber()}. Supported version(s): ${ )} does not support The Lounge v${Helper.getVersionNumber()}. Supported version(s): ${
json.thelounge.supports json.thelounge.supports
}` }`
@ -56,20 +67,23 @@ program
process.exit(2); process.exit(2);
} }
log.info(`Installing ${colors.green(json.name + " v" + json.version)}...`); log.info(`Installing ${colors.green(humanVersion)}...`);
const yarnVersion = isLocalFile ? packageName : `${json.name}@${json.version}`;
return Utils.executeYarnCommand("add", "--exact", `${json.name}@${json.version}`) return Utils.executeYarnCommand("add", "--exact", yarnVersion)
.then(() => { .then(() => {
log.info( log.info(`${colors.green(humanVersion)} has been successfully installed.`);
`${colors.green(
json.name + " v" + json.version if (isLocalFile) {
)} has been successfully installed.` // 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}`;
});
}
}) })
.catch((code) => { .catch((code) => {
throw `Failed to install ${colors.green( throw `Failed to install ${colors.red(humanVersion)}. Exit code: ${code}`;
json.name + " v" + json.version
)}. Exit code: ${code}`;
}); });
}) })
.catch((e) => { .catch((e) => {