11import { execaCommand } from 'execa'
22import fg from 'fast-glob'
33import { exec } from 'node:child_process'
4- import { copyFile , mkdir , mkdtemp , rm , writeFile } from 'node:fs/promises'
4+ import { copyFile , mkdir , mkdtemp , readFile , rm , writeFile } from 'node:fs/promises'
55import { tmpdir } from 'node:os'
66import { dirname , join } from 'node:path'
77import { fileURLToPath } from 'node:url'
@@ -12,9 +12,8 @@ import pLimit from 'p-limit'
1212const SITE_ID = 'ee859ce9-44a7-46be-830b-ead85e445e53'
1313
1414export interface DeployResult {
15- site_name : string
16- deploy_id : string
17- deploy_url : string
15+ deployID : string
16+ url : string
1817 logs : string
1918}
2019
@@ -25,14 +24,22 @@ export interface DeployResult {
2524export const createE2EFixture = async ( fixture : string ) => {
2625 const cwd = await mkdtemp ( join ( tmpdir ( ) , 'netlify-next-runtime-' ) )
2726 let deployID : string
28- const _cleanup = ( ) => cleanup ( cwd , deployID )
27+ let logs : string
28+ const _cleanup = ( failure : boolean = false ) => {
29+ if ( failure ) {
30+ console . log ( logs )
31+ }
32+ // on failures we don't delete the deploy
33+ return cleanup ( cwd , failure === true ? undefined : deployID )
34+ }
2935 try {
3036 const [ packageName ] = await Promise . all ( [ buildAndPackRuntime ( cwd ) , copyFixture ( fixture , cwd ) ] )
3137 await installRuntime ( packageName , cwd )
3238 const result = await deploySite ( cwd )
33- console . log ( `🌍 Deployed Site is live under: ${ result . deploy_url } ` )
34- deployID = result . deploy_id
35- return { cwd, cleanup : _cleanup , deployID : result . deploy_id , url : result . deploy_url }
39+ console . log ( `🌍 Deployed Site is live under: ${ result . url } ` )
40+ deployID = result . deployID
41+ logs = result . logs
42+ return { cwd, cleanup : _cleanup , deployID : result . deployID , url : result . url }
3643 } catch ( error ) {
3744 await _cleanup ( )
3845 throw error
@@ -92,10 +99,18 @@ async function installRuntime(packageName: string, cwd: string): Promise<void> {
9299
93100async function deploySite ( cwd : string ) : Promise < DeployResult > {
94101 console . log ( `🚀 Building and Deploying Site...` )
95- const cmd = `ntl deploy --build --site ${ SITE_ID } --json`
96- const { stdout } = await execaCommand ( cmd , { cwd } )
102+ const outputFile = 'deploy-output.txt'
103+ const cmd = `ntl deploy --build --site ${ SITE_ID } `
104+
105+ await execaCommand ( cmd , { cwd, all : true } ) . pipeAll ?.( join ( cwd , outputFile ) )
106+ const output = await readFile ( join ( cwd , outputFile ) , 'utf-8' )
97107
98- return JSON . parse ( stdout )
108+ const [ url ] = new RegExp ( / h t t p s : .+ r u n t i m e - t e s t i n g \. n e t l i f y \. a p p / gm) . exec ( output ) || [ ]
109+ if ( ! url ) {
110+ throw new Error ( 'Could not extract the URL from the build logs' )
111+ }
112+ const [ deployID ] = new URL ( url ) . host . split ( '--' )
113+ return { url, deployID, logs : output }
99114}
100115
101116async function deleteDeploy ( deploy_id ?: string ) : Promise < void > {
0 commit comments