forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-tarball.js
More file actions
155 lines (139 loc) · 3.93 KB
/
deploy-tarball.js
File metadata and controls
155 lines (139 loc) · 3.93 KB
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
// @ts-check
const path = require('path')
const execa = require('execa')
const fs = require('fs/promises')
const cwd = process.cwd()
async function main() {
const deployDir = path.join(cwd, 'files')
const publicDir = path.join(deployDir, 'public')
await fs.mkdir(publicDir, { recursive: true })
await fs.writeFile(
path.join(deployDir, 'package.json'),
JSON.stringify({
name: 'files',
dependencies: {},
scripts: {
build: 'node inject-deploy-url.js',
},
})
)
await fs.copyFile(
path.join(cwd, 'scripts/inject-deploy-url.js'),
path.join(deployDir, 'inject-deploy-url.js')
)
let nativePackagesDir = path.join(cwd, 'packages/next-swc/crates/napi/npm')
let platforms = (await fs.readdir(nativePackagesDir)).filter(
(name) => !name.startsWith('.')
)
const optionalDeps = {}
const { version } = JSON.parse(
await fs.readFile(path.join(cwd, 'lerna.json'), 'utf8')
)
await Promise.all(
platforms.map(async (platform) => {
let binaryName = `next-swc.${platform}.node`
await fs.cp(
path.join(cwd, 'packages/next-swc/native', binaryName),
path.join(nativePackagesDir, platform, binaryName)
)
let pkg = JSON.parse(
await fs.readFile(
path.join(nativePackagesDir, platform, 'package.json'),
'utf8'
)
)
pkg.version = version
await fs.writeFile(
path.join(nativePackagesDir, platform, 'package.json'),
JSON.stringify(pkg, null, 2)
)
const { stdout } = await execa(`npm`, [
`pack`,
`${path.join(nativePackagesDir, platform)}`,
])
process.stdout.write(stdout)
const tarballName = stdout.split('\n').pop()?.trim() || ''
await fs.rename(
path.join(cwd, tarballName),
path.join(publicDir, tarballName)
)
optionalDeps[pkg.name] = `https://DEPLOY_URL/${tarballName}`
})
)
const nextPkgJsonPath = path.join(cwd, 'packages/next/package.json')
const nextPkg = JSON.parse(await fs.readFile(nextPkgJsonPath, 'utf8'))
nextPkg.optionalDependencies = optionalDeps
await fs.writeFile(nextPkgJsonPath, JSON.stringify(nextPkg, null, 2))
const { stdout: nextPackStdout } = await execa(`npm`, [
`pack`,
`${path.join(cwd, 'packages/next')}`,
])
process.stdout.write(nextPackStdout)
const nextTarballName = nextPackStdout.split('\n').pop()?.trim() || ''
await fs.rename(
path.join(cwd, nextTarballName),
path.join(publicDir, nextTarballName)
)
await fs.writeFile(
path.join(deployDir, 'vercel.json'),
JSON.stringify(
{
version: 2,
rewrites: [
{
source: '/next.tgz',
destination: `/${nextTarballName}`,
},
],
},
null,
2
)
)
const vercelConfigDir = path.join(cwd, '.vercel')
if (process.env.VERCEL_TEST_TOKEN) {
await fs.mkdir(vercelConfigDir)
await fs.writeFile(
path.join(vercelConfigDir, 'auth.json'),
JSON.stringify({
token: process.env.VERCEL_TEST_TOKEN,
})
)
await fs.writeFile(
path.join(vercelConfigDir, 'config.json'),
JSON.stringify({})
)
console.log('wrote config to', vercelConfigDir)
}
const child = execa(
'vercel',
[
'--scope',
process.env.VERCEL_TEST_TEAM || '',
'--global-config',
vercelConfigDir,
'-y',
],
{
cwd: deployDir,
}
)
let deployOutput = ''
const handleData = (type) => (chunk) => {
process[type].write(chunk)
// only want stdout since that's where deployment URL
// is sent to
if (type === 'stdout') {
deployOutput += chunk.toString()
}
}
child.stdout?.on('data', handleData('stdout'))
child.stderr?.on('data', handleData('stderr'))
await child
const deployUrl = deployOutput.trim()
console.log(`\n\nNext.js tarball: ${deployUrl.trim()}/next.tgz`)
}
main().catch((err) => {
console.error(err)
process.exit(1)
})