11// @ts -check
22const ecstatic = require ( 'ecstatic' )
33const http = require ( 'http' )
4- const execa = require ( 'execa' )
54const debug = require ( 'debug' ) ( 'netlify-plugin-cypress' )
65const debugVerbose = require ( 'debug' ) ( 'netlify-plugin-cypress:verbose' )
76const la = require ( 'lazy-ass' )
@@ -15,15 +14,14 @@ function serveFolder (folder, port) {
1514 return http . createServer ( server ) . listen ( port )
1615}
1716
18- function startServerMaybe ( options = { } ) {
17+ function startServerMaybe ( run , options = { } ) {
1918 const startCommand = options . start
2019 if ( ! startCommand ) {
2120 debug ( 'No start command found' )
2221 return
2322 }
2423
25- const serverProcess = execa ( startCommand , {
26- stdio : 'inherit' ,
24+ const serverProcess = run ( startCommand , {
2725 detached : true ,
2826 shell : true
2927 } )
@@ -35,7 +33,7 @@ function startServerMaybe (options = {}) {
3533 }
3634}
3735
38- async function waitOnMaybe ( options = { } ) {
36+ async function waitOnMaybe ( failPlugin , options = { } ) {
3937 const waitOnUrl = options [ 'wait-on' ]
4038 if ( ! waitOnUrl ) {
4139 debug ( 'no wait-on defined' )
@@ -58,7 +56,7 @@ async function waitOnMaybe (options = {}) {
5856 } catch ( err ) {
5957 debug ( 'pinging %s for %d ms failed' , waitOnUrl , waitTimeoutMs )
6058 debug ( err )
61- throw new Error ( err . message )
59+ failPlugin ( `Pinging ${ waitOnUrl } for ${ waitTimeoutMs } failed` , { error : err } )
6260 }
6361}
6462
@@ -91,22 +89,19 @@ async function runCypressTests (baseUrl, record, spec, group, tag) {
9189 } )
9290}
9391
94- async function onInit ( ) {
92+ async function onInit ( arg ) {
9593 debug ( 'installing Cypress binary just in case' )
96- if ( debug . enabled ) {
97- await execa ( 'npx' , [ 'cypress' , 'install' ] , { stdio : 'inherit' } )
98- } else {
99- await execa ( 'npx' , [ 'cypress' , 'install' ] )
100- }
94+ const runOptions = debug . enabled ? { } : { stdio : 'ignore' }
95+ await arg . utils . run ( 'cypress' , [ 'install' ] , runOptions )
10196}
10297
103- const processCypressResults = ( results , failBuild ) => {
98+ const processCypressResults = ( results , failPlugin ) => {
10499 if ( results . failures ) {
105100 // Cypress failed without even running the tests
106101 console . error ( 'Problem running Cypress' )
107102 console . error ( results . message )
108103
109- return failBuild ( 'Problem running Cypress' , {
104+ return failPlugin ( 'Problem running Cypress' , {
110105 error : new Error ( results . message )
111106 } )
112107 }
@@ -120,13 +115,13 @@ const processCypressResults = (results, failBuild) => {
120115
121116 // results.totalFailed gives total number of failed tests
122117 if ( results . totalFailed ) {
123- return failBuild ( 'Failed Cypress tests' , {
118+ return failPlugin ( 'Failed Cypress tests' , {
124119 error : new Error ( `${ results . totalFailed } test(s) failed` )
125120 } )
126121 }
127122}
128123
129- async function postBuild ( { fullPublishFolder, record, spec, group, tag, failBuild } ) {
124+ async function postBuild ( { fullPublishFolder, record, spec, group, tag, failPlugin } ) {
130125 const port = 8080
131126 const server = serveFolder ( fullPublishFolder , port )
132127 debug ( 'local server listening on port %d' , port )
@@ -145,30 +140,26 @@ async function postBuild({ fullPublishFolder, record, spec, group, tag, failBuil
145140 } )
146141 } )
147142
148- processCypressResults ( results , failBuild )
143+ processCypressResults ( results , failPlugin )
149144}
150145
151146const hasRecordKey = ( ) => typeof process . env . CYPRESS_RECORD_KEY === 'string'
152147
153- module . exports = function cypressPlugin ( pluginConfig ) {
154- debugVerbose ( 'cypressPlugin config %o' , pluginConfig )
155-
156- // here we can grab all input settings to isolate Cypress logic
157- // from reading the inputs
158-
159- return {
160- name : 'cypress netlify plugin' ,
148+ module . exports = {
161149 onInit,
162- preBuild : async ( arg ) => {
150+ onPreBuild : async ( arg ) => {
163151 debug ( 'cypress plugin preBuild inputs %o' , arg . inputs )
164152 const preBuildInputs = arg . inputs && arg . inputs . preBuild
165153 if ( ! preBuildInputs ) {
166154 debug ( 'there are no preBuild inputs' )
167155 return
168156 }
169157
170- const closeServer = startServerMaybe ( preBuildInputs )
171- await waitOnMaybe ( preBuildInputs )
158+ const failPlugin = arg . utils && arg . utils . build && arg . utils . build . failPlugin
159+ la ( is . fn ( failPlugin ) , 'expected failPlugin function inside' , arg . utils )
160+
161+ const closeServer = startServerMaybe ( args . utils . run , preBuildInputs )
162+ await waitOnMaybe ( failPlugin , preBuildInputs )
172163
173164 const baseUrl = preBuildInputs [ 'wait-on' ]
174165 const record = Boolean ( preBuildInputs . record )
@@ -192,47 +183,43 @@ module.exports = function cypressPlugin (pluginConfig) {
192183 closeServer ( )
193184 }
194185
195- const failBuild = arg . utils && arg . utils . build && arg . utils . build . failBuild
196- la ( is . fn ( failBuild ) , 'expected failBuild function inside' , arg . utils )
197-
198- processCypressResults ( results , failBuild )
186+ processCypressResults ( results , failPlugin )
199187 } ,
200188
201- postBuild : async ( arg ) => {
189+ onPostBuild : async ( arg ) => {
202190 debugVerbose ( 'postBuild arg %o' , arg )
203191 debug ( 'cypress plugin postBuild inputs %o' , arg . inputs )
204192
205- const fullPublishFolder = arg . netlifyConfig . build . publish
193+ const fullPublishFolder = arg . constants . PUBLISH_DIR
206194 debug ( 'folder to publish is "%s"' , fullPublishFolder )
207195
208196 // only if the user wants to record the tests and has set the record key
209197 // then we should attempt recording
210- const record = hasRecordKey ( ) && Boolean ( pluginConfig . record )
198+ const record = hasRecordKey ( ) && Boolean ( arg . inputs . record )
211199
212- const spec = pluginConfig . spec
200+ const spec = arg . inputs . spec
213201 let group
214202 let tag
215203 if ( record ) {
216- group = pluginConfig . group || 'postBuild'
204+ group = arg . inputs . group || 'postBuild'
217205
218- if ( pluginConfig . tag ) {
219- tag = pluginConfig . tag
206+ if ( arg . inputs . tag ) {
207+ tag = arg . inputs . tag
220208 } else {
221209 tag = process . env . CONTEXT
222210 }
223211 }
224212
225- const failBuild = arg . utils && arg . utils . build && arg . utils . build . failBuild
226- la ( is . fn ( failBuild ) , 'expected failBuild function inside' , arg . utils )
213+ const failPlugin = arg . utils && arg . utils . build && arg . utils . build . failPlugin
214+ la ( is . fn ( failPlugin ) , 'expected failPlugin function inside' , arg . utils )
227215
228- return postBuild ( {
216+ await postBuild ( {
229217 fullPublishFolder,
230218 record,
231219 spec,
232220 group,
233221 tag,
234- failBuild
222+ failPlugin
235223 } )
236224 }
237- }
238225}
0 commit comments