@@ -4,9 +4,20 @@ import fs from 'fs-extra'
4
4
import { platform } from 'os'
5
5
import { NextInstance } from './base'
6
6
7
+ type DeployResponse = {
8
+ name : string
9
+ site_id : string
10
+ site_name : string
11
+ deploy_id : string
12
+ deploy_url : string
13
+ logs : string
14
+ }
15
+
7
16
export class NextDeployInstance extends NextInstance {
8
17
private _cliOutput : string
9
18
private _buildId : string
19
+ private _deployId : string
20
+ private _netlifySiteId : string
10
21
11
22
public get buildId ( ) {
12
23
return this . _buildId
@@ -28,22 +39,23 @@ export class NextDeployInstance extends NextInstance {
28
39
cwd : this . testDir ,
29
40
stdio : 'inherit' ,
30
41
} )
31
- // ensure Netlify CLI is installed
42
+ // Netlify CLI should be installed, but just making sure
32
43
try {
33
44
const res = await execa ( 'ntl' , [ '--version' ] )
34
45
console . log ( `Using Netlify CLI version:` , res . stdout )
35
46
} catch ( _ ) {
36
- console . log ( `Installing Netlify CLI` )
37
- await execa ( 'npm' , [ 'i' , '-g' , 'netlify-cli@latest' ] , {
38
- stdio : 'inherit' ,
39
- } )
47
+ throw new Error ( `You need to have netlify-cli installed.
48
+
49
+ You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"` )
40
50
}
41
51
42
- const NETLIFY_SITE_ID = process . env . NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
43
52
console . log ( `Deploys site for test: ${ testName } ` )
53
+
54
+ this . _netlifySiteId = process . env . NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
55
+
44
56
try {
45
- const statRes = await execa ( 'ntl' , [ 'status' , '--json' ] , {
46
- env : { NETLIFY_SITE_ID , NODE_ENV : 'production' } ,
57
+ await execa ( 'ntl' , [ 'status' , '--json' ] , {
58
+ env : { NETLIFY_SITE_ID : this . _netlifySiteId , NODE_ENV : 'production' } ,
47
59
} )
48
60
} catch ( err ) {
49
61
if ( err . message . includes ( "You don't appear to be in a folder that is linked to a site" ) ) {
@@ -58,7 +70,7 @@ export class NextDeployInstance extends NextInstance {
58
70
cwd : this . testDir ,
59
71
reject : false ,
60
72
env : {
61
- NETLIFY_SITE_ID ,
73
+ NETLIFY_SITE_ID : this . _netlifySiteId ,
62
74
NODE_ENV : 'production' ,
63
75
DISABLE_IPX : platform ( ) === 'linux' ? undefined : '1' ,
64
76
} ,
@@ -68,8 +80,9 @@ export class NextDeployInstance extends NextInstance {
68
80
throw new Error ( `Failed to deploy project ${ deployRes . stdout } ${ deployRes . stderr } (${ deployRes . exitCode } )` )
69
81
}
70
82
try {
71
- const data = JSON . parse ( deployRes . stdout )
83
+ const data : DeployResponse = JSON . parse ( deployRes . stdout )
72
84
this . _url = data . deploy_url
85
+ this . _deployId = data . deploy_id
73
86
console . log ( `Deployed to ${ this . _url } ` , data )
74
87
this . _parsedUrl = new URL ( this . _url )
75
88
} catch ( err ) {
@@ -89,6 +102,37 @@ export class NextDeployInstance extends NextInstance {
89
102
// no-op as the deployment is created during setup()
90
103
}
91
104
105
+ public async destroy ( ) : Promise < void > {
106
+ if ( this . isDestroyed ) {
107
+ throw new Error ( `Next.js deploy instance already destroyed` )
108
+ }
109
+
110
+ // During setup() the test site is deployed to Netlify
111
+ // Once testing is complete, we should delete the deploy again
112
+
113
+ if ( ! process . env . NEXT_TEST_SKIP_CLEANUP ) {
114
+ console . log ( `Deleting project with deploy_id ${ this . _deployId } ` )
115
+
116
+ const deleteResponse = await execa ( 'ntl' , [ 'api' , 'deleteDeploy' , '--data' , `{ "deploy_id": "${ this . _deployId } " }` ] )
117
+
118
+ if ( deleteResponse . exitCode !== 0 ) {
119
+ throw new Error ( `Failed to delete project ${ deleteResponse . stdout } ${ deleteResponse . stderr } (${ deleteResponse . exitCode } )` )
120
+ }
121
+
122
+ console . log ( `Successfully deleted project with deploy_id ${ this . _deployId } ` )
123
+ }
124
+
125
+ // Code below is copied from the base NextInstance class
126
+
127
+ this . isDestroyed = true
128
+ this . emit ( 'destroy' , [ ] )
129
+
130
+ if ( ! process . env . NEXT_TEST_SKIP_CLEANUP ) {
131
+ await fs . remove ( this . testDir )
132
+ }
133
+ require ( 'console' ) . log ( `destroyed next instance` )
134
+ }
135
+
92
136
public async patchFile ( filename : string , content : string ) : Promise < void > {
93
137
throw new Error ( 'patchFile is not available in deploy test mode' )
94
138
}
0 commit comments