2017-09-18 17:57:24 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-15 22:31:06 +02:00
|
|
|
const log = require("../log");
|
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");
|
|
|
|
|
2017-12-07 04:54:54 +01:00
|
|
|
if (!fs.existsSync(Helper.getConfigPath())) {
|
|
|
|
log.error(`${Helper.getConfigPath()} does not exist.`);
|
2017-09-18 17:57:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-27 10:05:34 +02:00
|
|
|
log.info("Retrieving information about the package...");
|
|
|
|
|
2018-03-07 04:40:21 +01:00
|
|
|
const split = packageName.split("@");
|
|
|
|
packageName = split[0];
|
|
|
|
const packageVersion = split[1] || "latest";
|
|
|
|
|
2017-09-18 17:57:24 +02:00
|
|
|
packageJson(packageName, {
|
2017-11-15 07:35:15 +01:00
|
|
|
fullMetadata: true,
|
2018-03-07 04:40:21 +01:00
|
|
|
version: packageVersion,
|
2017-09-18 17:57:24 +02:00
|
|
|
}).then((json) => {
|
2017-09-29 06:03:01 +02:00
|
|
|
if (!("thelounge" in json)) {
|
2018-03-07 04:40:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-07 04:40:21 +01:00
|
|
|
log.info(`Installing ${colors.green(json.name + " v" + json.version)}...`);
|
2017-09-18 17:57:24 +02:00
|
|
|
|
|
|
|
const packagesPath = Helper.getPackagesPath();
|
2018-02-08 06:19:44 +01:00
|
|
|
const packagesConfig = path.join(packagesPath, "package.json");
|
2017-09-18 17:57:24 +02:00
|
|
|
|
2018-01-19 11:50:33 +01:00
|
|
|
// Create node_modules folder, otherwise yarn will start walking upwards to find one
|
2018-02-08 06:19:44 +01:00
|
|
|
fsextra.ensureDirSync(path.join(packagesPath, "node_modules"));
|
2017-09-18 17:57:24 +02:00
|
|
|
|
2018-01-19 11:50:33 +01:00
|
|
|
// Create package.json with private set to true, if it doesn't exist already
|
2018-01-17 02:25:35 +01:00
|
|
|
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
|
|
|
|
2018-04-24 20:38:54 +02:00
|
|
|
return Utils.executeYarnCommand(
|
|
|
|
"add",
|
|
|
|
"--production",
|
2019-01-16 11:51:52 +01:00
|
|
|
"--exact",
|
2018-04-24 20:38:54 +02:00
|
|
|
`${json.name}@${json.version}`
|
|
|
|
).then(() => {
|
2018-03-07 04:40:21 +01:00
|
|
|
log.info(`${colors.green(json.name + " v" + json.version)} has been successfully installed.`);
|
2018-04-24 20:38:54 +02:00
|
|
|
}).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);
|
|
|
|
});
|
|
|
|
});
|