Merge pull request #1539 from thelounge/xpaw/install-command

Create lounge install command
This commit is contained in:
Pavel Djundik 2017-09-20 11:11:48 +03:00 committed by GitHub
commit 01eda0aac3
3 changed files with 84 additions and 0 deletions

View File

@ -52,6 +52,7 @@
"ldapjs": "1.0.1", "ldapjs": "1.0.1",
"lodash": "4.17.4", "lodash": "4.17.4",
"moment": "2.18.1", "moment": "2.18.1",
"package-json": "4.0.1",
"read": "1.0.7", "read": "1.0.7",
"request": "2.82.0", "request": "2.82.0",
"semver": "5.4.1", "semver": "5.4.1",

View File

@ -32,6 +32,7 @@ require("./add");
require("./remove"); require("./remove");
require("./reset"); require("./reset");
require("./edit"); require("./edit");
require("./install");
program.parse(process.argv); program.parse(process.argv);

View File

@ -0,0 +1,82 @@
"use strict";
const colors = require("colors/safe");
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");
if (!fs.existsSync(Helper.CONFIG_PATH)) {
log.error(`${Helper.CONFIG_PATH} does not exist.`);
return;
}
packageJson(packageName, {
fullMetadata: true
}).then((json) => {
if (!("lounge" in json)) {
log.error(`${colors.red(packageName)} does not have The Lounge metadata.`);
process.exit(1);
}
log.info(`Installing ${colors.green(packageName)}...`);
const packagesPath = Helper.getPackagesPath();
const packagesParent = path.dirname(packagesPath);
const packagesConfig = path.join(packagesParent, "package.json");
// Create node_modules folder, otherwise npm will start walking upwards to find one
fsextra.ensureDirSync(packagesPath);
// Create package.json with private set to true to avoid npm warnings
fs.writeFileSync(packagesConfig, JSON.stringify({
private: true,
description: "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.",
}, null, "\t"));
const npm = child.spawn(
process.platform === "win32" ? "npm.cmd" : "npm",
[
"install",
"--production",
"--no-save",
"--no-bin-links",
"--no-package-lock",
"--prefix",
packagesParent,
packageName
],
{
stdio: "inherit"
}
);
npm.on("error", (e) => {
log.error(`${e}`);
process.exit(1);
});
npm.on("close", (code) => {
if (code !== 0) {
log.error(`Failed to install ${colors.green(packageName)}. Exit code: ${code}`);
return;
}
log.info(`${colors.green(packageName)} has been successfully installed.`);
});
}).catch((e) => {
log.error(`${e}`);
process.exit(1);
});
});