-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (157 loc) · 4.78 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
/* global System */
var stubbed = [],
require = requireWithStubs,
caches = {}, //this could be a WeakMap
originals = {};
function isObject(value){
return Object.prototype.toString.call(value) === '[object Object]';
}
function preserveIfDefined(normalizedName){
var isDefined = System.has(normalizedName);
return isDefined ? preserveDefinition(normalizedName) : Promise.resolve();
}
function preserveDefinition(normalizedName){
return System.import(normalizedName).then(function(originalModule){
originals[normalizedName] = System.get(normalizedName);
return originals[normalizedName];
});
}
function stubSingleModule(name, implementation) {
var isAlreadyStubbed = stubbed.filter(function(def){
return def.name === name;
}).length > 0;
if (isAlreadyStubbed) {
throw new Error('Cannot stub module "' + name + '" twice');
}
stubbed.push({
name: name,
implementation: implementation
});
}
function stub(){
var stubOne = typeof arguments[0] === 'string' && !!arguments[1],
stubMany = isObject(arguments[0]),
map;
if (stubOne){
stubSingleModule(arguments[0], arguments[1]);
return;
}
if (stubMany){
map = arguments[0];
Object.keys(map).forEach(function(key) {
stubSingleModule(key, map[key]);
});
return;
}
throw new Error('stub method expects either an Object as a map of names and default exports, or a single <name, exports> pair as arguments');
}
function fetchDependency(name) {
var load = {
name: name,
metadata: {}
};
if (caches[name]) {
return Promise.resolve(caches[name]);
}
return System.locate(load)
.then(function(address) {
load.address = address;
return System.fetch(load);
}).then(function (source){
load.source = source;
caches[name] = load;
return load;
});
}
function addNormalizedName(stub, i) {
return System.normalize(stub.name).then(function(normalizedName){
stubbed[i].normalizedName = normalizedName;
});
}
function getReplacementModule(originalModule, stub) {
var implementation = stub.implementation;
var moduleDef;
switch (typeof implementation) {
case 'object':
moduleDef = implementation;
break;
case 'function':
moduleDef = Object.keys(implementation).reduce(function (acc, key) {
acc[key] = implementation[key];
return acc;
}, {
default: implementation
});
break;
default:
moduleDef = {
default: implementation
};
}
return System.newModule(moduleDef);
}
function redefineStubs(){
return Promise.all(
stubbed.map(function (stub) {
var normalizedName = stub.normalizedName;
return preserveDefinition(normalizedName).then(function(originalModule){
System.delete(normalizedName);
System.set(normalizedName, getReplacementModule(originalModule, stub));
});
})
);
}
function redefineRequiredModule(normalizedName) {
return preserveIfDefined(normalizedName)
.then(function() {
System.delete(normalizedName)
return fetchDependency(normalizedName);
})
.then(function(load) {
originals[normalizedName] = {
source: load.source
};
return System.define(load.name, load.source);
})
.then(function(){
stubbed.push({
normalizedName: normalizedName
});
return System.get(normalizedName);
});
}
function requireWithStubs(name){
return Promise.all(stubbed.map(addNormalizedName))
.then(redefineStubs)
.then(function () {
return System.normalize(name);
})
.then(redefineRequiredModule)
}
function reset(){
var restoreStubsPromise = stubbed.reduce(function(promise, stub){
var normalizedName = stub.normalizedName;
var original = originals[normalizedName];
if (!original) {
return promise;
}
System.delete(normalizedName);
return promise.then(function(){
if (original.source) {
return System.define(normalizedName, original.source);
} else {
System.set(normalizedName, originals[normalizedName]);
}
});
}, Promise.resolve());
return restoreStubsPromise
.then(function() {
stubbed = [];
});
}
module.exports = {
stub: stub,
requireWithStubs: requireWithStubs,
require: require,
reset: reset
};