-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
198 lines (161 loc) · 5.48 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* jshint node: true */
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var mergeTrees = require('broccoli-merge-trees');
var funnel = require('broccoli-funnel');
var replaceString = require('broccoli-replace');
var assign = require('object-assign');
var colors = require('colors');
var deprecate = require('ember-cli/lib/utilities/deprecate');
var MARKER_PREFIX = 'ember-index';
module.exports = {
name: 'ember-index',
contentFor: function(type, config) {
var contentKey,
strings = {};
if (this.options.enabled && this.options.content) {
this.options.content.forEach(function(content) {
var key = content.key || content.id;
var startMarker = this._getStartMarker(key);
var endMarker = this._getEndMarker(key);
var rootPath = this.app.project.root;
var appPath = this.app.options.trees.app;
var contentFilePath;
if (appPath && typeof appPath.__broccoliGetInfo__ === 'function') {
appPath = appPath.__broccoliGetInfo__();
appPath = appPath && appPath.sourceDirectory
}
if (typeof appPath !== 'string') {
appPath = 'app';
}
if(content.file) {
if (new RegExp('^' + rootPath).test(appPath) || (appPath.indexOf(':\\') !== -1 && appPath.indexOf(appPath) === 0)){
contentFilePath = path.join(appPath, content.file);
} else {
contentFilePath = path.join(rootPath, appPath, content.file);
}
}
this._handleIdDepracation(content);
if (content.file && fs.existsSync(contentFilePath)) {
strings[key] = startMarker + fs.readFileSync(contentFilePath) + endMarker;
} else if(content.string && typeof content.string === 'string') {
strings[key] = startMarker + content.string + endMarker;
} else {
if(content.file) {
console.error(('ember-index addon: Cannot find ' + contentFilePath).red);
} else {
console.error(('ember-index addon: No "file" or "string" property set for this item').red);
}
}
}.bind(this));
}
if (/^ember-index/.test(type)) {
if (type === 'ember-index') {
contentKey = 'default';
} else {
contentKey = type.substring('ember-index-'.length, type.length);
}
}
return strings[contentKey] || '';
},
included: function(app) {
this._super.included.apply(this, arguments);
this.options = assign({}, this._defaultOptions, (app.options['ember-index'] || this.app.project.config(app.env)['ember-index'] || {}));
this.options.content = util.isArray(this.options.content) ? this.options.content : (this.options.content ? [assign({
key: 'default'
}, this.options.content)] : []);
this.validateOptions();
},
postprocessTree: function(type, tree) {
var returnedTree = tree,
renamedIndexTree,
indexTree,
treesToMerge = [],
indexFiles = ['index.html'];
if (this.app.tests) {
indexFiles.push('tests/index.html');
}
if (this.options.enabled && type === 'all') {
if (this.options.output) {
renamedIndexTree = funnel(tree, {
srcDir: '.',
files: ['index.html'],
destDir: this.options.destDir || '.',
getDestinationPath: function() {
return this.options.output;
}.bind(this)
});
}
indexTree = funnel(tree, {
srcDir: '.',
files: indexFiles,
destDir: '.'
});
this.options.content.forEach(function(content) {
var key = content.key || content.id;
var startMarker = this._getStartMarker(key);
var endMarker = this._getEndMarker(key);
var markersRegExp = new RegExp('(' + startMarker + '|' + endMarker + ')', 'g');
var injectedContentRegExp = new RegExp(startMarker + '([\\s\\S])*' + endMarker, 'g');
this._handleIdDepracation(content);
if (this.options.output && renamedIndexTree) {
var file = this.options.destDir ? [this.options.destDir + '/' + this.options.output] : [this.options.output];
renamedIndexTree = replaceString(renamedIndexTree, {
files: file,
patterns: [
{
match: content.includeInOutput ? markersRegExp : injectedContentRegExp,
replacement: ''
}
]
});
}
indexTree = replaceString(indexTree, {
files: indexFiles,
patterns: [
{
match: content.includeInIndexHtml ? markersRegExp : injectedContentRegExp,
replacement: ''
}
]
});
}, this);
treesToMerge.push(tree);
if (this.options.output && renamedIndexTree) {
treesToMerge.push(renamedIndexTree)
}
treesToMerge.push(indexTree);
returnedTree = mergeTrees(treesToMerge, {
overwrite: true
});
}
return returnedTree;
},
validateOptions: function() {
if (this.options.output === 'index.html' && (!this.options.destDir || this.options.destDir === '.')) {
throw new Error('ember-index: Output file name cannot be \'index.html\'');
}
},
_defaultOptions: {
enabled: true,
content: [],
output: null,
},
_startMarkerPrefix: MARKER_PREFIX + '-start-' + new Date().getTime(),
_endMarkerPrefix: MARKER_PREFIX + '-end-' + new Date().getTime(),
_getStartMarker: function(key) {
return this._startMarkerPrefix + '-' + key;
},
_getEndMarker: function(key) {
return this._endMarkerPrefix + '-' + key;
},
_handleIdDepracation: function(content){
deprecate('ember-index: "id" property is depracated when defining multiple content entries. Please use "key" instead.', !!content.id && !this._didShowIdDeprecationWarning);
if (!this._didShowIdDeprecationWarning && !!content.id){
this._didShowIdDeprecationWarning = true;
}
},
_didShowIdDeprecationWarning: false
};