-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
76 lines (62 loc) · 2.12 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
var fs = require('fs');
var path = require('path');
function renderContentWithTag(content, tag, options) {
var attrs = options && options.attrs;
var openTag = '<' + tag;
var closeTag = '</' + tag + '>';
for (var attr in attrs) {
if (attrs.hasOwnProperty(attr)) {
openTag += ' ' + attr + '="' + attrs[attr] + '"';
}
}
openTag += '>';
return [openTag, content, closeTag].join('\n');
}
function InlineContentRenderer(project) {
this.name = 'ember-cli-inline-content';
this.project = project;
}
InlineContentRenderer.prototype.included = function(app) {
this.options = app.options || {};
};
InlineContentRenderer.prototype.contentFor = function(type, config) {
var inlineContent = this.options.inlineContent;
var inlineContentForType = inlineContent && inlineContent[type];
var contentOptions, filePath, content;
if(inlineContentForType) {
contentOptions = ('object' === typeof inlineContentForType) && inlineContentForType;
if (contentOptions && contentOptions.enabled !== undefined && Boolean(contentOptions.enabled) === false) {
return;
}
if (contentOptions && contentOptions.content !== undefined) {
content = contentOptions.content;
} else {
filePath = contentOptions && contentOptions.file || inlineContentForType;
content = this.readFile(filePath);
}
if ('function' === typeof contentOptions.postProcess) {
content = contentOptions.postProcess(content);
}
if (filePath) {
switch (path.extname(filePath)) {
case '.js':
return renderContentWithTag(content, 'script', contentOptions);
case '.css':
return renderContentWithTag(content, 'style', contentOptions);
}
}
return content;
}
};
InlineContentRenderer.prototype.readFile = function(filePath) {
if (!filePath) {
return console.log(this.name + ' error: file path not defined');
}
var fullPath = path.join(this.project.root, filePath);
try {
return fs.readFileSync(fullPath, 'utf8');
} catch(e){
return console.log(this.name + ' error: file not found: ' + fullPath);
}
};
module.exports = InlineContentRenderer;