-
Notifications
You must be signed in to change notification settings - Fork 4
/
lightbike.js
executable file
·75 lines (57 loc) · 2.12 KB
/
lightbike.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
#! /usr/bin/env node
var path = require('path');
var _ = require('underscore-node');
var execSync = require('sync-exec');
var fse = require('fs-extra');
var util = require('util');
// lightbike modules
var logStats = require('./lib/reporters/console.js');
var scaffoldStats = require('./lib/util/scaffoldStats.js');
var calcStats = require('./lib/util/calcStats.js');
// capture CLI args
var userArgs = process.argv.slice(2);
var configPath = userArgs[0];
var verbose = userArgs[1] && userArgs[1].match('verbose')
// sanity checks
if (!configPath) {
console.log('Please specify a config file.');
console.log('Example: $ lightbike /some/path/config.json');
console.log('View an example config at ' + __dirname + '/config-example.json');
process.exit(1);
}
if (!execSync('which browsertime').stdout) {
console.log('Browsertime cli not found. Please install browsertime and ensure it is in PATH.');
process.exit(1);
}
// Setup
fse.emptyDirSync(__dirname + '/tmp');
var config = require(path.resolve(configPath));
var startTime = Date.now();
var stats = scaffoldStats(config, startTime, __dirname);
// should be part of options hash
var minWaitTime = 4000; // ms
var WAITSCRIPT = 'return (function(){ if (typeof(LIGHTSTART) === "undefined") window.LIGHTSTART = (new Date).getTime(); if (((new Date).getTime() - LIGHTSTART) > ' + minWaitTime + ') return true; })()';
var logDir = path.resolve(__dirname + '/tmp');
// the magic
_.each(stats, function(stat) {
console.log('Testing ' + stat.base + ' ' + stat.name);
var cmd = [
"browsertime " + stat.url + " -n 1 -viewPort " + stat.browserSize,
" --resultDir " + logDir,
" --logDir " + logDir,
" --output " + stats[stat.key].timings,
" --har " + stats[stat.key].har,
" --waitScript '" + WAITSCRIPT + "'"
].join('');
if (stat.headers) cmd += " --headers " + "'" + stat.headers + "'";
if (stat.block) cmd += " --blacklist " + "'" + stat.block + "'";
// if (verbose) {
// console.log();
// console.log(cmd);
// console.log();
// }
execSync(cmd, 120000);
calcStats(config, stat, stat.url);
logStats(config, stat, verbose);
console.log('');
})