|
| 1 | +// @ts-check |
| 2 | +const path = require('path') |
| 3 | +const execa = require('execa') |
| 4 | +const fs = require('fs/promises') |
| 5 | + |
| 6 | +const cwd = process.cwd() |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const deployDir = path.join(cwd, 'files') |
| 10 | + const publicDir = path.join(deployDir, 'public') |
| 11 | + await fs.mkdir(publicDir, { recursive: true }) |
| 12 | + await fs.writeFile( |
| 13 | + path.join(deployDir, 'package.json'), |
| 14 | + JSON.stringify({ |
| 15 | + name: 'files', |
| 16 | + dependencies: {}, |
| 17 | + scripts: { |
| 18 | + build: 'node inject-deploy-url.js', |
| 19 | + }, |
| 20 | + }) |
| 21 | + ) |
| 22 | + await fs.copyFile( |
| 23 | + path.join(cwd, 'scripts/inject-deploy-url.js'), |
| 24 | + path.join(deployDir, 'inject-deploy-url.js') |
| 25 | + ) |
| 26 | + |
| 27 | + let nativePackagesDir = path.join(cwd, 'packages/next-swc/crates/napi/npm') |
| 28 | + let platforms = (await fs.readdir(nativePackagesDir)).filter( |
| 29 | + (name) => !name.startsWith('.') |
| 30 | + ) |
| 31 | + |
| 32 | + const optionalDeps = {} |
| 33 | + const { version } = JSON.parse( |
| 34 | + await fs.readFile(path.join(cwd, 'lerna.json'), 'utf8') |
| 35 | + ) |
| 36 | + |
| 37 | + await Promise.all( |
| 38 | + platforms.map(async (platform) => { |
| 39 | + let binaryName = `next-swc.${platform}.node` |
| 40 | + await fs.cp( |
| 41 | + path.join(cwd, 'packages/next-swc/native', binaryName), |
| 42 | + path.join(nativePackagesDir, platform, binaryName) |
| 43 | + ) |
| 44 | + let pkg = JSON.parse( |
| 45 | + await fs.readFile( |
| 46 | + path.join(nativePackagesDir, platform, 'package.json'), |
| 47 | + 'utf8' |
| 48 | + ) |
| 49 | + ) |
| 50 | + pkg.version = version |
| 51 | + await fs.writeFile( |
| 52 | + path.join(nativePackagesDir, platform, 'package.json'), |
| 53 | + JSON.stringify(pkg, null, 2) |
| 54 | + ) |
| 55 | + const { stdout } = await execa(`npm`, [ |
| 56 | + `pack`, |
| 57 | + `${path.join(nativePackagesDir, platform)}`, |
| 58 | + ]) |
| 59 | + process.stdout.write(stdout) |
| 60 | + const tarballName = stdout.split('\n').pop()?.trim() || '' |
| 61 | + await fs.rename( |
| 62 | + path.join(cwd, tarballName), |
| 63 | + path.join(publicDir, tarballName) |
| 64 | + ) |
| 65 | + optionalDeps[pkg.name] = `https://DEPLOY_URL/${tarballName}` |
| 66 | + }) |
| 67 | + ) |
| 68 | + |
| 69 | + const nextPkgJsonPath = path.join(cwd, 'packages/next/package.json') |
| 70 | + const nextPkg = JSON.parse(await fs.readFile(nextPkgJsonPath, 'utf8')) |
| 71 | + |
| 72 | + nextPkg.optionalDependencies = optionalDeps |
| 73 | + |
| 74 | + await fs.writeFile(nextPkgJsonPath, JSON.stringify(nextPkg, null, 2)) |
| 75 | + |
| 76 | + const { stdout: nextPackStdout } = await execa(`npm`, [ |
| 77 | + `pack`, |
| 78 | + `${path.join(cwd, 'packages/next')}`, |
| 79 | + ]) |
| 80 | + process.stdout.write(nextPackStdout) |
| 81 | + const nextTarballName = nextPackStdout.split('\n').pop()?.trim() || '' |
| 82 | + await fs.rename( |
| 83 | + path.join(cwd, nextTarballName), |
| 84 | + path.join(publicDir, nextTarballName) |
| 85 | + ) |
| 86 | + |
| 87 | + await fs.writeFile( |
| 88 | + path.join(deployDir, 'vercel.json'), |
| 89 | + JSON.stringify( |
| 90 | + { |
| 91 | + version: 2, |
| 92 | + rewrites: [ |
| 93 | + { |
| 94 | + source: '/next.tgz', |
| 95 | + destination: `/${nextTarballName}`, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + null, |
| 100 | + 2 |
| 101 | + ) |
| 102 | + ) |
| 103 | + const vercelConfigDir = path.join(cwd, '.vercel') |
| 104 | + |
| 105 | + if (process.env.VERCEL_TEST_TOKEN) { |
| 106 | + await fs.mkdir(vercelConfigDir) |
| 107 | + await fs.writeFile( |
| 108 | + path.join(vercelConfigDir, 'auth.json'), |
| 109 | + JSON.stringify({ |
| 110 | + token: process.env.VERCEL_TEST_TOKEN, |
| 111 | + }) |
| 112 | + ) |
| 113 | + await fs.writeFile( |
| 114 | + path.join(vercelConfigDir, 'config.json'), |
| 115 | + JSON.stringify({}) |
| 116 | + ) |
| 117 | + console.log('wrote config to', vercelConfigDir) |
| 118 | + } |
| 119 | + |
| 120 | + const child = execa( |
| 121 | + 'vercel', |
| 122 | + [ |
| 123 | + '--scope', |
| 124 | + process.env.VERCEL_TEST_TEAM || '', |
| 125 | + '--global-config', |
| 126 | + vercelConfigDir, |
| 127 | + '-y', |
| 128 | + ], |
| 129 | + { |
| 130 | + cwd: deployDir, |
| 131 | + } |
| 132 | + ) |
| 133 | + let deployOutput = '' |
| 134 | + const handleData = (type) => (chunk) => { |
| 135 | + process[type].write(chunk) |
| 136 | + |
| 137 | + // only want stdout since that's where deployment URL |
| 138 | + // is sent to |
| 139 | + if (type === 'stdout') { |
| 140 | + deployOutput += chunk.toString() |
| 141 | + } |
| 142 | + } |
| 143 | + child.stdout?.on('data', handleData('stdout')) |
| 144 | + child.stderr?.on('data', handleData('stderr')) |
| 145 | + |
| 146 | + await child |
| 147 | + |
| 148 | + const deployUrl = deployOutput.trim() |
| 149 | + console.log(`\n\nNext.js tarball: ${deployUrl.trim()}/next.tgz`) |
| 150 | +} |
| 151 | + |
| 152 | +main().catch((err) => { |
| 153 | + console.error(err) |
| 154 | + process.exit(1) |
| 155 | +}) |
0 commit comments