-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
index.js
141 lines (120 loc) · 4.15 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
/* eslint-env node */
'use strict';
var fs = require('fs');
var path = require('path');
var MergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
module.exports = {
name: require('./package').name,
isDevelopingAddon: function () {
return false;
},
// borrowed from ember-cli-pretender
_findPretenderPaths: function () {
if (!this._pretenderPath) {
var resolve = require('resolve');
this._pretenderPath = resolve.sync('pretender');
this._pretenderDir = path.dirname(this._pretenderPath);
this._routeRecognizerPath = resolve.sync('route-recognizer', {
basedir: this._pretenderDir,
});
this._fakeRequestPath = resolve.sync('fake-xml-http-request', {
basedir: this._pretenderDir,
});
}
},
// borrowed from ember-cli-pretender
treeForVendor: function (tree) {
this._findPretenderPaths();
var pretenderTree = new Funnel(this._pretenderDir, {
files: [path.basename(this._pretenderPath)],
destDir: '/pretender',
});
var routeRecognizerFilename = path.basename(this._routeRecognizerPath);
var routeRecognizerTree = new Funnel(
path.dirname(this._routeRecognizerPath),
{
files: [routeRecognizerFilename, routeRecognizerFilename + '.map'],
destDir: '/route-recognizer',
}
);
var fakeRequestTree = new Funnel(path.dirname(this._fakeRequestPath), {
files: [path.basename(this._fakeRequestPath)],
destDir: '/fake-xml-http-request',
});
var trees = [
tree,
pretenderTree,
routeRecognizerTree,
fakeRequestTree,
// tree is not always defined, so filter out if empty
].filter(Boolean);
return new MergeTrees(trees, {
annotation: 'pretender-and-friends: treeForVendor',
});
},
treeForApp: function (appTree) {
var trees = [appTree];
if (this.includeFactoryGuyFiles) {
try {
if (fs.statSync('tests/factories').isDirectory()) {
var factoriesTree = new Funnel('tests/factories', {
destDir: 'tests/factories',
});
trees.push(factoriesTree);
}
} catch (err) {
// do nothing;
}
}
return MergeTrees(trees);
},
included: function (app) {
this._super.included.apply(this, arguments);
this.setupFactoryGuyInclude(app);
if (this.includeFactoryGuyFiles) {
this._findPretenderPaths();
app.import(
'vendor/fake-xml-http-request/' + path.basename(this._fakeRequestPath)
);
app.import(
'vendor/route-recognizer/' + path.basename(this._routeRecognizerPath)
);
app.import('vendor/pretender/' + path.basename(this._pretenderPath));
// this seems like a stupid thing to do, but it is needed in fastboot environment / borrowed it from mirage
// eventually what I should do is not load any factory guy files in fastboot environment,
// but that is a real pain, so for now this will do.
app.import('vendor/pretender-shim.js', {
type: 'vendor',
exports: { pretender: ['default'] },
});
}
},
setupFactoryGuyInclude: function (app) {
let defaultEnabled = /test|development/.test(app.env),
defaultSettings = { enabled: defaultEnabled, useScenarios: false },
userSettings = app.project.config(app.env).factoryGuy || {},
settings = Object.assign(defaultSettings, userSettings);
if (settings.useScenarios) {
settings.enabled = true;
}
this.includeFactoryGuyFiles = settings.enabled;
// Have to be carefull not to exclude factory guy from addon tree
// in development or test env
let trees = /test|development/.test(app.env) ? 'app' : 'app|addon';
this.treeExcludeRegex = new RegExp(trees);
},
treeFor: function (name) {
// Not sure why this is necessary, but this stops the factory guy files
// from being added to app tree. Would have thought that this would have
// happened in treeForApp above, but not the case
if (
!this.includeFactoryGuyFiles &&
this.treeExcludeRegex &&
this.treeExcludeRegex.test(name)
) {
return;
}
return this._super.treeFor.apply(this, arguments);
},
};