-
Notifications
You must be signed in to change notification settings - Fork 74
/
cli.js
executable file
·104 lines (92 loc) · 3.13 KB
/
cli.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
#!/usr/bin/env node
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE
'use strict';
/* eslint-disable no-logger */
const yargs = require('yargs');
const PWMetrics = require('../lib/index');
const {getConfigFromFile} = require('../lib/utils/fs');
const {getMessageWithPrefix, getMessage} = require('../lib/utils/messages');
const {Logger} = require('../lib/utils/logger');
const logger = Logger.getInstance();
let config;
const cliFlags = yargs
.help('help')
.version(() => require('../package').version)
.showHelpOnFail(false, 'Specify --help for available options')
.wrap(yargs.terminalWidth())
.usage('Usage: $0 <url>')
.command('url', 'URL to test')
.option('json', {
'describe': 'Output as json',
'type': 'boolean',
'default': 'false',
'group': 'Output:'
})
.option('output-path', {
'describe': 'The file path to output the results',
'type': 'string',
'default': 'stdout',
'group': 'Output:'
})
.option('runs', {
'describe': 'Does n runs (eg. 3, 5), and reports the median run\'s numbers.',
'type': 'number',
'default': 1,
})
.option('expectations', {
'describe': 'Expectations from metrics results. Useful for CI. Config required.',
'type': 'boolean',
'default': false
})
.option('submit', {
'describe': 'Submit metric results into google sheets. Config required.',
'type': 'boolean',
'default': false
})
.option('config', {
'describe': 'Path to config file',
'type': 'string'
})
.option('upload', {
'describe': 'Upload trace to Google Drive',
'type': 'boolean',
'default': false
})
.option('view', {
'describe': 'Open trace uploaded to Google Drive in timeline-viewer (https://chromedevtools.github.io/timeline-viewer/)',
'type': 'boolean',
'default': false
})
.option('fail-on-error', {
'describe': 'Exit PWMetrics with an error status code after the first unfilled expectation',
'type': 'boolean',
'default': false
})
.check((argv) => {
// Make sure pwmetrics has been passed a url, either from cli or config fileg()
// Test if flag was explicitly set, yargs default will always assume flag is called, lack of optional support
if (argv.config !== undefined) {
config = argv.config.length ? getConfigFromFile(argv.config) : getConfigFromFile();
}
if (argv._.length === 0 && (config === undefined || !config.url))
throw new Error(getMessageWithPrefix('ERROR', 'NO_URL'));
return true;
})
.epilogue('For more Lighthouse CLI options see https://github.com/GoogleChrome/lighthouse/#lighthouse-cli-options')
.argv;
// Merge options from all sources. Order indicates precedence (last one wins)
const options = Object.assign({}, {flags: cliFlags}, config);
// Get url first from cmd line then from config file.
options.url = cliFlags._[0] || options.url;
if (!options.url || !options.url.length)
throw new Error(getMessage('NO_URL'));
const pwMetrics = new PWMetrics(options.url, options);
pwMetrics.start()
.then(() => {
process.exit(0);
})
.catch(err => {
logger.error(err);
process.exit(1);
});