Skip to content

Commit c4ace3b

Browse files
committed
add wait-on parameter preBuild
1 parent 8f1f72f commit c4ace3b

File tree

5 files changed

+226
-1
lines changed

5 files changed

+226
-1
lines changed

netlify.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ publish = "public"
1616
[plugins.inputs.preBuild]
1717
start = 'npm start'
1818
wait-on = 'http://localhost:5000'
19+
wait-on-timeout = '3' # seconds

package-lock.json

+152
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"dependencies": {
1919
"debug": "4.1.1",
2020
"ecstatic": "4.1.2",
21-
"execa": "3.4.0"
21+
"execa": "3.4.0",
22+
"got": "9.6.0"
2223
},
2324
"repository": {
2425
"type": "git",

src/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const http = require('http')
44
const execa = require('execa')
55
const debug = require('debug')('netlify-plugin-cypress')
66
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
7+
const { ping } = require('./utils')
78

89
function serveFolder (folder, port) {
910
const server = ecstatic({
@@ -12,6 +13,32 @@ function serveFolder (folder, port) {
1213
return http.createServer(server).listen(port)
1314
}
1415

16+
async function waitOnMaybe (options = {}) {
17+
const waitOnUrl = options['wait-on']
18+
if (!waitOnUrl) {
19+
debug('no wait-on defined')
20+
return
21+
}
22+
23+
const waitOnTimeout = options['wait-on-timeout'] || '60'
24+
25+
console.log(
26+
'waiting on "%s" with timeout of %s seconds',
27+
waitOnUrl,
28+
waitOnTimeout
29+
)
30+
31+
const waitTimeoutMs = parseFloat(waitOnTimeout) * 1000
32+
33+
try {
34+
await ping(waitOnUrl, waitTimeoutMs)
35+
} catch (err) {
36+
debug('pinging %s for %d ms failed', waitOnUrl, waitTimeoutMs)
37+
debug(err)
38+
throw new Error(err.message)
39+
}
40+
}
41+
1542
async function runCypressTests (baseUrl, record, spec) {
1643
// we will use Cypress via its NPM module API
1744
// https://on.cypress.io/module-api
@@ -91,6 +118,16 @@ module.exports = function cypressPlugin (pluginConfig) {
91118
return {
92119
name: 'cypress netlify plugin',
93120
onInit,
121+
preBuild: async (arg) => {
122+
debug('cypress plugin preBuild inputs %o', arg.inputs)
123+
const preBuildInputs = arg.inputs && arg.inputs.preBuild
124+
if (!preBuildInputs) {
125+
return
126+
}
127+
128+
await waitOnMaybe(preBuildInputs)
129+
},
130+
94131
postBuild: async (arg) => {
95132
debugVerbose('postBuild arg %o', arg)
96133
debug('cypress plugin postBuild inputs %o', arg.inputs)

src/utils.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @ts-check
2+
const got = require('got')
3+
const debug = require('debug')('netlify-plugin-cypress')
4+
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
5+
6+
/**
7+
* A small utility for checking when an URL responds, kind of
8+
* a poor man's https://www.npmjs.com/package/wait-on
9+
*/
10+
const ping = (url, timeout) => {
11+
debug('pinging "%s" for %d ms max', url, timeout)
12+
const start = +new Date()
13+
return got(url, {
14+
retry: {
15+
retries(retry, error) {
16+
const now = +new Date()
17+
debugVerbose(
18+
`${now - start}ms ${error.method} ${error.host} ${
19+
error.code
20+
}`
21+
)
22+
if (now - start > timeout) {
23+
console.error('%s timed out', url)
24+
return 0
25+
}
26+
return 1000
27+
}
28+
}
29+
})
30+
}
31+
32+
module.exports = {
33+
ping
34+
}

0 commit comments

Comments
 (0)