forked from adopted-ember-addons/ember-data-factory-guy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.7 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
/*jshint node: true */
'use strict';
var fs = require('fs');
var path = require('path');
var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
module.exports = {
name: 'ember-data-factory-guy',
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(app);
this.app = app;
this.setupFactoryGuyInclude(app);
if (this.includeFactoryGuyFiles) {
app.import(path.join(app.bowerDirectory, 'jquery-mockjax', 'dist', 'jquery.mockjax.js'));
}
},
setupFactoryGuyInclude: function(app) {
let defaultEnabled = /test|development/.test(app.env);
let defaultSettings = { enabled: defaultEnabled, useScenarios: false };
let userSettings = app.project.config(app.env).factoryGuy || {};
let settings = Object.assign(defaultSettings, userSettings);
if (settings.useScenarios) { settings.enabled = true; }
this.includeFactoryGuyFiles = settings.enabled;
},
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 && name === 'app') {
return;
}
return this._super.treeFor.apply(this, arguments);
}
};