@@ -4,9 +4,20 @@ import fs from 'fs-extra'
44import { platform } from 'os'
55import { NextInstance } from './base'
66
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+
716export class NextDeployInstance extends NextInstance {
817 private _cliOutput : string
918 private _buildId : string
19+ private _deployId : string
20+ private _netlifySiteId : string
1021
1122 public get buildId ( ) {
1223 return this . _buildId
@@ -28,22 +39,23 @@ export class NextDeployInstance extends NextInstance {
2839 cwd : this . testDir ,
2940 stdio : 'inherit' ,
3041 } )
31- // ensure Netlify CLI is installed
42+ // Netlify CLI should be installed, but just making sure
3243 try {
3344 const res = await execa ( 'ntl' , [ '--version' ] )
3445 console . log ( `Using Netlify CLI version:` , res . stdout )
3546 } 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"` )
4050 }
4151
42- const NETLIFY_SITE_ID = process . env . NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
4352 console . log ( `Deploys site for test: ${ testName } ` )
53+
54+ this . _netlifySiteId = process . env . NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
55+
4456 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' } ,
4759 } )
4860 } catch ( err ) {
4961 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 {
5870 cwd : this . testDir ,
5971 reject : false ,
6072 env : {
61- NETLIFY_SITE_ID ,
73+ NETLIFY_SITE_ID : this . _netlifySiteId ,
6274 NODE_ENV : 'production' ,
6375 DISABLE_IPX : platform ( ) === 'linux' ? undefined : '1' ,
6476 } ,
@@ -68,8 +80,9 @@ export class NextDeployInstance extends NextInstance {
6880 throw new Error ( `Failed to deploy project ${ deployRes . stdout } ${ deployRes . stderr } (${ deployRes . exitCode } )` )
6981 }
7082 try {
71- const data = JSON . parse ( deployRes . stdout )
83+ const data : DeployResponse = JSON . parse ( deployRes . stdout )
7284 this . _url = data . deploy_url
85+ this . _deployId = data . deploy_id
7386 console . log ( `Deployed to ${ this . _url } ` , data )
7487 this . _parsedUrl = new URL ( this . _url )
7588 } catch ( err ) {
@@ -89,6 +102,37 @@ export class NextDeployInstance extends NextInstance {
89102 // no-op as the deployment is created during setup()
90103 }
91104
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+
92136 public async patchFile ( filename : string , content : string ) : Promise < void > {
93137 throw new Error ( 'patchFile is not available in deploy test mode' )
94138 }
0 commit comments