-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
277 lines (219 loc) · 6.81 KB
/
gulpfile.js
File metadata and controls
277 lines (219 loc) · 6.81 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var gulp = require('gulp'),
concat = require('gulp-concat'),
fs = require('fs'),
http = require('http'),
karma = require('gulp-karma'),
util = require('util'),
htmlVal = require('html-validator'), // require('w3cjs'),
css = require('css-validator');
var testFiles = [
'tests/spec.js',
'src/test.js' //,
// 'index.html'//,
// //'src/js/included.js'
];
var src = ['http://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/0.6.0/webcomponents.js', 'http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', 'http://cdnjs.cloudflare.com/ajax/libs/qunit/1.16.0/qunit.min.js'];
var len = 0;
src.forEach(function(el, index, array){
var req = http.get(el, function(res){
lastSlash = el.lastIndexOf('/');
name = el.slice(lastSlash)
file = fs.createWriteStream('./src/js/'+name);
var writeit = res.pipe(file);
fs.readdir('./src/js/', function(err, arr){
})
len++;
res.on('end', function(){
})
})
})
gulp.task('karma', function() {
// Be sure to return the stream
return gulp.src(testFiles)
.pipe(karma({
configFile: 'my.config.js', // 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
});
});
gulp.task('js', function(){
gulp.src(['./src/js/*.js'])
.pipe(concat('./allqunit.js'))
.pipe(gulp.dest('./'))
})
gulp.task('watch', function(){
gulp.watch('./src/js/*.js', ['js'])
.on('change', function(e){
console.log(e.type);
})
})
gulp.task('validate', function() {
// Define output file
var file = './validationsResults.txt',
// Setup date to mark log
d = new Date(),
t = d.getFullYear().toString()+d.getDate().toString()+("0"+d.getMonth().toString().slice(-2))+" "+d.getHours().toString()+":"+d.getMinutes().toString()+":"+d.getSeconds().toString()+" START:";
// var htmlOpts = {
// format : 'text',
// validator: 'http://validator.w3.org/check '
// };
// valHTML('./index.html');
// Setup timestamp for current run
write("\r\n"+t);
// Select which HTML files you want check encoding for
// single file
htmlEncoding('index.html');
// multiple files
// setup array of files
var mArr = ['index.html', 'index2.html', './assets/index.php'];
enumerate(mArr, 'encode');
// Determine size of all files in a directory
directorySize('./src/js/'); // Outputs 'Size of ./src/js/ = xxxxbytes'
// Determine size of 1 file in a directory
fileSize('./assets/js/landing.min.js');
// Determine size of each file in a directory
enumerate('./assets/js', 'size' ); // ('directory', ['size'] || ['lines'] || [encode]) size = size of each file || lines = # of lines in each file. i.e. enumerate('./assets/js', 'lines');
// Determine number of lines in a file
lines('./assets/js/landing.min.js');
// Validate css
// Single file
var cssLoc = './src/styles/styles.css',
cssString = fs.readFileSync(cssLoc, 'utf-8');
cssValidate(cssLoc, cssString);
// Multiple files
// var cssStringArr = ['./src/styles/styles.css', './src/styles/lp-styles.min.css'];
// enumerate(cssStringArr, 'css');
// Validate HTML
// Single file
var htmlFile = './index.html';
// htmlValidate(htmlFile);
// Single URL
// htmlValidate('http://livalorx.com/index.php');
// Multiple files
// var htmlFileArr = ['./index.html', './index2.html' ];
// enumerate(htmlFileArr, 'html');
// Multiple URLs
// enumerate(['http://livalorx.com', 'http://livalorx.com/whylivalo.php'], 'html');
// Directory
enumerate('./html/', 'html');
function htmlValidate(file) {
var opts = {
// url : 'http://livalorx.com', // optionally validate a URL
format : 'text' //
};
if (file.indexOf('http') >= 0) {
opts.url = file;
} else {
opts.data = fs.readFileSync( file, 'utf8');
}
work();
function work() {
htmlVal(opts, function(err, data){
if(err) throw err;
// console.log(data);
write("HTML Validation Results for "+file+":\r\n"+ data);
});
}
}
function cssValidate(loc, string) {
css({text: cssString, profile:'css3', warning:'no', output:'text'}, function (err, data) {
if(err) throw err;
console.log(data.errors.length);
var errors = '',
b = ' - ';
data.errors.forEach(function(curr, index, arr){
// console.log(curr);
curr.skippedstring ? mess = curr.skippedstring.slice(1) : mess = curr.message;
errors += '@LINE #: '+curr.line+b+'TYPE: '+curr.errortype+b+'CONTEXT: '+curr.context+b+'Error String: '+mess+'\r\n';
});
error = '\r\n@Line #: '+data.errors.line;
write('CSS Errors in '+loc+': '+data.errors.length+'\r\n'+errors+'\r\n');
// assert.strictEqual(data.validity, true);
// assert.deepEqual(data.errors, []);
// assert.deepEqual(data.warnings, []);
});
}
function htmlEncoding(file) {
fs.readFile(file, 'utf-8', function(err, data){
if (err) throw err;
charAt = data.indexOf('charset')+9;
encoding = data.substr(charAt, 5);
if(encoding !== 'UTF-8') {
console.log("UTF Not Correct");
}
write("Encoding in "+file+" = "+encoding);
})
}
function write(data) {
if(fs.existsSync(file)) {
fs.appendFile(file, data+"\r\n", function(err){
if(err) throw err;
})
} else {
fs.writeFile(file, t+"\r\n"+data+"\r\n")
}
}
function fileSize(file){
// console.log("file", file);
var stat = fs.statSync(file);
write("Size of "+file+" = "+stat.size);
// return stat.size;
}
function directorySize(directory){
var dir = fs.readdirSync(directory),
size = 0;
dir.forEach(function(curr, index, array){
// console.log(curr);
siz = fs.statSync(directory+"/"+curr);
size+=siz.size;
// console.log(siz.size);
// fs.statSync(this);
})
write("Size of "+directory+" = "+size);
// return size+" bytes";
}
function lines(file) {
var d = fs.readFileSync(file, 'utf-8');
fs.readFile(file, 'utf-8', function(err, data){
if(err) throw err;
});
var arr = d.split('\n');
write("# of lines in "+file+" = "+arr.length);
// return arr.length;
}
function enumerate(arr, type) {
var dir = '',
size = 0,
siz = true;
if (typeof arr === 'object') {
dir = arr;
} else {
dir = fs.readdirSync(arr);
}
// console.log(type);
// console.log(siz, " is siz");
dir.forEach(function(curr, index, array){
switch(type) {
case 'size':
fileSize(arr+"/"+curr);
break;
case 'lines':
lines(arr+"/"+curr);
break;
case 'encode':
htmlEncoding(curr);
break;
case 'css':
var cssString = fs.readFileSync(curr, 'utf-8');
cssValidate(curr, cssString);
break;
case 'html':
htmlValidate(curr);
}
})
}
});
gulp.task('default', ['js']);