skin.js
2.28 KB
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
/**
* Task for creating a skin.dev.less files for each of the skin directories located in the path. It will automatically import
* the used less files based on the JS components found in the importFrom file. It will look for -x-less JSDoc comments and include
* these into the output less file.
*/
var path = require('path');
var fs = require('fs');
module.exports = function(grunt) {
/**
* Compiles a less file with imports for all the specified paths.
*/
function compileLessFile(paths, lessFilePath) {
var lessImportCode = "";
paths.forEach(function(filePath) {
lessImportCode += '@import "' + filePath + '";\n';
});
fs.writeFileSync(lessFilePath, lessImportCode);
}
/**
* Compiles a less source file from all the specified paths.
*/
function compileLessSourceFile(paths, lessFilePath) {
var lessSourceCode = "";
paths.forEach(function(filePath) {
lessSourceCode += "\n" + fs.readFileSync(path.join(path.dirname(lessFilePath), filePath)) + "\n";
});
fs.writeFileSync(lessFilePath, lessSourceCode);
}
/**
* Parses the JS doc comments for -x-less items and include returns them as an array.
*/
function parseLessDocs(filePath) {
var matches, docCommentRegExp = /\/\*\*([\s\S]+?)\*\//g, lessFiles = [];
var source = grunt.file.read(filePath).toString();
for (matches = docCommentRegExp.exec(source); matches; matches = docCommentRegExp.exec(source)) {
var docComment = matches[1];
var lessMatch = /\@\-x\-less\s+(.+)/g.exec(docComment);
if (lessMatch) {
lessFiles.push(lessMatch[1]);
}
}
return lessFiles;
}
grunt.registerMultiTask("skin", "Creates skin less files out of registred UI components.", function() {
var options = grunt.config([this.name, this.target]).options;
fs.readdirSync(options.path).forEach(function(dirName) {
var skinDirPath = path.join(options.path, dirName);
if (fs.statSync(skinDirPath).isDirectory()) {
var lessFiles = options.prepend || [];
if (options.importFrom) {
lessFiles = lessFiles.concat(parseLessDocs(options.importFrom));
}
if (options.append) {
lessFiles = lessFiles.concat(options.append);
}
compileLessFile(lessFiles, path.join(skinDirPath, options.devLess));
compileLessSourceFile(lessFiles, path.join(skinDirPath, options.srcLess));
}
});
});
};