forked from adopted-ember-addons/ember-data-factory-guy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (69 loc) · 2.5 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
/* eslint-env node */
'use strict';
var fs = require('fs');
var path = require('path');
var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
const map = require('broccoli-stew').map;
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);
},
treeForVendor: function(tree) {
// tree can be undefined.
var trees = tree ? [tree] : [];
if (this.includeFactoryGuyFiles) {
var packagePath = path.dirname(require.resolve('jquery-mockjax'));
var packageTree = new Funnel(this.treeGenerator(packagePath), {
srcDir: '/',
destDir: 'jquery-mockjax'
});
trees.push(packageTree);
}
trees = map(trees, (content) => `if (typeof FastBoot === 'undefined') { ${content} }`);
return mergeTrees(trees);
},
included: function(app) {
this._super.included.apply(this, arguments);
this.app = app;
this.setupFactoryGuyInclude(app);
if (this.includeFactoryGuyFiles) {
app.import(path.join('vendor', 'jquery-mockjax', '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;
// 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);
}
};