forked from ember-cli/ember-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-info.js
414 lines (352 loc) · 13.4 KB
/
package-info.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
'use strict';
const path = require('path');
const ErrorList = require('./error-list');
const Errors = require('./errors');
const AddonInfo = require('../addon-info');
const isAddon = require('../../utilities/is-addon');
const logger = require('heimdalljs-logger')('ember-cli:package-info-cache:package-info');
function lexicographically(a, b) {
const aIsString = typeof a.name === 'string';
const bIsString = typeof b.name === 'string';
if (aIsString && bIsString) {
return a.name.localeCompare(b.name);
} else if (aIsString) {
return -1;
} else if (bIsString) {
return 1;
} else {
return 0;
}
}
function pushUnique(array, entry) {
const index = array.indexOf(entry);
if (index > -1) {
// the entry already exists in the array, but since the presedence between
// addons is "last right wins". We first remove the duplicate entry, and
// append it to the end of the array.
array.splice(index, 1);
}
// At this point, the entry is not in the array. So we must append it.
array.push(entry);
// All this ensures:
// pushUnique([a1,a2,a3], a1)
// results in:
//
// [a2, a3, a1]
//
// which results in the most "least surprising" addon ordering.
}
/**
* Class that stores information about a single package (directory tree with
* a package.json and other data, like and Addon or Project.) It is one of the
* two types of entries in a PackageInfoCache. It is only created by the
* PackageInfoCache.
*
* @public
* @class PackageInfo
*/
class PackageInfo {
constructor(pkgObj, realPath, cache, isRoot = false) {
this.pkg = pkgObj;
this.pkg['ember-addon'] = this.pkg['ember-addon'] || {};
this.realPath = realPath;
this.cache = cache;
this.errors = new ErrorList();
// other fields that will be set as needed. For JIT we'll define
// them here.
this.addonMainPath = undefined; // addons only
this.inRepoAddons = undefined; // (list of PackageInfo - both)
this.internalAddons = undefined; // (list of PackageInfo - project only)
this.cliInfo = undefined; // (PackageInfo - project only)
this.dependencyPackages = undefined; // (obj keyed by dependency name: PackageInfo)
// NOTE: ALL dependencies, not just addons
this.devDependencyPackages = undefined; // (obj keyed by devDependency name: PackageInfo)
// NOTE: these are ALL dependencies, not just addons
this.nodeModules = undefined; // (NodeModulesList, set only if pkg contains node_modules)
// flag indicating that the packageInfo is considered valid. This will
// be true as long as we have a valid directory and our package.json file
// is okay and, if we're an ember addon, that we have a valid 'main' file.
// Missing dependencies will not be considered an error, since they may
// not actually be used.
this.valid = true;
this.mayHaveAddons = isRoot || this.isAddon(); // mayHaveAddons used in index.js
this._hasDumpedInvalidAddonPackages = false;
}
// Make various fields of the pkg object available.
get name() {
return this.pkg.name;
}
/**
* Given error data, add an ErrorEntry to the ErrorList for this object.
* This is used by the _readPackage and _readNodeModulesList methods. It
* should not be called otherwise.
*
* @protected
* @method addError
* @param {String} errorType one of the Errors.ERROR_* constants.
* @param {Object} errorData any error data relevant to the type of error
* being created. See showErrors().
*/
addError(errorType, errorData) {
this.errors.addError(errorType, errorData);
}
/**
* Indicate if there are any errors in the ErrorList for this package. Note that this does
* NOT indicate if there are any errors in the objects referred to by this package (e.g.,
* internal addons or dependencies).
*
* @public
* @method hasErrors
* @param {boolean} true if there are errors in the ErrorList, else false.
*/
hasErrors() {
return this.errors.hasErrors();
}
/**
* Add a reference to an in-repo addon PackageInfo object.
*
* @protected
* @method addInRepoAddon
* @param {PackageInfo} inRepoAddonPkg the PackageInfo for the in-repo addon
* @return null
*/
addInRepoAddon(inRepoAddonPkg) {
if (!this.inRepoAddons) {
this.inRepoAddons = [];
}
this.inRepoAddons.push(inRepoAddonPkg);
}
/**
* Add a reference to an internal addon PackageInfo object.
* "internal" addons (note: not in-repo addons) only exist in
* Projects, not other packages. Since the cache is loaded from
* 'loadProject', this can be done appropriately.
*
* @protected
* @method addInternalAddon
* @param {PackageInfo} internalAddonPkg the PackageInfo for the internal addon
* @return null
*/
addInternalAddon(internalAddonPkg) {
if (!this.internalAddons) {
this.internalAddons = [];
}
this.internalAddons.push(internalAddonPkg);
}
/**
* For each dependency in the given list, find the corresponding
* PackageInfo object in the cache (going up the file tree if
* necessary, as in the node resolution algorithm). Return a map
* of the dependencyName to PackageInfo object. Caller can then
* store it wherever they like.
*
* Note: this is not to be called until all packages that can be have
* been added to the cache.
*
* Note: this is for ALL dependencies, not just addons. To get just
* addons, filter the result by calling pkgInfo.isAddon().
*
* Note: this is only intended for use from PackageInfoCache._resolveDependencies.
* It is not to be called directly by anything else.
*
* @protected
* @method addDependencies
* @param {PackageInfo} dependencies value of 'dependencies' or 'devDependencies'
* attributes of a package.json.
* @return {Object} a JavaScript object keyed on dependency name/path with
* values the corresponding PackageInfo object from the cache.
*/
addDependencies(dependencies) {
if (!dependencies) {
return null;
}
let dependencyNames = Object.keys(dependencies);
if (dependencyNames.length === 0) {
return null;
}
let packages = Object.create(null);
let missingDependencies = [];
dependencyNames.forEach(dependencyName => {
logger.info('%s: Trying to find dependency %o', this.pkg.name, dependencyName);
let dependencyPackage;
// much of the time the package will have dependencies in
// a node_modules inside it, so check there first because it's
// quicker since we have the reference. Only check externally
// if we don't find it there.
if (this.nodeModules) {
dependencyPackage = this.nodeModules.findPackage(dependencyName);
}
if (!dependencyPackage) {
dependencyPackage = this.cache._findPackage(
dependencyName,
path.dirname(this.realPath)
);
}
if (dependencyPackage) {
packages[dependencyName] = dependencyPackage;
} else {
missingDependencies.push(dependencyName);
}
});
if (missingDependencies.length > 0) {
this.addError(Errors.ERROR_DEPENDENCIES_MISSING, missingDependencies);
}
return packages;
}
isAddon() {
return isAddon(this.pkg.keywords);
}
/**
* Add to a list of child addon PackageInfos for this packageInfo.
*
* @method addPackages
* @param {Array} addonPackageList the list of child addon PackageInfos being constructed from various
* sources in this packageInfo.
* @param {Array | Object} packageInfoList a list or map of PackageInfos being considered
* (e.g., pkgInfo.dependencyPackages) for inclusion in the addonPackageList.
* @param {Function} excludeFn an optional function. If passed in, each child PackageInfo
* will be tested against the function and only included in the package map if the function
* returns a truthy value.
*/
addPackages(addonPackageList, packageInfoList, excludeFn) {
if (!packageInfoList) {
return;
}
let result = [];
if (Array.isArray(packageInfoList)) {
if (excludeFn) {
packageInfoList = packageInfoList.filter(pkgInfo => !excludeFn(pkgInfo));
}
packageInfoList.forEach(pkgInfo => result.push(pkgInfo));
} else {
// We're going to assume we have a map of name to packageInfo
Object.keys(packageInfoList).forEach(name => {
let pkgInfo = packageInfoList[name];
if (!excludeFn || !excludeFn(pkgInfo)) {
result.push(pkgInfo);
}
});
}
result.sort(lexicographically).forEach(pkgInfo => pushUnique(addonPackageList, pkgInfo));
return addonPackageList;
}
/**
* Discover the child addons for this packageInfo, which corresponds to an Addon.
*
* @method discoverAddonAddons
*/
discoverAddonAddons() {
let addonPackageList = [];
this.addPackages(addonPackageList,
this.dependencyPackages, pkgInfo => !pkgInfo.isAddon() || pkgInfo.name === 'ember-cli');
this.addPackages(addonPackageList, this.inRepoAddons);
return addonPackageList;
}
/**
* Discover the child addons for this packageInfo, which corresponds to a Project.
*
* @method discoverProjectAddons
*/
discoverProjectAddons() {
let project = this.project;
let addonPackageList = [];
this.addPackages(addonPackageList, (project.isEmberCLIAddon() ? [this] : null));
this.addPackages(addonPackageList, (this.cliInfo ? this.cliInfo.inRepoAddons : null));
this.addPackages(addonPackageList, this.internalAddons);
this.addPackages(addonPackageList, this.devDependencyPackages, pkgInfo => !pkgInfo.isAddon());
this.addPackages(addonPackageList, this.dependencyPackages, pkgInfo => !pkgInfo.isAddon());
this.addPackages(addonPackageList, this.inRepoAddons);
return addonPackageList;
}
/**
* Given a list of PackageInfo objects, generate the 'addonPackages' object (keyed by
* name, value = AddonInfo instance, for all packages marked 'valid'). This is stored in
* both Addon and Project instances.
*
* @method generateAddonPackages
* @param {Array} addonPackageList the list of child addon PackageInfos to work from
* @param {Function} excludeFn an optional function. If passed in, each child PackageInfo
* will be tested against the function and only included in the package map if the function
* returns a truthy value.
*/
generateAddonPackages(addonPackageList, excludeFn) {
let validPackages = this.getValidPackages(addonPackageList);
let packageMap = Object.create(null);
validPackages.forEach(pkgInfo => {
let addonInfo = new AddonInfo(pkgInfo.name, pkgInfo.realPath, pkgInfo.pkg);
if (!excludeFn || !excludeFn(addonInfo)) {
packageMap[pkgInfo.name] = addonInfo;
}
});
return packageMap;
}
getValidPackages(addonPackageList) {
return addonPackageList.filter(pkgInfo => pkgInfo.valid);
}
getInvalidPackages(addonPackageList) {
return addonPackageList.filter(pkgInfo => !pkgInfo.valid);
}
dumpInvalidAddonPackages(addonPackageList) {
if (this._hasDumpedInvalidAddonPackages) { return; }
this._hasDumpedInvalidAddonPackages = true;
let invalidPackages = this.getInvalidPackages(addonPackageList);
if (invalidPackages.length > 0) {
let typeName = (this.project ? 'project' : 'addon');
logger.info('');
logger.info(`The 'package.json' file for the ${typeName} at ${this.realPath}`);
let relativePath;
if (invalidPackages.length === 1) {
relativePath = path.relative(this.realPath, invalidPackages[0].realPath);
logger.info(` specifies an invalid, malformed or missing addon at relative path '${relativePath}'`);
} else {
logger.info(' specifies invalid, malformed or missing addons at relative paths');
invalidPackages.forEach(packageInfo => {
let relativePath = path.relative(this.realPath, packageInfo.realPath);
logger.info(` '${relativePath}'`);
});
}
}
}
/**
* This is only supposed to be called by the addon instantiation code.
* Also, the assumption here is that this PackageInfo really is for an
* Addon, so we don't need to check each time.
*
* @method getAddonConstructor
* @return {AddonConstructor} an instance of a constructor function for the Addon class
* whose package information is stored in this object.
*/
getAddonConstructor() {
if (this.addonConstructor) {
return this.addonConstructor;
}
// Load the addon.
// TODO: Future work - allow a time budget for loading each addon and warn
// or error for those that take too long.
let module = require(this.addonMainPath);
let mainDir = path.dirname(this.addonMainPath);
let ctor;
if (typeof module === 'function') {
ctor = module;
ctor.prototype.root = ctor.prototype.root || mainDir;
ctor.prototype.pkg = ctor.prototype.pkg || this.pkg;
} else {
const Addon = require('../addon'); // done here because of circular dependency
ctor = Addon.extend(
Object.assign({ root: mainDir, pkg: this.pkg }, module)
);
}
// XXX Probably want to store the timings here in PackageInfo,
// rather than adding _meta_ to the constructor.
// XXX Will also need to remove calls to it from various places.
ctor._meta_ = {
modulePath: this.addonMainPath,
lookupDuration: 0,
initializeIn: 0,
};
return (this.addonConstructor = ctor);
}
}
module.exports = PackageInfo;
module.exports.lexicographically = lexicographically;
module.exports.pushUnique = pushUnique;