-
Notifications
You must be signed in to change notification settings - Fork 6
/
npm.js
98 lines (87 loc) · 2.78 KB
/
npm.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
"format cjs";
var utils = require('./npm-utils');
var convert = require("./npm-convert");
var crawl = require('./npm-crawl');
var npmLoad = require("./npm-load");
var isNode = typeof process === "object" &&
{}.toString.call(process) === "[object process]";
/**
* @function translate
* @description Convert the package.json file into a System.config call.
* @signature `translate(load)`
* @param {Object} load Load object
* @return {Promise} a promise to resolve with the load's new source.
*/
exports.translate = function(load){
var loader = this;
// This could be an empty string if the fetch failed.
if(load.source == "") {
return "define([]);";
}
var resavePackageInfo = isNode && loader.isEnv &&
!loader.isEnv("production");
var prevPackages = loader.npmContext && loader.npmContext.pkgInfo;
var context = {
packages: [],
pkgInfo: [],
loader: this,
// places we load package.jsons from
paths: {},
// paths that are currently be loaded
loadingPaths: {},
versions: {},
fetchCache: {},
deferredConversions: {},
npmLoad: npmLoad,
crawl: crawl,
convert: convert,
resavePackageInfo: resavePackageInfo,
forwardSlashMap: {},
// default file structure for npm 3 and higher
isFlatFileStructure: true
};
this.npmContext = context;
var pkg = {origFileUrl: load.address, fileUrl: utils.relativeURI(loader.baseURL, load.address)};
crawl.processPkgSource(context, pkg, load.source);
var pkgVersion = context.versions[pkg.name] = {};
pkgVersion[pkg.version] = context.versions.__default = pkg;
// backwards compatible for < npm 3
var steal = utils.pkg.config(pkg) || {};
if(steal.npmAlgorithm === "nested"){
context.isFlatFileStructure = false;
}else{
steal.npmAlgorithm = "flat";
}
pkg.steal = steal;
return crawl.root(context, pkg, true).then(function(){
// clean up packages so everything is unique
var names = {};
var packages = context.pkgInfo;
utils.forEach(context.packages, function(pkg, index){
if(!packages[pkg.name+"@"+pkg.version]) {
if(pkg.browser){
delete pkg.browser.transform;
}
pkg = utils.json.transform(loader, load, pkg);
packages.push({
name: pkg.name,
version: pkg.version,
fileUrl: utils.path.isRelative(pkg.fileUrl) ?
pkg.fileUrl :
utils.relativeURI(context.loader.baseURL, pkg.fileUrl),
main: pkg.main,
steal: convert.steal(context, pkg, pkg.steal, index === 0),
globalBrowser: convert.browser(pkg, pkg.globalBrowser),
browser: convert.browser(pkg, pkg.browser || pkg.browserify),
jspm: convert.jspm(pkg, pkg.jspm),
jam: convert.jspm(pkg, pkg.jam),
resolutions: {}
});
packages[pkg.name+"@"+pkg.version] = true;
}
});
npmLoad.addExistingPackages(context, prevPackages);
var source = npmLoad.makeSource(context, pkg);
return source;
});
};