forked from taigaio/taiga-front
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp-utils.js
176 lines (133 loc) · 4.39 KB
/
gulp-utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2021-present Kaleidos Ventures SL
*/
var exports = module.exports = {};
var fs = require("fs");
var gutil = require('gulp-util');
var Theme = function() {
var defaultTheme = "taiga";
var themesPath = "app/themes";
var tmpThemesPath = "tmp/themes";
var themesSequenceIndex = 0;
var themesSequence = [];
var searchIndex = function(name) {
for(var i = 0; i < themesSequence.length; i++) {
if (themesSequence[i].name === name) {
return i;
}
}
};
var initThemes = function () {
var availableThemes = {};
var files = fs.readdirSync(themesPath);
files.forEach(function(file) {
var path = themesPath + '/' + file;
var tmpPath = tmpThemesPath + '/' + file;
if (fs.statSync(path).isDirectory()) {
availableThemes[file] = {
name: file,
path: path,
customVariables: path + "/variables.scss",
customScss: path + "/custom.scss",
customCss: tmpPath + "/custom.css",
};
}
});
themesSequence.push(availableThemes[defaultTheme]);
for (var theme in availableThemes) {
if (theme !== defaultTheme) {
themesSequence.push(availableThemes[theme]);
}
}
};
initThemes();
var obj = {};
obj.next = function() {
themesSequenceIndex++;
};
obj.set = function(name) {
themesSequenceIndex = searchIndex(name);
};
Object.defineProperty(obj, "current", {
get: function() {
return themesSequence[themesSequenceIndex];
}
});
Object.defineProperty(obj, "availableThemes", {
get: function() {
return themesSequence;
}
});
obj.size = themesSequence.length;
return obj;
};
exports.themes = {
sequence: function() {
return Theme();
}
};
exports.unusedCss = function(options) {
var through = require('through2');
var css = require('css');
var path = require('path');
var content = fs.readFileSync(options.css, "utf8");
var ast = css.parse(content, {});
var files = [];
var validsSelectors = [];
ast.stylesheet.rules.forEach(function(rule) {
if (rule.selectors) {
rule.selectors.forEach(function(selectorRule) {
var selectors = selectorRule.split(" ");
selectors.forEach(function(selector) {
var valid = false;
if (selector.slice(0, 2) === 'tg') {
valid = true;
} else if (selector[0] === '.') {
valid = true;
selector = '.' + selector.split('.')[1]; // '.class1.class2 -> .class1'
}
selector = selector.split('::')[0];
selector = selector.split(':')[0];
if(valid && validsSelectors.indexOf(selector) === -1) {
validsSelectors.push(selector);
}
});
});
}
});
var addFile = function(file, encoding, cb) {
files.push(file);
cb();
};
var searchUnusedCss = function(cb) {
var invalid = [];
validsSelectors.forEach(function(validSelector) {
var finded = false;
files.every(function(file) {
var content = file.contents.toString();
var ext = path.extname(file.path);
var pattern = validSelector;
if (ext === '.html') {
pattern = validSelector.slice(1);
}
if(content.indexOf(pattern) !== -1) {
finded = true;
return false;
}
return true;
});
if (!finded) {
invalid.push(validSelector);
}
});
for(var i = 0; i < invalid.length; i++) {
gutil.log(gutil.colors.magenta(invalid[i]));
}
cb();
};
return through.obj(addFile, searchUnusedCss);
};