-
Notifications
You must be signed in to change notification settings - Fork 22
/
level-plain.js
executable file
·54 lines (49 loc) · 1.6 KB
/
level-plain.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
var bemNaming = require('bem-naming'),
vow = require('vow'),
path = require('path'),
fs = require('fs');
/**
* Plain scheme for level.
*
* @namespace level-plain
* @see {@link Level}
*/
module.exports = {
buildLevel: function (levelPath, levelBuilder) {
return vow.when(addPlainFiles(levelPath, levelBuilder)).then(function () {
levelBuilder.build();
});
},
buildFilePath: function (levelPath, blockName, elemName, modName, modVal) {
return path.join(levelPath, blockName) +
(elemName ? '__' + elemName : '') +
(modName ? '_' + modName : '') +
(modVal ? '_' + modVal : '');
}
};
function addPlainFiles(directory, levelBuilder) {
filterFiles(fs.readdirSync(directory)).forEach(function (filename) {
var fullname = path.join(directory, filename),
stat = fs.statSync(fullname),
bem;
if (stat.isDirectory()) {
if (filename.indexOf('.') !== -1) {
bem = parseFilename(filename);
levelBuilder.addFile(fullname, bem.block, bem.elem, bem.modName, bem.modVal);
} else {
addPlainFiles(fullname, levelBuilder);
}
} else {
bem = parseFilename(filename);
levelBuilder.addFile(fullname, bem.block, bem.elem, bem.modName, bem.modVal);
}
});
}
function filterFiles(filenames) {
return filenames.filter(function (filename) {
return filename.charAt(0) !== '.';
});
}
function parseFilename(filename) {
return bemNaming.parse(filename.split('.')[0]);
}