-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.js
251 lines (213 loc) · 6.38 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"use strict";
const npa = require("npm-package-arg");
const path = require("path");
const loadJsonFile = require("load-json-file");
const writePkg = require("write-pkg");
// symbol used to "hide" internal state
const PKG = Symbol("pkg");
function binSafeName({ name, scope }) {
return scope ? name.substring(scope.length + 1) : name;
}
// package.json files are not that complicated, so this is intentionally naïve
function shallowCopy(json) {
return Object.keys(json).reduce((obj, key) => {
const val = json[key];
/* istanbul ignore if */
if (Array.isArray(val)) {
obj[key] = val.slice();
} else if (val && typeof val === "object") {
obj[key] = Object.assign({}, val);
} else {
obj[key] = val;
}
return obj;
}, {});
}
class Package {
constructor(pkg, location, rootPath = location) {
// npa will throw an error if the name is invalid
const resolved = npa.resolve(pkg.name, `file:${path.relative(rootPath, location)}`, rootPath);
Object.defineProperties(this, {
// read-only
name: {
enumerable: true,
value: pkg.name,
},
location: {
value: location,
},
private: {
value: Boolean(pkg.private),
},
resolved: {
value: resolved,
},
rootPath: {
value: rootPath,
},
// internal state is "private"
[PKG]: {
configurable: true,
value: pkg,
},
// safer than instanceof across module boundaries
__isLernaPackage: {
value: true,
},
// immutable
bin: {
value:
typeof pkg.bin === "string"
? {
[binSafeName(resolved)]: pkg.bin,
}
: Object.assign({}, pkg.bin),
},
scripts: {
value: Object.assign({}, pkg.scripts),
},
manifestLocation: {
value: path.join(location, "package.json"),
},
nodeModulesLocation: {
value: path.join(location, "node_modules"),
},
binLocation: {
value: path.join(location, "node_modules", ".bin"),
},
});
}
// accessors
get version() {
return this[PKG].version;
}
set version(version) {
this[PKG].version = version;
}
get contents() {
// if modified with setter, use that value
if (this._contents) {
return this._contents;
}
// if provided by pkg.publishConfig.directory value
if (this[PKG].publishConfig && this[PKG].publishConfig.directory) {
return path.join(this.location, this[PKG].publishConfig.directory);
}
// default to package root
return this.location;
}
set contents(subDirectory) {
Object.defineProperty(this, "_contents", {
value: path.join(this.location, subDirectory),
});
}
// "live" collections
get dependencies() {
return this[PKG].dependencies;
}
get devDependencies() {
return this[PKG].devDependencies;
}
get optionalDependencies() {
return this[PKG].optionalDependencies;
}
get peerDependencies() {
return this[PKG].peerDependencies;
}
/**
* Map-like retrieval of arbitrary values
* @param {String} key field name to retrieve value
* @returns {Any} value stored under key, if present
*/
get(key) {
return this[PKG][key];
}
/**
* Map-like storage of arbitrary values
* @param {String} key field name to store value
* @param {Any} val value to store
* @returns {Package} instance for chaining
*/
set(key, val) {
this[PKG][key] = val;
return this;
}
/**
* Provide shallow copy for munging elsewhere
* @returns {Object}
*/
toJSON() {
return shallowCopy(this[PKG]);
}
/**
* Refresh internal state from disk (e.g., changed by external lifecycles)
*/
refresh() {
return loadJsonFile(this.manifestLocation).then(pkg => {
// overwrite configurable property
Object.defineProperty(this, PKG, {
value: pkg,
});
return this;
});
}
/**
* Write manifest changes to disk
* @returns {Promise} resolves when write finished
*/
serialize() {
return writePkg(this.manifestLocation, this[PKG]).then(() => this);
}
/**
* Mutate local dependency spec according to type
* @param {Object} resolved npa metadata
* @param {String} depVersion semver
* @param {String} savePrefix npm_config_save_prefix
*/
updateLocalDependency(resolved, depVersion, savePrefix) {
const depName = resolved.name;
// first, try runtime dependencies
let depCollection = this.dependencies;
// try optionalDependencies if that didn't work
if (!depCollection || !depCollection[depName]) {
depCollection = this.optionalDependencies;
}
// fall back to devDependencies
if (!depCollection || !depCollection[depName]) {
depCollection = this.devDependencies;
}
if (resolved.registry || resolved.type === "directory") {
// a version (1.2.3) OR range (^1.2.3) OR directory (file:../foo-pkg)
depCollection[depName] = `${savePrefix}${depVersion}`;
} else if (resolved.gitCommittish) {
// a git url with matching committish (#v1.2.3 or #1.2.3)
const [tagPrefix] = /^\D*/.exec(resolved.gitCommittish);
// update committish
const { hosted } = resolved; // take that, lint!
hosted.committish = `${tagPrefix}${depVersion}`;
// always serialize the full url (identical to previous resolved.saveSpec)
depCollection[depName] = hosted.toString({ noGitPlus: false, noCommittish: false });
} else if (resolved.gitRange) {
// a git url with matching gitRange (#semver:^1.2.3)
const { hosted } = resolved; // take that, lint!
hosted.committish = `semver:${savePrefix}${depVersion}`;
// always serialize the full url (identical to previous resolved.saveSpec)
depCollection[depName] = hosted.toString({ noGitPlus: false, noCommittish: false });
}
}
}
function lazy(ref, dir = ".") {
if (typeof ref === "string") {
const location = path.resolve(path.basename(ref) === "package.json" ? path.dirname(ref) : ref);
const manifest = loadJsonFile.sync(path.join(location, "package.json"));
return new Package(manifest, location);
}
// don't use instanceof because it fails across nested module boundaries
if ("__isLernaPackage" in ref) {
return ref;
}
// assume ref is a json object
return new Package(ref, dir);
}
module.exports = Package;
module.exports.lazy = lazy;