forked from chrislopresto/ember-freestyle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreestyle-usage-snippet-finder.js
128 lines (119 loc) · 3.96 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
/* globals require, module */
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) { // jshint ignore:line
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 matched = false;
var inside = false;
var content = [];
var output = {};
var name;
fileContent.split("\n").forEach(function(line) {
if (matched) {
if (inside) {
// Test for start of closing curlies {{/
if (new RegExp('\\{\\{\\/' + componentName).test(line)) {
matched = false;
inside = false;
output[name] = content.join("\n");
content = [];
} else {
content.push(line);
}
} else {
inside = line.indexOf('}}') >= 0; // curlies closed }}
}
} else {
// Test for start of opening curlies {{#freestyle-usage 'name'
var m = new RegExp('\\{\\{#' + componentName + '\\s+[\'|"](\\S+)[\'|"].*').exec(line);
if (m) {
matched = true;
inside = m[0].indexOf('}}') >= 0; // curlies closed }}
name = m[1];
// TODO: Cleanup freestyle-notes vs freestyle-usage disambiguation here
if (name.indexOf(':notes') >= 0) {
if (output[name]) {
ui.writeLine('ember-freestyle detected multiple instances of the freestyle-note slug "' + name +'"');
}
} else {
if (output[name + ':usage']) {
ui.writeLine('ember-freestyle detected multiple instances of the freestyle-usage slug "' + name +'"');
}
name += ':usage';
}
}
}
});
return output;
}
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 freestyleNoteSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-note', self.ui);
var componentSnippets = naiveMerge(freestyleUsageSnippets, freestyleNoteSnippets);
var commentSnippets = extractCommentSnippets(fs.readFileSync(filename, 'utf-8'));
var snippets = naiveMerge(componentSnippets, commentSnippets);
for (var name in snippets){
var windowsFriendlyName = name.replace(":notes", "_notes").replace(":usage", "_usage"); // replace : in order to have windows friendly filenames
fs.writeFileSync(path.join(destDir, windowsFriendlyName)+path.extname(filename),
snippets[name]);
}
});
});
};
module.exports = SnippetFinder;