-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtemplate-file-info.js
More file actions
102 lines (80 loc) · 2.26 KB
/
template-file-info.js
File metadata and controls
102 lines (80 loc) · 2.26 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
92
93
94
95
96
97
98
99
100
101
102
var path = require('path');
var fse = require('fs-extra');
var existsSync = require('exists-sync');
var compile = require('htmlbars').compile;
var ClassicFileInfo = require('./classic-file-info');
function buildDetectorPlugin(renderables) {
function Detector(options) {
this.options = options;
this.syntax = null; // set by HTMLBars
}
Detector.prototype.transform = function(ast) {
this.syntax.traverse(ast, {
MustacheStatement: processNode,
BlockStatement: processNode,
SubExpression: processNode
});
return ast;
};
function processNode(node) {
var renderable = node.path.original;
logRenderable(renderable);
processNodeForComponentOrPartialHelper(node);
}
function processNodeForComponentOrPartialHelper(node) {
var nameNode;
switch (node.path.original) {
case 'partial':
case 'component':
nameNode = node.params[0];
break;
default:
return;
}
if (nameNode.type === 'StringLiteral') {
logRenderable(nameNode.value);
}
}
function logRenderable(renderable) {
if (renderables.indexOf(renderable) === -1) {
renderables.push(renderable);
}
}
return Detector;
}
var TemplateFileInfo = ClassicFileInfo.extend({
type: 'TemplateFileInfo',
populate: function() {
this._super.populate.apply(this, arguments);
this.detectRenderableInvocations();
},
detectRenderableInvocations: function() {
this.renderablesInvoked = [];
var fullPath = path.join(this.projectRoot, this.sourceRelativePath);
if (!existsSync(fullPath)) {
return;
}
var fileContents = fse.readFileSync(fullPath, { encoding: 'utf8' });
try {
compile(fileContents, {
plugins: {
ast: [buildDetectorPlugin(this.renderablesInvoked)]
}
});
} catch (e) {
// do nothing
}
for (var i = 0; i < this.renderablesInvoked.length; i++) {
var renderable = this.renderablesInvoked[i];
this.registerRenderableUsage(renderable);
}
},
registerRenderableUsage: function(renderable) {
this._fileInfoCollection.registerRenderableInvocation({
sourceRelativePath: this.sourceRelativePath,
renderable: renderable,
fileInfo: this
});
}
});
module.exports = TemplateFileInfo;