@@ -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 DeployRes = {
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
@@ -39,11 +50,13 @@ export class NextDeployInstance extends NextInstance {
39
50
} )
40
51
}
41
52
42
- const NETLIFY_SITE_ID = process . env . NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
43
53
console . log ( `Deploys site for test: ${ testName } ` )
54
+
55
+ this . _netlifySiteId = process . env . NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
56
+
44
57
try {
45
- const statRes = await execa ( 'ntl' , [ 'status' , '--json' ] , {
46
- env : { NETLIFY_SITE_ID , NODE_ENV : 'production' } ,
58
+ await execa ( 'ntl' , [ 'status' , '--json' ] , {
59
+ env : { NETLIFY_SITE_ID : this . _netlifySiteId , NODE_ENV : 'production' } ,
47
60
} )
48
61
} catch ( err ) {
49
62
if ( err . message . includes ( "You don't appear to be in a folder that is linked to a site" ) ) {
@@ -58,7 +71,7 @@ export class NextDeployInstance extends NextInstance {
58
71
cwd : this . testDir ,
59
72
reject : false ,
60
73
env : {
61
- NETLIFY_SITE_ID ,
74
+ NETLIFY_SITE_ID : this . _netlifySiteId ,
62
75
NODE_ENV : 'production' ,
63
76
DISABLE_IPX : platform ( ) === 'linux' ? undefined : '1' ,
64
77
} ,
@@ -68,8 +81,9 @@ export class NextDeployInstance extends NextInstance {
68
81
throw new Error ( `Failed to deploy project ${ deployRes . stdout } ${ deployRes . stderr } (${ deployRes . exitCode } )` )
69
82
}
70
83
try {
71
- const data = JSON . parse ( deployRes . stdout )
84
+ const data : DeployRes = JSON . parse ( deployRes . stdout )
72
85
this . _url = data . deploy_url
86
+ this . _deployId = data . deploy_id
73
87
console . log ( `Deployed to ${ this . _url } ` , data )
74
88
this . _parsedUrl = new URL ( this . _url )
75
89
} catch ( err ) {
@@ -89,6 +103,37 @@ export class NextDeployInstance extends NextInstance {
89
103
// no-op as the deployment is created during setup()
90
104
}
91
105
106
+ public async destroy ( ) : Promise < void > {
107
+ if ( this . isDestroyed ) {
108
+ throw new Error ( `next instance already destroyed` )
109
+ }
110
+
111
+ // During setup() the test site is deployed to Netlify
112
+ // Once testing is complete, we should delete the deploy again
113
+
114
+ if ( ! process . env . NEXT_TEST_SKIP_CLEANUP ) {
115
+ console . log ( `Deleting project with deploy_id ${ this . _deployId } ` )
116
+
117
+ const deleteRes = await execa ( 'ntl' , [ 'api' , 'deleteDeploy' , '--data' , `{ "deploy_id": "${ this . _deployId } " }` ] )
118
+
119
+ if ( deleteRes . exitCode !== 0 ) {
120
+ throw new Error ( `Failed to delete project ${ deleteRes . stdout } ${ deleteRes . stderr } (${ deleteRes . exitCode } )` )
121
+ }
122
+
123
+ console . log ( `Successfully deleted project with deploy_id ${ this . _deployId } ` )
124
+ }
125
+
126
+ // Code below is copied from the base NextInstance class
127
+
128
+ this . isDestroyed = true
129
+ this . emit ( 'destroy' , [ ] )
130
+
131
+ if ( ! process . env . NEXT_TEST_SKIP_CLEANUP ) {
132
+ await fs . remove ( this . testDir )
133
+ }
134
+ require ( 'console' ) . log ( `destroyed next instance` )
135
+ }
136
+
92
137
public async patchFile ( filename : string , content : string ) : Promise < void > {
93
138
throw new Error ( 'patchFile is not available in deploy test mode' )
94
139
}
0 commit comments