-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
73 lines (68 loc) · 2.23 KB
/
index.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
var htmlparser = require("htmlparser2"),
fs = require("fs"),
path = require("path"),
vow = require('vow'),
promisify = require('vow-node').promisify,
classes = [],
stat = promisify(fs.stat);
/**
* Returns unique CSS class names from html string in source file order.
* @param {string} html
* @returns {string[]} class names
*/
function getClasses(html) {
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(attribs.class){
attribs.class.split(' ').forEach(function(className) {
(classes.indexOf(className) === -1) && classes.push(className);
});
}
},
}, {decodeEntities: true});
parser.write(html);
parser.end();
return classes;
}
/**
* Returns file names from CSS classes with redifinition levels on fs.
* @param {string[]} blocks
* @param {string[]} levels
* @returns {string[]}
*/
function getFilesFromBlocks(blocks, levels) {
var cssFiles = [],
blockDirs = [];
return vow.all(levels.map(function(level) {
return vow.allResolved(blocks.map(function(blockName) {
var dirName = path.resolve('./' + level + '/' + blockName + '/');
return stat(dirName).then(function (stats) {
if (stats.isDirectory()) {
blockDirs.push(dirName);
var fileName = path.resolve(dirName + '/' + blockName + '.css');
}
if (fs.statSync(fileName).isFile()) {
cssFiles.push(fileName);
}
return stats;
});
}));
})).then(function() {
return { css: cssFiles, dirs: blockDirs };
});
}
/**
* Returns promise with block directories from html file.
* Looking on current file tree with redifinition levels.
* @param {Object} params
* @param {string} params.htmlSrc html file name for parsing
* @param {(string|string[])} params.levels — redefinition levels
*
*/
exports.getFileNames = function(params) {
var htmlSrc = fs.readFileSync(params.htmlSrc, 'utf8'),
blocks = getClasses(htmlSrc);
return getFilesFromBlocks(blocks, params.levels).then(function(files) {
return files;
});
};