This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
forked from lerna/lerna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (180 loc) · 6.29 KB
/
index.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
const dedent = require("dedent");
const npa = require("npm-package-arg");
const pMap = require("p-map");
const path = require("path");
const pacote = require("pacote");
const semver = require("semver");
const { Command } = require("@lerna/command");
const npmConf = require("@lerna/npm-conf");
const bootstrap = require("@lerna/bootstrap");
const { ValidationError } = require("@lerna/validation-error");
const { getFilteredPackages } = require("@lerna/filter-options");
const { getRangeToReference } = require("./lib/get-range-to-reference");
module.exports = factory;
function factory(argv) {
return new AddCommand(argv);
}
class AddCommand extends Command {
get requiresGit() {
return false;
}
get dependencyType() {
if (this.options.dev) {
return "devDependencies";
}
if (this.options.peer) {
return "peerDependencies";
}
return "dependencies";
}
initialize() {
this.spec = npa(this.options.pkg);
this.dirs = new Set(this.options.globs.map((fp) => path.resolve(this.project.rootPath, fp)));
this.selfSatisfied = this.packageSatisfied();
// https://docs.npmjs.com/misc/config#save-prefix
this.savePrefix = this.options.exact ? "" : "^";
if (this.packageGraph.has(this.spec.name) && !this.selfSatisfied) {
const available = this.packageGraph.get(this.spec.name).version;
throw new ValidationError(
"ENOTSATISFIED",
dedent`
Requested range not satisfiable:
${this.spec.name}@${this.spec.fetchSpec} (available: ${available})
`
);
}
let chain = Promise.resolve();
chain = chain.then(() => this.getPackageVersion());
chain = chain.then((version) => {
if (version == null) {
throw new ValidationError(
"ENOTSATISFIED",
dedent`
Requested package has no version: ${this.spec.name}
`
);
}
this.spec.version = version;
});
chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options));
chain = chain.then((filteredPackages) => {
this.filteredPackages = filteredPackages;
});
chain = chain.then(() => this.collectPackagesToChange());
chain = chain.then((packagesToChange) => {
this.packagesToChange = packagesToChange;
});
return chain.then(() => {
const proceed = this.packagesToChange.length > 0;
if (!proceed) {
this.logger.warn(`No packages found where ${this.spec.name} can be added.`);
}
return proceed;
});
}
execute() {
const numberOfPackages = `${this.packagesToChange.length} package${
this.packagesToChange.length > 1 ? "s" : ""
}`;
this.logger.info("", `Adding ${this.spec.name} in ${numberOfPackages}`);
let chain = Promise.resolve();
chain = chain.then(() => this.makeChanges());
if (this.options.bootstrap !== false) {
chain = chain.then(() => {
const argv = Object.assign({}, this.options, {
args: [],
cwd: this.project.rootPath,
// silence initial cli version logging, etc
composed: "add",
// NEVER pass filter-options, it is very bad
scope: undefined,
ignore: undefined,
private: undefined,
since: undefined,
excludeDependents: undefined,
includeDependents: undefined,
includeDependencies: undefined,
});
return bootstrap(argv);
});
}
return chain;
}
collectPackagesToChange() {
const { name: targetName } = this.spec;
let result = this.filteredPackages;
// Skip packages that only would install themselves
if (this.packageGraph.has(targetName)) {
result = result.filter((pkg) => pkg.name !== targetName);
}
// Skip packages that are not selected by dir globs
if (this.dirs.size) {
result = result.filter((pkg) => this.dirs.has(pkg.location));
}
// Skip packages without actual changes to manifest
result = result.filter((pkg) => {
const deps = this.getPackageDeps(pkg);
// Check if one of the packages to install necessitates a change to pkg's manifest
if (!(targetName in deps)) {
return true;
}
return getRangeToReference(this.spec, deps, pkg.location, this.savePrefix) !== deps[targetName];
});
return result;
}
makeChanges() {
const { name: targetName } = this.spec;
return pMap(this.packagesToChange, (pkg) => {
const deps = this.getPackageDeps(pkg);
const range = getRangeToReference(this.spec, deps, pkg.location, this.savePrefix);
this.logger.verbose("add", `${targetName}@${range} to ${this.dependencyType} in ${pkg.name}`);
deps[targetName] = range;
return pkg.serialize();
});
}
getPackageDeps(pkg) {
let deps = pkg.get(this.dependencyType);
if (!deps) {
deps = {};
pkg.set(this.dependencyType, deps);
}
return deps;
}
getPackageVersion() {
if (this.selfSatisfied) {
const node = this.packageGraph.get(this.spec.name);
return Promise.resolve(this.spec.saveRelativeFileSpec ? node.location : node.version);
}
// @see https://github.com/zkat/pacote/blob/latest/lib/util/opt-check.js
const opts = npmConf({
includeDeprecated: false,
// we can't pass everything, as our --scope conflicts with pacote's --scope
registry: this.options.registry,
});
return pacote.manifest(this.spec, opts.snapshot).then((pkg) => pkg.version);
}
packageSatisfied() {
const { name, fetchSpec } = this.spec;
const pkg = this.packageGraph.get(name);
if (!pkg) {
return false;
}
// an explicit "file:packages/foo" always saves as a relative "file:../foo"
if (this.spec.type === "directory" && fetchSpec === pkg.location) {
this.spec.saveRelativeFileSpec = true;
return true;
}
// existing relative file spec means local dep should be added the same way
this.spec.saveRelativeFileSpec = Array.from(this.packageGraph.values()).some(
(node) =>
node.localDependencies.size &&
Array.from(node.localDependencies.values()).some((resolved) => resolved.type === "directory")
);
if (fetchSpec === "latest") {
return true;
}
return semver.intersects(pkg.version, fetchSpec);
}
}
module.exports.AddCommand = AddCommand;