UNPKG

2.48 kBJavaScriptView Raw
1"use strict";
2
3const colors = require("colors/safe");
4const program = require("commander");
5const Helper = require("../helper");
6const Utils = require("./utils");
7
8program
9 .command("install <package>")
10 .description("Install a theme or a package")
11 .on("--help", Utils.extraHelp)
12 .action(function(packageName) {
13 const fs = require("fs");
14 const fsextra = require("fs-extra");
15 const path = require("path");
16 const child = require("child_process");
17 const packageJson = require("package-json");
18
19 if (!fs.existsSync(Helper.getConfigPath())) {
20 log.error(`${Helper.getConfigPath()} does not exist.`);
21 return;
22 }
23
24 log.info("Retrieving information about the package...");
25
26 packageJson(packageName, {
27 fullMetadata: true,
28 }).then((json) => {
29 if (!("thelounge" in json)) {
30 log.error(`${colors.red(packageName)} does not have The Lounge metadata.`);
31
32 process.exit(1);
33 }
34
35 log.info(`Installing ${colors.green(packageName)}...`);
36
37 const packagesPath = Helper.getPackagesPath();
38 const packagesConfig = path.join(packagesPath, "package.json");
39
40 // Create node_modules folder, otherwise npm will start walking upwards to find one
41 fsextra.ensureDirSync(path.join(packagesPath, "node_modules"));
42
43 // Create package.json with private set to true to avoid npm warnings, if
44 // it doesn't exist already
45 if (!fs.existsSync(packagesConfig)) {
46 fs.writeFileSync(packagesConfig, JSON.stringify({
47 private: true,
48 description: "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.",
49 }, null, "\t"));
50 }
51
52 const npm = child.spawn(
53 process.platform === "win32" ? "npm.cmd" : "npm",
54 [
55 "install",
56 "--production",
57 "--save",
58 "--save-exact",
59 "--no-bin-links",
60 "--no-package-lock",
61 "--no-progress",
62 "--prefix",
63 packagesPath,
64 `${packageName}@${json.version}`,
65 ],
66 {
67 // This is the same as `"inherit"` except `process.stdout` is ignored
68 stdio: [process.stdin, "ignore", process.stderr],
69 }
70 );
71
72 npm.on("error", (e) => {
73 log.error(`${e}`);
74 process.exit(1);
75 });
76
77 npm.on("close", (code) => {
78 if (code !== 0) {
79 log.error(`Failed to install ${colors.green(packageName)}. Exit code: ${code}`);
80 return;
81 }
82
83 log.info(`${colors.green(packageName + " v" + json.version)} has been successfully installed.`);
84 });
85 }).catch((e) => {
86 log.error(`${e}`);
87 process.exit(1);
88 });
89 });