22import execa from 'execa'
33import fs from 'fs-extra'
44import { Span } from 'next/src/trace'
5+ import { tmpdir } from 'node:os'
56import path from 'path'
67import { NextInstance } from './base'
78
@@ -14,6 +15,31 @@ type NetlifyDeployResponse = {
1415 logs : string
1516}
1617
18+ async function packNextRuntimeImpl ( ) {
19+ const runtimePackDir = await fs . mkdtemp ( path . join ( tmpdir ( ) , 'next-runtime-pack' ) )
20+
21+ const { stdout } = await execa (
22+ 'npm' ,
23+ [ 'pack' , '--json' , '--ignore-scripts' , `--pack-destination=${ runtimePackDir } ` ] ,
24+ { cwd : process . env . RUNTIME_DIR || `${ process . cwd ( ) } /../next-runtime` } ,
25+ )
26+ const [ { filename, name } ] = JSON . parse ( stdout )
27+
28+ return {
29+ runtimePackageName : name ,
30+ runtimePackageTarballPath : path . join ( runtimePackDir , filename ) ,
31+ }
32+ }
33+
34+ let packNextRuntimePromise : ReturnType < typeof packNextRuntimeImpl > | null = null
35+ function packNextRuntime ( ) {
36+ if ( ! packNextRuntimePromise ) {
37+ packNextRuntimePromise = packNextRuntimeImpl ( )
38+ }
39+
40+ return packNextRuntimePromise
41+ }
42+
1743export class NextDeployInstance extends NextInstance {
1844 private _cliOutput : string
1945 private _buildId : string
@@ -45,8 +71,10 @@ export class NextDeployInstance extends NextInstance {
4571 await fs . rename ( nodeModules , nodeModulesBak )
4672 }
4773
74+ const { runtimePackageName, runtimePackageTarballPath } = await packNextRuntime ( )
75+
4876 // install dependencies
49- await execa ( 'npm' , [ 'i' , '--legacy-peer-deps' ] , {
77+ await execa ( 'npm' , [ 'i' , runtimePackageTarballPath , '--legacy-peer-deps' ] , {
5078 cwd : this . testDir ,
5179 stdio : 'inherit' ,
5280 } )
@@ -68,10 +96,7 @@ export class NextDeployInstance extends NextInstance {
6896 publish = ".next"
6997
7098 [[plugins]]
71- package = "${ path . relative (
72- this . testDir ,
73- process . env . RUNTIME_DIR || `${ process . cwd ( ) } /../next-runtime` ,
74- ) } "
99+ package = "${ runtimePackageName } "
75100 `
76101
77102 await fs . writeFile ( path . join ( this . testDir , 'netlify.toml' ) , toml )
0 commit comments