forked from treosh/lighthouse-ci-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
124 lines (107 loc) · 3.59 KB
/
config.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
const core = require('@actions/core')
const { loadRcFile } = require('@lhci/utils/src/lighthouserc')
const { get } = require('lodash')
const { resolve } = require('path')
exports.getInput = function getInputArgs() {
// fallback to upload.serverBaseUrl + upload.token for previous API support
const serverBaseUrl = core.getInput('serverBaseUrl') || core.getInput('upload.serverBaseUrl')
const serverToken = core.getInput('serverToken') || core.getInput('upload.token')
// Make sure we don't have LHCI xor API token
if (!!serverBaseUrl != !!serverToken) {
// Fail and exit
core.setFailed(`Need both a LHCI server url and an API token.`)
process.exit(1)
}
const temporaryPublicStorage = core.getInput('temporaryPublicStorage') === 'true' ? true : false
if (serverBaseUrl && temporaryPublicStorage) {
core.setFailed(`Both LHCI server and Temporary storage are set, choose one upload method.`)
process.exit(1)
}
let staticDistDir = null
let urls = null
let numberOfRuns = null
// Inspect lighthouserc file for malformations
const configPath = core.getInput('configPath') ? resolve(core.getInput('configPath')) : null
if (configPath) {
const rcFileObj = loadRcFile(configPath)
if (!rcFileObj.ci) {
// Fail and exit
core.setFailed(`Config missing top level 'ci' property`)
process.exit(1)
}
// Check if we have a static-dist-dir
if (rcFileObj.ci.collect) {
if (rcFileObj.ci.collect.url) {
urls = rcFileObj.ci.collect.url
}
if (rcFileObj.ci.collect.staticDistDir) {
staticDistDir = rcFileObj.ci.collect.staticDistDir
}
if (rcFileObj.ci.collect.numberOfRuns) {
numberOfRuns = rcFileObj.ci.collect.numberOfRuns
}
}
}
// Get and interpolate URLs
urls = interpolateProcessIntoUrls(getList('urls')) || urls
// Make sure we have either urls or a static-dist-dir
if (!urls && !staticDistDir) {
// Fail and exit
core.setFailed(`Need either 'urls' in action parameters or a 'static_dist_dir' in lighthouserc file`)
process.exit(1)
}
return {
// collect
urls,
runs: core.getInput('runs') ? parseInt(core.getInput('runs'), 10) : numberOfRuns || 1, // `runs`, check config, and fallback to 1
staticDistDir,
// assert
budgetPath: core.getInput('budgetPath') || '',
configPath,
// upload
serverBaseUrl,
serverToken,
temporaryPublicStorage,
uploadArtifacts: core.getInput('uploadArtifacts') === 'true' ? true : false,
basicAuthUsername: core.getInput('basicAuthUsername') || 'lighthouse',
basicAuthPassword: core.getInput('basicAuthPassword'),
artifactName: core.getInput('artifactName'),
}
}
/**
* Check if the file under `configPath` has `assert` params set.
*
* @param {string | null} configPath
*/
exports.hasAssertConfig = function hasAssertConfig(configPath) {
if (!configPath) return false
const rcFileObj = loadRcFile(configPath)
return Boolean(get(rcFileObj, 'ci.assert'))
}
/**
* Wrapper for core.getInput for a list input.
*
* @param {string} arg
*/
function getList(arg, separator = '\n') {
const input = core.getInput(arg)
if (!input) return []
return input.split(separator).map((url) => url.trim())
}
/**
* Takes a set of URL strings and interpolates
* any declared ENV vars into them
*
* @param {string[]} urls
*/
function interpolateProcessIntoUrls(urls) {
return urls.map((url) => {
if (!url.includes('$')) return url
Object.keys(process.env).forEach((key) => {
if (url.includes(`${key}`)) {
url = url.replace(`$${key}`, `${process.env[key]}`)
}
})
return url
})
}