File size: 5,293 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { config } from 'dotenv'

import execa from 'execa'
import path from 'path'
import url from 'url'
import { generatePackageJson } from './generate-package-json.js'
import { Listr } from 'listr2'
import { forceCrash } from './bench.js'
import { red } from '../../packages/next/dist/lib/picocolors.js'
import { resetProject } from '../../scripts/reset-project.mjs'

config()

export const TEST_PROJECT_NAME = process.env.VERCEL_TEST_PROJECT_NAME
const ORIGIN_PROJECT_NAME = TEST_PROJECT_NAME + '-origin'
const HEAD_PROJECT_NAME = TEST_PROJECT_NAME + '-head'

const TEST_TEAM_NAME = process.env.VERCEL_TEST_TEAM
const TEST_TOKEN = process.env.VERCEL_TEST_TOKEN
const VERCEL_EDGE_FUNCTIONS_BRIDGE_PKG =
  process.env.VERCEL_EDGE_FUNCTIONS_BRIDGE_PKG
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

const appFolder = path.join(__dirname, 'benchmark-app')
const originAppFolder = path.join(__dirname, 'benchmark-app-origin')
const headAppFolder = path.join(__dirname, 'benchmark-app-head')

export async function generateProjects() {
  const { originUrl, headUrl } = await new Listr(
    [
      {
        title: 'Origin project',
        task: (ctx, task) =>
          task.newListr(
            (parent) => [
              {
                title: 'Resetting project',
                task: async () => {
                  await resetProject({
                    teamId: TEST_TEAM_NAME,
                    projectName: ORIGIN_PROJECT_NAME,
                    disableDeploymentProtection: true,
                  })
                },
              },
              {
                title: 'copying app',
                task: async () => {
                  await execa('cp', ['-f', '-R', appFolder, originAppFolder])
                },
              },
              {
                title: 'Set Next.js version in package.json',
                task: async () => {
                  await generatePackageJson(originAppFolder)
                },
              },
              {
                title: 'deploying project',
                task: async () => {
                  const url = await deployProject(
                    ORIGIN_PROJECT_NAME,
                    originAppFolder
                  )
                  ctx.originUrl = url
                },
              },
            ],
            { concurrent: false }
          ),
      },
      {
        title: 'Head project',
        task: (ctx, task) =>
          task.newListr(
            (parent) => [
              {
                title: 'Resetting project',
                task: async () => {
                  await resetProject({
                    teamId: TEST_TEAM_NAME,
                    projectName: HEAD_PROJECT_NAME,
                    disableDeploymentProtection: true,
                  })
                },
              },
              {
                title: 'copying app',
                task: async () => {
                  await execa('cp', ['-f', '-R', appFolder, headAppFolder])
                },
              },
              {
                title: 'pack local Next.js version',
                task: async () => {
                  await generatePackageJson(headAppFolder, true)
                },
              },
              {
                title: 'deploying project',
                task: async () => {
                  const url = await deployProject(
                    HEAD_PROJECT_NAME,
                    headAppFolder
                  )
                  ctx.headUrl = url
                },
              },
            ],
            { concurrent: false }
          ),
      },
    ],
    { concurrent: true }
  ).run()

  return [originUrl, headUrl]
}

export async function cleanupProjectFolders() {
  await Promise.all([
    execa('rm', ['-rf', originAppFolder]),
    execa('rm', ['-rf', headAppFolder]),
  ])
}

export async function deployProject(projectName, appFolder) {
  try {
    const vercelFlags = ['--scope', TEST_TEAM_NAME]
    const vercelEnv = { ...process.env, TOKEN: TEST_TOKEN }

    // link the project
    const linkRes = await execa(
      'vercel',
      ['link', '-p', projectName, '--confirm', ...vercelFlags],
      {
        cwd: appFolder,
        env: vercelEnv,
      }
    )

    if (linkRes.exitCode !== 0) {
      throw new Error(
        `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
      )
    }

    const deployRes = await execa(
      'vercel',
      [
        'deploy',
        '--build-env',
        'NEXT_PRIVATE_TEST_MODE=1',
        '--build-env',
        'NEXT_TELEMETRY_DISABLED=1',
        ...(VERCEL_EDGE_FUNCTIONS_BRIDGE_PKG
          ? [
              '--build-env',
              `VERCEL_EDGE_FUNCTIONS_BRIDGE_PKG=${VERCEL_EDGE_FUNCTIONS_BRIDGE_PKG}`,
            ]
          : []),
        '--force',
        ...vercelFlags,
        ...(forceCrash ? ['--env', 'CRASH_FUNCTION=1'] : []),
      ],
      {
        cwd: appFolder,
        env: vercelEnv,
      }
    )

    if (deployRes.exitCode !== 0) {
      throw new Error(
        `Failed to deploy project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
      )
    }

    return deployRes.stdout
  } catch (err) {
    console.log(red('Deployment failed: ', err))
    throw err
  }
}