thelounge/src/command-line/install.js

75 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-09-18 17:57:24 +02:00
"use strict";
2018-03-02 19:28:54 +01:00
const colors = require("chalk");
2017-09-18 17:57:24 +02: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)
.action(function(packageName) {
const fs = require("fs");
const fsextra = require("fs-extra");
const path = require("path");
const packageJson = require("package-json");
if (!fs.existsSync(Helper.getConfigPath())) {
log.error(`${Helper.getConfigPath()} does not exist.`);
2017-09-18 17:57:24 +02:00
return;
}
log.info("Retrieving information about the package...");
const split = packageName.split("@");
packageName = split[0];
const packageVersion = split[1] || "latest";
2017-09-18 17:57:24 +02:00
packageJson(packageName, {
fullMetadata: true,
version: packageVersion,
2017-09-18 17:57:24 +02:00
}).then((json) => {
if (!("thelounge" in json)) {
log.error(`${colors.red(json.name + " v" + json.version)} does not have The Lounge metadata.`);
2017-09-18 17:57:24 +02:00
process.exit(1);
}
log.info(`Installing ${colors.green(json.name + " v" + json.version)}...`);
2017-09-18 17:57:24 +02:00
const packagesPath = Helper.getPackagesPath();
const packagesConfig = path.join(packagesPath, "package.json");
2017-09-18 17:57:24 +02:00
// Create node_modules folder, otherwise yarn will start walking upwards to find one
fsextra.ensureDirSync(path.join(packagesPath, "node_modules"));
2017-09-18 17:57:24 +02: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: "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.",
}, null, "\t"));
}
2017-09-18 17:57:24 +02:00
return Utils.executeYarnCommand(
"add",
"--json",
"--exact",
"--production",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
`${json.name}@${json.version}`
).then(() => {
log.info(`${colors.green(json.name + " v" + json.version)} has been successfully installed.`);
}).catch((code) => {
throw `Failed to install ${colors.green(json.name + " v" + json.version)}. Exit code: ${code}`;
2017-09-18 17:57:24 +02:00
});
}).catch((e) => {
log.error(`${e}`);
process.exit(1);
});
});