2017-09-18 11:57:24 -04:00
|
|
|
"use strict";
|
|
|
|
|
2018-03-02 13:28:54 -05:00
|
|
|
const colors = require("chalk");
|
2017-09-18 11:57:24 -04: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 child = require("child_process");
|
|
|
|
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 11:57:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-27 04:05:34 -04:00
|
|
|
log.info("Retrieving information about the package...");
|
|
|
|
|
2017-09-18 11:57:24 -04:00
|
|
|
packageJson(packageName, {
|
2017-11-15 01:35:15 -05:00
|
|
|
fullMetadata: true,
|
2017-09-18 11:57:24 -04:00
|
|
|
}).then((json) => {
|
2017-09-29 00:03:01 -04:00
|
|
|
if (!("thelounge" in json)) {
|
2017-09-18 11:57:24 -04:00
|
|
|
log.error(`${colors.red(packageName)} does not have The Lounge metadata.`);
|
|
|
|
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`Installing ${colors.green(packageName)}...`);
|
|
|
|
|
|
|
|
const packagesPath = Helper.getPackagesPath();
|
2018-02-08 00:19:44 -05:00
|
|
|
const packagesConfig = path.join(packagesPath, "package.json");
|
2017-09-18 11:57:24 -04:00
|
|
|
|
2018-01-19 05:50:33 -05:00
|
|
|
// Create node_modules folder, otherwise yarn will start walking upwards to find one
|
2018-02-08 00:19:44 -05:00
|
|
|
fsextra.ensureDirSync(path.join(packagesPath, "node_modules"));
|
2017-09-18 11:57:24 -04:00
|
|
|
|
2018-01-19 05:50:33 -05:00
|
|
|
// Create package.json with private set to true, if it doesn't exist already
|
2018-01-16 20:25:35 -05: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 11:57:24 -04:00
|
|
|
|
2018-01-19 05:50:33 -05:00
|
|
|
const yarn = path.join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"node_modules",
|
|
|
|
"yarn",
|
|
|
|
"bin",
|
|
|
|
"yarn.js"
|
|
|
|
);
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
const add = child.spawn(
|
|
|
|
process.execPath,
|
2017-09-18 11:57:24 -04:00
|
|
|
[
|
2018-01-19 05:50:33 -05:00
|
|
|
yarn,
|
|
|
|
"add",
|
|
|
|
"--json",
|
|
|
|
"--exact",
|
2017-09-18 11:57:24 -04:00
|
|
|
"--production",
|
2018-01-19 05:50:33 -05:00
|
|
|
"--ignore-scripts",
|
|
|
|
"--non-interactive",
|
|
|
|
"--cwd",
|
2018-02-08 00:19:44 -05:00
|
|
|
packagesPath,
|
2018-02-14 05:09:02 -05:00
|
|
|
`${packageName}@${json.version}`,
|
2018-01-19 05:50:33 -05:00
|
|
|
]
|
2017-09-18 11:57:24 -04:00
|
|
|
);
|
|
|
|
|
2018-01-19 05:50:33 -05:00
|
|
|
add.stdout.on("data", (data) => {
|
|
|
|
data.toString().trim().split("\n").forEach((line) => {
|
|
|
|
line = JSON.parse(line);
|
|
|
|
|
|
|
|
if (line.type === "success") {
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
add.on("error", (e) => {
|
2017-09-18 11:57:24 -04:00
|
|
|
log.error(`${e}`);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2018-01-19 05:50:33 -05:00
|
|
|
add.on("close", (code) => {
|
|
|
|
if (!success || code !== 0) {
|
2017-09-18 11:57:24 -04:00
|
|
|
log.error(`Failed to install ${colors.green(packageName)}. Exit code: ${code}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:13:46 -05:00
|
|
|
log.info(`${colors.green(packageName + " v" + json.version)} has been successfully installed.`);
|
2017-09-18 11:57:24 -04:00
|
|
|
});
|
|
|
|
}).catch((e) => {
|
|
|
|
log.error(`${e}`);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
});
|