@@ -4,15 +4,54 @@ const ecstatic = require('ecstatic');
4
4
const fs = require ( 'fs-extra' ) ;
5
5
const glob = require ( 'glob' ) ;
6
6
7
- const APPLITOOLS_CONFIG = 'applitools.config.js' ;
8
- const VISUAL_DIFF = 'visual-diff' ;
7
+ const createEnvFile = async ( { inputs, builtPages } ) => {
8
+ await fs . writeJSON ( `${ __dirname } /cypress.env.json` , {
9
+ SITE_NAME : process . env . SITE_NAME || 'localhost-test' ,
10
+ APPLITOOLS_BROWSERS : JSON . stringify ( inputs . browser ) ,
11
+ APPLITOOLS_FAIL_BUILD_ON_DIFF : inputs . failBuildOnDiff ,
12
+ APPLITOOLS_SERVER_URL : inputs . serverUrl ,
13
+ APPLITOOLS_IGNORE_SELECTOR : inputs . ignoreSelector
14
+ ? inputs . ignoreSelector
15
+ . split ( ',' )
16
+ . map ( ( selector ) => ( { selector : selector . trim ( ) } ) )
17
+ : [ ] ,
18
+ APPLITOOLS_CONCURRENCY : inputs . concurrency ,
19
+ PAGES_TO_CHECK : builtPages ,
20
+ CYPRESS_CACHE_FOLDER : './node_modules/CypressBinary' ,
21
+ } ) ;
22
+ } ;
23
+
24
+ const runCypress = async ( { utils, port } ) => {
25
+ await utils . run (
26
+ 'node' ,
27
+ [ 'cypress.js' , 'run' , '--config' , `baseUrl=http://localhost:${ port } ` ] ,
28
+ { cwd : __dirname } ,
29
+ ) ;
30
+
31
+ return await fs . readJSON ( `${ __dirname } /results.json` ) ;
32
+ } ;
33
+
34
+ const shutdownServer = async ( { server } ) => {
35
+ await new Promise ( ( resolve , reject ) => {
36
+ server . close ( ( err ) => {
37
+ if ( err ) {
38
+ return reject ( err ) ;
39
+ }
40
+
41
+ resolve ( ) ;
42
+ } ) ;
43
+ } ) ;
44
+ } ;
9
45
10
46
module . exports = {
11
47
onPreBuild : async ( { utils } ) => {
12
48
// bail immediately if this isn’t a production build
13
49
if ( process . env . CONTEXT !== 'production' ) return ;
14
50
15
- await utils . run ( 'cypress' , [ 'install' ] , { stdio : 'ignore' } ) ;
51
+ await utils . run ( 'cypress' , [ 'install' ] , {
52
+ stdio : 'ignore' ,
53
+ cwd : __dirname ,
54
+ } ) ;
16
55
} ,
17
56
onPostBuild : async ( { constants : { PUBLISH_DIR } , utils, inputs } ) => {
18
57
// bail immediately if this isn’t a production build
@@ -29,53 +68,13 @@ module.exports = {
29
68
. createServer ( ecstatic ( { root : `${ PUBLISH_DIR } ` } ) )
30
69
. listen ( port ) ;
31
70
32
- const applitoolsConfig = {
33
- showLogs : true ,
34
- } ;
35
-
36
- await Promise . all ( [
37
- fs . writeFile (
38
- APPLITOOLS_CONFIG ,
39
- `module.exports = ${ JSON . stringify ( applitoolsConfig ) } ` ,
40
- 'utf8' ,
41
- ) ,
42
- fs . copy ( `${ __dirname } /template/${ VISUAL_DIFF } ` , VISUAL_DIFF ) ,
43
- ] ) ;
44
-
45
- const cypress = require ( 'cypress' ) ;
46
71
const builtPages = glob
47
72
. sync ( `${ PUBLISH_DIR } /**/*.html` )
48
73
. map ( ( p ) => path . dirname ( p . replace ( PUBLISH_DIR , '' ) ) ) ;
49
74
50
- const results = await cypress . run ( {
51
- configFile : `${ VISUAL_DIFF } /visual-diff.json` ,
52
- config : { baseUrl : `http://localhost:${ port } ` , video : false } ,
53
- env : {
54
- SITE_NAME : process . env . SITE_NAME || 'localhost-test' ,
55
- APPLITOOLS_BROWSERS : JSON . stringify ( inputs . browser ) ,
56
- APPLITOOLS_FAIL_BUILD_ON_DIFF : inputs . failBuildOnDiff ,
57
- APPLITOOLS_SERVER_URL : inputs . serverUrl ,
58
- APPLITOOLS_IGNORE_SELECTOR : inputs . ignoreSelector
59
- ? inputs . ignoreSelector
60
- . split ( ',' )
61
- . map ( ( selector ) => ( { selector : selector . trim ( ) } ) )
62
- : [ ] ,
63
- APPLITOOLS_CONCURRENCY : inputs . concurrency ,
64
- PAGES_TO_CHECK : builtPages ,
65
- CYPRESS_CACHE_FOLDER : './node_modules/CypressBinary' ,
66
- } ,
67
- record : false ,
68
- } ) ;
69
-
70
- await new Promise ( ( resolve , reject ) => {
71
- server . close ( ( err ) => {
72
- if ( err ) {
73
- return reject ( err ) ;
74
- }
75
-
76
- resolve ( ) ;
77
- } ) ;
78
- } ) ;
75
+ await createEnvFile ( { inputs, builtPages } ) ;
76
+ const results = await runCypress ( { utils, port } ) ;
77
+ await shutdownServer ( { server } ) ;
79
78
80
79
if ( results . failures ) {
81
80
utils . build . failPlugin ( `Cypress had a problem` , {
@@ -103,10 +102,12 @@ module.exports = {
103
102
} ,
104
103
onEnd : async ( ) => {
105
104
// cleanup transient files
106
- await Promise . all ( [
107
- fs . remove ( APPLITOOLS_CONFIG ) ,
108
- fs . remove ( VISUAL_DIFF ) ,
109
- fs . remove ( 'cypress' ) ,
110
- ] ) ;
105
+ await Promise . all (
106
+ [
107
+ `${ __dirname } /cypress.env.json` ,
108
+ `${ __dirname } /cypress` ,
109
+ `${ __dirname } /results.json` ,
110
+ ] . map ( ( pathToRemove ) => fs . remove ( pathToRemove ) ) ,
111
+ ) ;
111
112
} ,
112
113
} ;
0 commit comments