|
| 1 | +// This is copied to the Next.js repo |
| 2 | +import execa from 'execa' |
| 3 | +import fs from 'fs-extra' |
| 4 | +import { Span } from 'next/src/trace' |
| 5 | +import path from 'path' |
| 6 | +import { NextInstance } from './base' |
| 7 | + |
| 8 | +type NetlifyDeployResponse = { |
| 9 | + name: string |
| 10 | + site_id: string |
| 11 | + site_name: string |
| 12 | + deploy_id: string |
| 13 | + deploy_url: string |
| 14 | + logs: string |
| 15 | +} |
| 16 | + |
| 17 | +export class NextDeployInstance extends NextInstance { |
| 18 | + private _cliOutput: string |
| 19 | + private _buildId: string |
| 20 | + private _deployId: string |
| 21 | + |
| 22 | + public get buildId() { |
| 23 | + // get deployment ID via fetch since we can't access |
| 24 | + // build artifacts directly |
| 25 | + return this._buildId |
| 26 | + } |
| 27 | + |
| 28 | + public async setup(parentSpan: Span) { |
| 29 | + // create the test site |
| 30 | + await super.createTestDir({ parentSpan, skipInstall: true }) |
| 31 | + |
| 32 | + // install dependencies |
| 33 | + await execa('npm', ['i'], { |
| 34 | + cwd: this.testDir, |
| 35 | + stdio: 'inherit', |
| 36 | + }) |
| 37 | + |
| 38 | + // use next runtime package installed by the test runner |
| 39 | + if (!fs.existsSync(path.join(this.testDir, 'netlify.toml'))) { |
| 40 | + const toml = /* toml */ ` |
| 41 | + [build] |
| 42 | + command = "npm run build" |
| 43 | + publish = ".next" |
| 44 | + |
| 45 | + [[plugins]] |
| 46 | + package = "${path.relative(this.testDir, `${process.cwd()}/../next-runtime-minimal`)}" |
| 47 | + ` |
| 48 | + |
| 49 | + await fs.writeFile(path.join(this.testDir, 'netlify.toml'), toml) |
| 50 | + } |
| 51 | + |
| 52 | + // ensure netlify cli is installed |
| 53 | + try { |
| 54 | + const res = await execa('netlify', ['--version']) |
| 55 | + require('console').log(`Using Netlify CLI version:`, res.stdout) |
| 56 | + } catch (_) { |
| 57 | + require('console').log(`You need to have netlify-cli installed. |
| 58 | + |
| 59 | + You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"`) |
| 60 | + } |
| 61 | + |
| 62 | + // ensure project is linked |
| 63 | + try { |
| 64 | + await execa('ntl', ['status', '--json']) |
| 65 | + } catch (err) { |
| 66 | + if (err.message.includes("You don't appear to be in a folder that is linked to a site")) { |
| 67 | + throw new Error(`Site is not linked. Please set "NETLIFY_AUTH_TOKEN" and "NETLIFY_SITE_ID"`) |
| 68 | + } |
| 69 | + throw err |
| 70 | + } |
| 71 | + |
| 72 | + require('console').log(`Deploying project at ${this.testDir}`) |
| 73 | + |
| 74 | + const testName = |
| 75 | + process.env.TEST_FILE_PATH && path.relative(process.cwd(), process.env.TEST_FILE_PATH) |
| 76 | + |
| 77 | + const deployTitle = process.env.GITHUB_SHA |
| 78 | + ? `${testName} - ${process.env.GITHUB_SHA}` |
| 79 | + : testName |
| 80 | + |
| 81 | + if (fs.existsSync(path.join(this.testDir, 'cache-handler.js'))) { |
| 82 | + process.env.CUSTOM_CACHE_HANDLER = path.join(this.testDir, 'cache-handler.js') |
| 83 | + } |
| 84 | + |
| 85 | + const deployRes = await execa( |
| 86 | + 'ntl', |
| 87 | + ['deploy', '--build', '--json', '--message', deployTitle ?? ''], |
| 88 | + { |
| 89 | + cwd: this.testDir, |
| 90 | + reject: false, |
| 91 | + }, |
| 92 | + ) |
| 93 | + |
| 94 | + if (deployRes.exitCode !== 0) { |
| 95 | + throw new Error( |
| 96 | + `Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})`, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + const data: NetlifyDeployResponse = JSON.parse(deployRes.stdout) |
| 102 | + this._url = data.deploy_url |
| 103 | + this._parsedUrl = new URL(this._url) |
| 104 | + this._deployId = data.deploy_id |
| 105 | + require('console').log(`Deployment URL: ${this._url}`) |
| 106 | + } catch (err) { |
| 107 | + console.error(err) |
| 108 | + throw new Error(`Failed to parse deploy output: ${deployRes.stdout}`) |
| 109 | + } |
| 110 | + |
| 111 | + this._buildId = ( |
| 112 | + await fs.readFile( |
| 113 | + path.join(this.testDir, this.nextConfig?.distDir || '.next', 'BUILD_ID'), |
| 114 | + 'utf8', |
| 115 | + ) |
| 116 | + ).trim() |
| 117 | + |
| 118 | + require('console').log(`Got buildId: ${this._buildId}`) |
| 119 | + } |
| 120 | + |
| 121 | + public get cliOutput() { |
| 122 | + return this._cliOutput || '' |
| 123 | + } |
| 124 | + |
| 125 | + public async start() { |
| 126 | + // no-op as the deployment is created during setup() |
| 127 | + } |
| 128 | + |
| 129 | + public async patchFile(filename: string, content: string): Promise<void> { |
| 130 | + throw new Error('patchFile is not available in deploy test mode') |
| 131 | + } |
| 132 | + public async readFile(filename: string): Promise<string> { |
| 133 | + throw new Error('readFile is not available in deploy test mode') |
| 134 | + } |
| 135 | + public async deleteFile(filename: string): Promise<void> { |
| 136 | + throw new Error('deleteFile is not available in deploy test mode') |
| 137 | + } |
| 138 | + public async renameFile(filename: string, newFilename: string): Promise<void> { |
| 139 | + throw new Error('renameFile is not available in deploy test mode') |
| 140 | + } |
| 141 | +} |
0 commit comments