forked from chrislopresto/ember-freestyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freestyle-usage-snippet-finder.js
129 lines (120 loc) · 4.15 KB
/
freestyle-usage-snippet-finder.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
/*eslint-env node*/
var Writer = require('broccoli-writer');
var glob = require('glob');
var _Promise = require('es6-promise').Promise;
var fs = require('fs');
var path = require('path');
function naiveMerge(obj1, obj2) {
var obj3 = {};
var prop;
for (prop in obj1) { obj3[prop] = obj1[prop]; }
for (prop in obj2) { obj3[prop] = obj2[prop]; }
return obj3;
}
function findFiles(srcDir) {
return new _Promise(function(resolve, reject) {
glob(path.join(srcDir, "**/*.+(js|hbs|css|scss|less)"), function(err, files) {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}
function extractHbsComponentSnippets(fileContent, componentName, ui) {
var processingSnippet = false;
var insideOpeningTag = false;
var snippetLines = [];
var snippets = {};
var name;
fileContent.split("\n").forEach(function(line) {
if (processingSnippet) {
if (insideOpeningTag) {
// Test for start of closing curlies {{/freestyle-usage
if (new RegExp('\\{\\{\\/' + componentName).test(line)) {
processingSnippet = false;
insideOpeningTag = false;
snippets[name] = snippetLines.join("\n");
snippetLines = [];
} else {
snippetLines.push(line);
}
} else {
insideOpeningTag = line.indexOf('}}') >= 0; // curlies closed }}
}
} else {
// Test for start of opening curlies {{#freestyle-usage 'human-readable-name'
var m = new RegExp('\\{\\{#' + componentName + '\\s+[\'|"](\\S+)[\'|"].*').exec(line);
if (m) {
processingSnippet = true;
insideOpeningTag = m[0].indexOf('}}') >= 0; // curlies closed }}
// Extract snippet positional name param via regex match
name = m[1];
// TODO: Cleanup freestyle-notes vs freestyle-usage disambiguation here
if (name.indexOf('--notes') >= 0) {
if (snippets[name]) {
ui.writeLine('ember-freestyle detected multiple instances of the freestyle-note slug "' + name +'"');
}
} else {
if (snippets[name + '--usage']) {
ui.writeLine('ember-freestyle detected multiple instances of the freestyle-usage slug "' + name +'"');
}
name += '--usage';
}
}
}
});
return snippets;
}
function extractCommentSnippets(fileContent) {
var inside = false;
var content = [];
var output = {};
var name;
fileContent.split("\n").forEach(function(line) {
if (inside) {
if (/\bEND-FREESTYLE-USAGE\b/.test(line)) {
inside = false;
output[name] = content.join("\n");
content = [];
} else {
content.push(line);
}
} else {
var m = /\bBEGIN-FREESTYLE-USAGE\s+(\S+)\b/.exec(line);
if (m) {
inside = true;
name = m[1];
}
}
});
return output;
}
function SnippetFinder(inputTree, ui) {
if (!(this instanceof SnippetFinder)) {
return new SnippetFinder(inputTree, ui);
}
this.inputTree = inputTree;
this.ui = ui;
}
SnippetFinder.prototype = Object.create(Writer.prototype);
SnippetFinder.prototype.constructor = SnippetFinder;
SnippetFinder.prototype.write = function(readTree, destDir) {
var self = this;
return readTree(this.inputTree).then(findFiles).then(function(files) {
files.forEach(function(filename) {
var freestyleUsageSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-usage', self.ui);
var freestyleDynamicSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-dynamic', self.ui);
var freestyleNoteSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-note', self.ui);
var componentSnippets = naiveMerge(naiveMerge(freestyleUsageSnippets, freestyleDynamicSnippets), freestyleNoteSnippets);
var commentSnippets = extractCommentSnippets(fs.readFileSync(filename, 'utf-8'));
var snippets = naiveMerge(componentSnippets, commentSnippets);
for (var name in snippets) {
fs.writeFileSync(path.join(destDir, name)+path.extname(filename),
snippets[name]);
}
});
});
};
module.exports = SnippetFinder;