-
Notifications
You must be signed in to change notification settings - Fork 28
/
Gruntfile.js
executable file
·135 lines (105 loc) · 2.94 KB
/
Gruntfile.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
module.exports = function(grunt) {
'use strict';
//------------------------------------------------------------------------------
// UUCSS task
// TODO: this should become it's own module and published on npm
//------------------------------------------------------------------------------
function keys(o) {
var a = [];
for (var i in o) {
if (o.hasOwnProperty(i) !== true) continue;
a.push(i);
}
return a;
}
function arrToObj(arr) {
var o = {};
for (var i = 0; i < arr.length; i++) {
o[ arr[i] ] = null;
}
return o;
}
function difference(arr1, arr2) {
var o1 = arrToObj(arr1);
var o2 = arrToObj(arr2);
var delta = [];
for (var i in o1) {
if (o1.hasOwnProperty(i) !== true) continue;
if (o2.hasOwnProperty(i) !== true) {
delta.push(i)
}
}
for (var i in o2) {
if (o2.hasOwnProperty(i) !== true) continue;
if (o1.hasOwnProperty(i) !== true) {
delta.push(i)
}
}
return delta.sort();
}
// UUCSS Class Names must contain at least one letter and one number
function hasNumbersAndLetters(str) {
if (str.search(/\d/) === -1) {
return false;
}
if (str.search(/[a-z]/) === -1) {
return false;
}
return true;
}
// returns an array of unique UUCSS classes from a file
function extractUUCSSClasses(filename, pattern) {
if (! pattern) {
pattern = /([a-z0-9]+-){1,}([abcdef0-9]){5}/g;
}
var fileContents = grunt.file.read(filename);
var classes = {};
var matches = fileContents.match(pattern);
if (matches) {
for (var i = 0; i < matches.length; i++) {
var c = matches[i];
if (hasNumbersAndLetters(c) === true) {
classes[c] = null;
}
}
}
return keys(classes);
}
function uucssCount() {
var cssClasses = extractUUCSSClasses("public/css/t3tr0s.min.css");
var jsClasses = extractUUCSSClasses("public/js/client.min.js");
console.log(cssClasses.length + " class names found in css/t3tr0s.min.css");
console.log(jsClasses.length + " class names found in js/client.min.js");
console.log("Classes found in one file but not the other:");
console.log( difference(jsClasses, cssClasses) );
}
//------------------------------------------------------------------------------
// Grunt Config
//------------------------------------------------------------------------------
grunt.initConfig({
// LESS conversion
less: {
options: {
yuicompress: true
},
watch: {
files: {
'public/css/t3tr0s.min.css': 'public/css/main.less'
}
}
},
watch: {
options: {
atBegin: true
},
files: "public/css/*.less",
tasks: ["less:watch"]
}
});
// load tasks from npm
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('uucss-count', uucssCount);
grunt.registerTask('default', ['less']);
// end module.exports
};