-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathindex.js
173 lines (149 loc) · 5.09 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
'use strict';
const path = require('path');
const mergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const map = require('broccoli-stew').map;
const writeFile = require('broccoli-file-creator');
module.exports = {
name: 'ember-cli-mirage',
options: {
nodeAssets: {
'@xg-wang/whatwg-fetch': npmAsset({
import: ['dist/fetch.umd.js']
}),
'route-recognizer': npmAsset({
srcDir: 'dist',
import: ['route-recognizer.js'],
vendor: ['route-recognizer.js.map']
}),
'fake-xml-http-request': npmAsset({
import: ['fake_xml_http_request.js']
}),
'pretender': npmAsset({
import: ['pretender.js']
}),
'faker': npmAsset({
import: ['build/build/faker.js']
})
}
},
included() {
let app;
// If the addon has the _findHost() method (in ember-cli >= 2.7.0), we'll just
// use that.
if (typeof this._findHost === 'function') {
app = this._findHost();
} else {
// Otherwise, we'll use this implementation borrowed from the _findHost()
// method in ember-cli.
let current = this;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
}
this.app = app;
this.addonConfig = this.app.project.config(app.env)['ember-cli-mirage'] || {};
this.addonBuildConfig = this.app.options['ember-cli-mirage'] || {};
// Call super after initializing config so we can use _shouldIncludeFiles for the node assets
this._super.included.apply(this, arguments);
if (this.addonBuildConfig.directory) {
this.mirageDirectory = this.addonBuildConfig.directory;
} else if (this.addonConfig.directory) {
this.mirageDirectory = this.addonConfig.directory;
} else if (app.project.pkg['ember-addon'] && app.project.pkg['ember-addon'].configPath) {
this.mirageDirectory = path.resolve(app.project.root, path.join('tests', 'dummy', 'mirage'));
} else {
this.mirageDirectory = path.join(this.app.project.root, '/mirage');
}
if (this._shouldIncludeFiles()) {
app.import('vendor/ember-cli-mirage/pretender-shim.js', {
type: 'vendor',
exports: { 'pretender': ['default'] }
});
}
},
blueprintsPath() {
return path.join(__dirname, 'blueprints');
},
treeFor(name) {
let tree;
let shouldIncludeFiles = this._shouldIncludeFiles();
if (!shouldIncludeFiles && name === 'app') {
// Include a noop initializer, even if Mirage is excluded from the build
tree = writeFile('initializers/ember-cli-mirage.js', `
export default {
name: 'ember-cli-mirage',
initialize() {}
};
`);
} else if (shouldIncludeFiles) {
tree = this._super.treeFor.apply(this, arguments);
}
return tree;
},
_lintMirageTree(mirageTree) {
let lintedMirageTrees;
// _eachProjectAddonInvoke was added in [email protected]
// this conditional can be removed when we no longer support
// versions older than 2.5.0
if (this._eachProjectAddonInvoke) {
lintedMirageTrees = this._eachProjectAddonInvoke('lintTree', ['mirage', mirageTree]);
} else {
lintedMirageTrees = this.project.addons.map(function(addon) {
if (addon.lintTree) {
return addon.lintTree('mirage', mirageTree);
}
}).filter(Boolean);
}
let lintedMirage = mergeTrees(lintedMirageTrees, {
overwrite: true,
annotation: 'TreeMerger (mirage-lint)'
});
return new Funnel(lintedMirage, {
destDir: 'tests/mirage/'
});
},
treeForApp(appTree) {
let trees = [ appTree ];
let mirageFilesTree = new Funnel(this.mirageDirectory, {
destDir: 'mirage'
});
trees.push(mirageFilesTree);
if (this.hintingEnabled()) {
trees.push(this._lintMirageTree(mirageFilesTree));
}
return mergeTrees(trees);
},
_shouldIncludeFiles() {
if (process.env.EMBER_CLI_FASTBOOT) {
return false;
}
let environment = this.app.env;
let enabledInProd = environment === 'production' && this.addonConfig.enabled;
let explicitExcludeFiles = this.addonConfig.excludeFilesFromBuild;
if (enabledInProd && explicitExcludeFiles) {
throw new Error('Mirage was explicitly enabled in production, but its files were excluded '
+ 'from the build. Please, use only ENV[\'ember-cli-mirage\'].enabled in '
+ 'production environment.');
}
return enabledInProd || (environment && environment !== 'production' && explicitExcludeFiles !== true);
}
};
function npmAsset(options = {}) {
let defaultOptions = {
// guard against usage in FastBoot 1.0, where process.env.EMBER_CLI_FASTBOOT is not available
_processTree(input) {
return map(input, content => `if (typeof FastBoot !== 'undefined') { ${content} }`);
}
};
let assetOptions = Object.assign(defaultOptions, options);
return function() {
let finalOptions = Object.assign(
assetOptions,
{
enabled: this._shouldIncludeFiles()
}
);
return finalOptions;
};
}