|
| 1 | +import * as pulumi from '@pulumi/pulumi' |
| 2 | +import * as fs from 'fs/promises' |
| 3 | +import * as path from 'path' |
| 4 | +import execa = require('execa') |
| 5 | +import { |
| 6 | + assertGraphiQL, |
| 7 | + assertQuery, |
| 8 | + env, |
| 9 | + fsPromises, |
| 10 | + waitForEndpoint, |
| 11 | +} from '../utils' |
| 12 | +import { DeploymentConfiguration } from '../types' |
| 13 | + |
| 14 | +type VercelDeploymentInputs = {} |
| 15 | + |
| 16 | +class VercelProvider implements pulumi.dynamic.ResourceProvider { |
| 17 | + private baseUrl = 'https://api.vercel.com' |
| 18 | + private authToken = env('VERCEL_AUTH_TOKEN') |
| 19 | + private directory = path.resolve(__dirname, '..', 'examples', 'nextjs-edge') |
| 20 | + |
| 21 | + private getTeamId(): string | null { |
| 22 | + try { |
| 23 | + return env('VERCEL_TEAM_ID') |
| 24 | + } catch { |
| 25 | + return null |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + async delete(id: string) { |
| 30 | + const teamId = this.getTeamId() |
| 31 | + const response = await fetch( |
| 32 | + `${this.baseUrl}/v13/deployments/${id}${ |
| 33 | + teamId ? `?teamId=${teamId}` : '' |
| 34 | + }`, |
| 35 | + { |
| 36 | + method: 'DELETE', |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${this.authToken}`, |
| 39 | + }, |
| 40 | + }, |
| 41 | + ) |
| 42 | + |
| 43 | + if (response.status !== 200) { |
| 44 | + throw new Error( |
| 45 | + `Failed to delete Vercel deployment: invalid status code (${ |
| 46 | + response.status |
| 47 | + }), body: ${await response.text()}`, |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async create(): Promise<pulumi.dynamic.CreateResult> { |
| 53 | + // Create project |
| 54 | + const teamId = this.getTeamId() |
| 55 | + |
| 56 | + const url = new URL(`${this.baseUrl}/v9/projects`) |
| 57 | + url.searchParams.set('teamId', teamId) |
| 58 | + |
| 59 | + const response = await fetch(url.toString(), { |
| 60 | + method: 'POST', |
| 61 | + headers: { |
| 62 | + Authorization: `Bearer ${this.authToken}`, |
| 63 | + }, |
| 64 | + body: JSON.stringify({ |
| 65 | + name: new Date().toISOString(), |
| 66 | + framework: 'nextjs', |
| 67 | + }), |
| 68 | + }) |
| 69 | + |
| 70 | + await pulumi.log.info(`Create deployment (status=${response.status})`) |
| 71 | + |
| 72 | + if (response.status !== 200) { |
| 73 | + throw new Error( |
| 74 | + `Failed to create Vercel deployment: invalid status code (${ |
| 75 | + response.status |
| 76 | + }), body: ${await response.text()}`, |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + const responseJson: { |
| 81 | + id: string |
| 82 | + // url: string |
| 83 | + } = await response.json() |
| 84 | + |
| 85 | + // Deploy project |
| 86 | + await fs.rmdir(path.join(this.directory, '.vercel'), { |
| 87 | + recursive: true, |
| 88 | + // @ts-expect-error |
| 89 | + force: true, |
| 90 | + }) |
| 91 | + |
| 92 | + await fs.mkdir(path.join(this.directory, '.vercel')) |
| 93 | + |
| 94 | + await fs.writeFile( |
| 95 | + path.join(this.directory, '.vercel', 'project.json'), |
| 96 | + JSON.stringify({ |
| 97 | + projectId: responseJson.id, |
| 98 | + // TODO: figure out how we can `orgId` without inlining it. |
| 99 | + orgId: this.getTeamId(), |
| 100 | + settings: { |
| 101 | + createdAt: 1677572115659, |
| 102 | + framework: 'nextjs', |
| 103 | + devCommand: null, |
| 104 | + installCommand: null, |
| 105 | + buildCommand: null, |
| 106 | + outputDirectory: null, |
| 107 | + rootDirectory: null, |
| 108 | + directoryListing: false, |
| 109 | + nodeVersion: '18.x', |
| 110 | + }, |
| 111 | + }), |
| 112 | + ) |
| 113 | + |
| 114 | + await execa('pnpm', ['run', 'end2end:build'], { |
| 115 | + cwd: this.directory, |
| 116 | + }) |
| 117 | + |
| 118 | + const deployment = await execa('pnpm', ['run', 'end2end:deploy'], { |
| 119 | + cwd: this.directory, |
| 120 | + }) |
| 121 | + |
| 122 | + const regex = /\n✅ Production: (https:\/\/.*.vercel.app) \[/ |
| 123 | + |
| 124 | + const result = deployment.all?.match(regex) |
| 125 | + |
| 126 | + if (!Array.isArray(result)) { |
| 127 | + pulumi.log.info("Couldn't find deployment URL in output.") |
| 128 | + throw new Error("Couldn't find deployment URL in output.") |
| 129 | + } |
| 130 | + |
| 131 | + const deploymentUrl = result[1] |
| 132 | + |
| 133 | + return { |
| 134 | + id: responseJson.id, |
| 135 | + outs: { |
| 136 | + url: deploymentUrl, |
| 137 | + }, |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +export class VercelDeployment extends pulumi.dynamic.Resource { |
| 143 | + public readonly url: pulumi.Output<string> |
| 144 | + |
| 145 | + constructor( |
| 146 | + name: string, |
| 147 | + props: VercelDeploymentInputs, |
| 148 | + opts?: pulumi.CustomResourceOptions, |
| 149 | + ) { |
| 150 | + super( |
| 151 | + new VercelProvider(), |
| 152 | + name, |
| 153 | + { |
| 154 | + url: undefined, |
| 155 | + ...props, |
| 156 | + }, |
| 157 | + opts, |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +export const vercelEdgeDeployment: DeploymentConfiguration<{ |
| 163 | + functionUrl: string |
| 164 | +}> = { |
| 165 | + program: async () => { |
| 166 | + const deployment = new VercelDeployment('vercel-edge', {}) |
| 167 | + |
| 168 | + return { |
| 169 | + functionUrl: pulumi.interpolate`https://${deployment.url}/api/graphql`, |
| 170 | + } |
| 171 | + }, |
| 172 | + test: async ({ functionUrl }) => { |
| 173 | + console.log(`ℹ️ Vercel Edge Function deployed to URL: ${functionUrl.value}`) |
| 174 | + await waitForEndpoint(functionUrl.value, 5, 10000) |
| 175 | + await assertGraphiQL(functionUrl.value) |
| 176 | + await assertQuery(functionUrl.value) |
| 177 | + }, |
| 178 | +} |
0 commit comments