-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathinstall-peers.js
112 lines (94 loc) · 3.08 KB
/
install-peers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-disable */
const chalk = require("chalk");
const path = require("path");
const fs = require("fs");
const executioner = require("executioner");
const die = (message) => console.error(chalk.bold.red(message));
const warn = (message) => console.warn(chalk.yellow(message));
const status = (message) => console.warn(chalk.bold.white(message));
const success = (message) => console.warn(chalk.green(message));
class Package {
constructor(contents) {
try {
this.contents = JSON.parse(contents);
} catch (error) {
this.contents = null;
}
}
isValid() {
return this.contents !== null;
}
hasPeerDependencies() {
return this.contents.peerDependencies !== undefined;
}
get peerDependencies() {
return this.contents.peerDependencies || [];
}
get peerInstallOptions() {
return this.contents.peerInstallOptions || {};
}
}
let yarnBin;
const matched = `${path.sep}bin${path.sep}yarn.js`;
if (process.env["npm_execpath"] && process.env["npm_execpath"].includes(matched)) {
yarnBin = path.resolve(process.env["npm_execpath"]);
}
const envLabel = "install_peers_skip";
if (yarnBin && process.env[envLabel] !== "1") {
let peerDependencies = {};
fs.readdirSync("./packages", { encoding: "utf-8" }).forEach((dir) => {
try {
const contents = fs.readFileSync(`./packages/${dir}/package.json`);
if (contents === undefined) {
return console.log(`There doesn't seem to be a package.json in packages/${dir}/package.json`);
}
let packageContents = new Package(contents);
if (!packageContents.isValid()) {
return die("Invalid package.json contents\n");
}
if (!packageContents.hasPeerDependencies()) {
return warn(`Package ${dir} doesn't seem to have any peerDependencies\n`);
}
peerDependencies = {
...peerDependencies,
...packageContents.peerDependencies,
};
} catch (err) {
warn(err);
}
});
process.env[envLabel] = "1";
const packages = [...new Set(Object.keys(peerDependencies))]
.filter((key) => {
return !key.includes("@hyper-fetch");
})
.map(function (key) {
return `${key}@${peerDependencies[key]}`;
});
success(`Peer dependencies found - ${packages.join(", ")}...\n`);
var options = {
node: process.argv[0],
yarn: yarnBin,
// escape package names@versions
packages: packages.map((pkg) => `"${pkg}"`).join(" "),
ignoreWorkspaceRootCheck: "",
};
if (!packages.length) {
return status(`No packages to install.\n`);
}
executioner(
'"${node}" "${yarn}" add ${ignoreWorkspaceRootCheck} --peer --pure-lockfile --prefer-offline -W ${packages}',
options,
function (error) {
process.env[envLabel] = "0";
if (error) {
die("Installation failed", error);
return process.exit();
}
success("+ Successfully installed " + packages.length + " peerDependencies via yarn.\n");
return process.exit();
},
);
// Looks like yarn shows last line from the output of sub-scripts
status("- Installing " + packages.length + " peerDependencies...\n");
}