Debian Codegraph @

dojox/mobile/themes/utils/compile.js

raw symbols SCIP
1 var fs = require("fs");
2 var path = require("path");
3 var less = require("less");
4
5 var themeFolders = ["../android", "../iphone", "../blackberry", "../holodark", "../windows", "../custom", "../ios7"];
6 var commonFolders = ["../common/domButtons", "../common/transitions"];
7
8 var batchQueue = [];
9 var batchIndex = 0;
10 var processProgress = 0;
11
12 themeFolders.forEach(function(folder){
13 batchQueue.push(function(){
14 processFolder(folder, true);
15 processFolder(folder + "/dijit", false);
16 });
17 });
18
19 commonFolders.forEach(function(folder){
20 batchQueue.push(function(){
21 processFolder(folder, false);
22 });
23 });
24
25 batch();
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
28
29 function batch(){
30 if(batchIndex < batchQueue.length){
31 batchQueue[batchIndex]();
32 batchIndex++;
33 }
34 }
35
36 function beginProcess(){
37 processProgress++;
38 }
39 function endProcess(){
40 processProgress--;
41 if (processProgress == 0){
42 batch();
43 }
44 }
45
46 function processFolder(folder, usingCommonSubstitution){
47 var folderFiles = getLessFiles(folder);
48
49 if(usingCommonSubstitution){
50 var commonFiles = getLessFiles("../common");
51 var outputFile;
52 commonFiles.array.forEach(function(commonFile){
53 var themeFile = folderFiles.dic[commonFiles.dic[commonFile]];
54 if(themeFile){
55 // If there is a .less file in the theme folder, use it.
56 outputFile = themeFile.replace(".less", ".css");
57 applyLess(themeFile, null, outputFile);
58 }else{
59 // Otherwise, fall back to the .less file which is in 'common'.
60 var fileName = commonFiles.dic[commonFile];
61 outputFile = folder + "/" + fileName.replace(".less", ".css");
62 // dojox.mobile mirroring support
63 if(fileName.indexOf("_rtl") == -1){
64 applyLess(commonFile, '@import "' + folder + '/variables.less";', outputFile);
65 }else{
66 applyLess(commonFile, '@import "' + folder + '/variables_rtl.less";', outputFile);
67 }
68 }
69 });
70 }else{
71 folderFiles.array.forEach(function(file){
72 applyLess(file, null, file.replace(".less", ".css"));
73 });
74 }
75 }
76
77 function applyLess(file, prependText, outputFile){
78 beginProcess();
79 console.log("compiling:", file);
80
81 var parser = new(less.Parser)({paths: [path.dirname(file)], filename: file, optimization: 1});
82 var lessContent = fs.readFileSync(file, "utf-8");
83
84 if(prependText){
85 lessContent = prependText + lessContent;
86 }
87 parser.parse(lessContent, function(error, tree){
88 if(error){
89 less.writeError(error);
90 process.exit(1);
91 }
92 var fd = fs.openSync(outputFile, "w");
93
94 fs.write(fd, tree.toCSS({compress: false}).replace(/\n/g, "\r\n"), 0, "utf-8", function(f){
95 fs.close(fd);
96 console.log("writing:", outputFile);
97 endProcess();
98 });
99 });
100 }
101
102 function getLessFiles(folder){
103 var filesMap = {};
104 var filesArray = fs.readdirSync(folder);
105 filesArray = filesArray.filter(function(file){
106 return file && /\.less$/.test(file) && !/variables\.less$/.test(file) && !/css3\.less$/.test(file)
107 && !/variables_rtl\.less$/.test(file);
108 });
109
110 filesArray = filesArray.map(function(file){
111 filesMap[file] = folder + "/" + file;
112 filesMap[folder + "/" + file] = file;
113 return filesMap[file];
114 });
115
116 return {array: filesArray, dic: filesMap};
117 }