Skip to content

Commit 4f43470

Browse files
andrebanpaulirish
authored andcommitted
Improve check for used JS features (#544)
* Improve check for used JS features - Moved check to ./lib/environment.js so it can be re-used in different modules - Uses the vm module to check for the spread operator. - Added `process.exit(1)` when the node environment is invalid.
1 parent b438789 commit 4f43470

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

lighthouse-cli/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818

1919
'use strict';
2020

21+
const environment = require('../lighthouse-core/lib/environment.js');
22+
if (!environment.checkNodeCompatibility()) {
23+
console.warn('Compatibility error', 'Lighthouse requires node 5+ or 4 with --harmony');
24+
process.exit(1);
25+
}
26+
2127
const yargs = require('yargs');
22-
const semver = require('semver');
2328
const Printer = require('./printer');
2429
const lighthouse = require('../lighthouse-core');
2530
const log = require('../lighthouse-core/lib/log');
2631

27-
// node 5.x required due to use of ES2015 features, like spread operator
28-
if (semver.lt(process.version, '5.0.0')) {
29-
console.warn('Compatibility error', 'Lighthouse requires node 5+ or 4 with --harmony');
30-
}
31-
3232
const cli = yargs
3333
.help('help')
3434
.version()

lighthouse-core/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717

1818
'use strict';
1919

20-
const semver = require('semver');
20+
const environment = require('../lighthouse-core/lib/environment');
21+
if (!environment.checkNodeCompatibility()) {
22+
console.warn('Compatibility error', 'Lighthouse requires node 5+ or 4 with --harmony');
23+
process.exit(1);
24+
}
25+
2126
const Runner = require('./runner');
2227
const log = require('./lib/log.js');
2328
const ChromeProtocol = require('./gather/drivers/cri.js');
@@ -38,17 +43,14 @@ const Config = require('./config');
3843
*
3944
*/
4045

41-
// node 5.x required due to use of ES2015 features, like spread operator
42-
if (semver.lt(process.version, '5.0.0')) {
43-
log.warn('Compatibility error', 'Lighthouse requires node 5+ or 4 with --harmony');
44-
}
45-
4646
module.exports = function(url, flags, configJSON) {
4747
return new Promise((resolve, reject) => {
4848
if (!url) {
4949
return reject(new Error('Lighthouse requires a URL'));
5050
}
5151

52+
flags = flags || {};
53+
5254
// set logging preferences, assume quiet
5355
flags.logLevel = flags.logLevel || 'error';
5456
log.setLevel(flags.logLevel);
@@ -59,8 +61,6 @@ module.exports = function(url, flags, configJSON) {
5961
log.warn('Lighthouse', 'Performance stats will be skewed redirecting from HTTP to HTTPS.');
6062
}
6163

62-
flags = flags || {};
63-
6464
// Use ConfigParser to generate a valid config file
6565
const config = new Config(configJSON, flags.auditWhitelist);
6666

lighthouse-core/lib/environment.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
'use strict';
18+
19+
const semver = require('semver');
20+
const vm = require('vm');
21+
22+
function checkNodeCompatibility() {
23+
// node 5.x required due to use of ES2015 features, like spread operator
24+
if (semver.gte(process.version, '5.0.0')) {
25+
return true;
26+
}
27+
28+
try {
29+
// Test for the availability of the spread operator.
30+
new vm.Script('Math.max(...[ 5, 10 ])').runInThisContext();
31+
return true;
32+
} catch (e) {
33+
return false;
34+
}
35+
}
36+
37+
module.exports = {
38+
checkNodeCompatibility: checkNodeCompatibility
39+
};

0 commit comments

Comments
 (0)