forked from bem-node/bem-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
59 lines (47 loc) · 1.36 KB
/
test.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
var fs = require('fs');
function findTargets(path) {
var targets = [],
files = fs.readdirSync(path);
files.forEach(function (filename) {
var fullpath = path + '/' + filename;
if (/test.js$/.test(filename)) {
targets.push(filename)
} else {
if (fs.lstatSync(fullpath).isDirectory()) {
targets = targets.concat(findTargets(fullpath));
}
}
})
return targets;
}
function nameToBemjson(name) {
var match = name.match(/^([a-z\-]+)(?:__([a-z\-]+))?(?:_([a-z\-]+)_([a-z\-]+))?/),
block = match[1],
elem = match[2],
modName = match[3],
modVal = match[4],
result = {
block: block
},
mods;
if (elem) {
result.elem = elem;
}
if (modName && modVal) {
mods = {};
mods[modName] = modVal;
result[elem ? 'elemMods' : 'mods'] = mods;
}
return result;
}
function createPage(target) {
var block = nameToBemjson(target),
pageName = target.replace(/\..*/, ''),
pageDir = 'pages/' + pageName,
source = 'exports.blocks = ' + JSON.stringify([block], null, 4);
fs.mkdir(pageDir, function (err) {
fs.writeFile(pageDir + '/' + pageName + '.bemdecl.js', source);
})
}
findTargets('blocks')
.forEach(createPage);