Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (84 loc) · 2.85 KB
/
Copy pathindex.js
File metadata and controls
91 lines (84 loc) · 2.85 KB
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
/**
* Copyright IBM Corp. 2018, 2025
* SPDX-License-Identifier: MPL-2.0
*/
'use strict';
const mergeTrees = require('broccoli-merge-trees');
const writeFile = require('broccoli-file-creator');
const readdir = require('recursive-readdir-sync');
const read = require('fs').readFileSync;
module.exports = {
name: require('./package.json').name,
contentFor: function (type, config) {
const name = this.name;
const addon = config[name] || { enabled: false };
if (addon.enabled) {
const cwd = process.cwd();
switch (type) {
case 'body':
const templates = [];
Object.keys(addon.endpoints).forEach(function (key) {
const api = `${addon.endpoints[key]}`;
const absoluteAPI = `${cwd}/${api}`;
readdir(absoluteAPI).map(function (item, i, arr) {
const url = item.replace(cwd, '');
templates.push(
`<script type="text/javascript+template" data-url="${url}">${read(
item
)}</script>`
);
});
});
return templates.join('');
}
}
},
treeForApp: function (appTree) {
const config = this.app.project.config(this.app.env) || {};
const addon = config[this.name] || { enabled: false };
// don't include anything if we aren't enabled
if (!addon.enabled) {
return;
}
return this._super.treeForApp.apply(this, arguments);
},
treeFor: function (name) {
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;
const config = app.project.config(app.env) || {};
const addon = config[this.name] || { enabled: false };
// don't include anything if we aren't enabled
if (!addon.enabled) {
return;
}
// unless we've explicitly set auto-import to false (in order to provide custom functions)
// include an initializer to initialize the shim http client
if (addon['auto-import'] !== false && name === 'app') {
const tree = writeFile(
'instance-initializers/ember-cli-api-double.js',
`
import apiDouble from '@hashicorp/ember-cli-api-double';
apiDouble(JSON.parse('${JSON.stringify(addon)}'));
export default {
name: 'ember-cli-api-double',
initialize: function() {}
};
`
);
return mergeTrees([tree, this._super.treeFor.apply(this, arguments)]);
}
return this._super.treeFor.apply(this, arguments);
},
};