/
create-next-install.js
224 lines (206 loc) · 6.71 KB
/
create-next-install.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const os = require('os')
const path = require('path')
const execa = require('execa')
const fs = require('fs-extra')
const childProcess = require('child_process')
const { randomBytes } = require('crypto')
const { linkPackages } =
require('../../.github/actions/next-stats-action/src/prepare/repo-setup')()
/**
* These are simple dependencies provided by default. We want to optimize this special case.
*/
const areGenericDependencies = (dependencies) =>
Object.keys(dependencies).length === 6 &&
Object.entries(dependencies).every(([dep, version]) => {
if (dep === 'next') return true
return (
[
'react',
'react-dom',
'typescript',
'@types/react',
'@types/node',
].includes(dep) && version === 'latest'
)
})
async function createNextInstall({
parentSpan,
dependencies,
installCommand,
packageJson = {},
packageLockPath = '',
dirSuffix = '',
}) {
return await parentSpan
.traceChild('createNextInstall')
.traceAsyncFn(async (rootSpan) => {
const tmpDir = await fs.realpath(process.env.NEXT_TEST_DIR || os.tmpdir())
const origRepoDir = path.join(__dirname, '../../')
const installDir = path.join(
tmpDir,
`next-install-${randomBytes(32).toString('hex')}${dirSuffix}`
)
const tmpRepoDir = path.join(
tmpDir,
`next-repo-${randomBytes(32).toString('hex')}${dirSuffix}`
)
require('console').log('Using following temporary directories:')
require('console').log(installDir)
require('console').log(tmpRepoDir)
await rootSpan.traceChild(' enruse swc binary').traceAsyncFn(async () => {
// ensure swc binary is present in the native folder if
// not already built
for (const folder of await fs.readdir(
path.join(origRepoDir, 'node_modules/@next')
)) {
if (folder.startsWith('swc-')) {
const swcPkgPath = path.join(
origRepoDir,
'node_modules/@next',
folder
)
const outputPath = path.join(
origRepoDir,
'packages/next-swc/native'
)
await fs.copy(swcPkgPath, outputPath, {
filter: (item) => {
return (
item === swcPkgPath ||
(item.endsWith('.node') &&
!fs.pathExistsSync(
path.join(outputPath, path.basename(item))
))
)
},
})
}
}
})
for (const item of ['package.json', 'packages']) {
await rootSpan
.traceChild(`copy ${item} to temp dir`)
.traceAsyncFn(async () => {
await fs.copy(
path.join(origRepoDir, item),
path.join(tmpRepoDir, item),
{
filter: (item) => {
return (
!item.includes('node_modules') &&
!item.includes('.DS_Store') &&
// Exclude Rust compilation files
!/next[\\/]build[\\/]swc[\\/]target/.test(item) &&
!/next-swc[\\/]target/.test(item)
)
},
}
)
})
}
let combinedDependencies = dependencies
if (!(packageJson && packageJson.nextPrivateSkipLocalDeps)) {
const pkgPaths = await linkPackages({
repoDir: tmpRepoDir,
parentSpan: rootSpan,
})
combinedDependencies = {
next: pkgPaths.get('next'),
...Object.keys(dependencies).reduce((prev, pkg) => {
const pkgPath = pkgPaths.get(pkg)
prev[pkg] = pkgPath || dependencies[pkg]
return prev
}, {}),
}
}
await fs.ensureDir(installDir)
await fs.writeFile(
path.join(installDir, 'package.json'),
JSON.stringify(
{
...packageJson,
dependencies: combinedDependencies,
private: true,
},
null,
2
)
)
if (packageLockPath) {
await fs.copy(
packageLockPath,
path.join(installDir, path.basename(packageLockPath))
)
}
if (installCommand) {
const installString =
typeof installCommand === 'function'
? installCommand({ dependencies: combinedDependencies })
: installCommand
console.log('running install command', installString)
rootSpan.traceChild('run custom install').traceFn(() => {
childProcess.execSync(installString, {
cwd: installDir,
stdio: ['ignore', 'inherit', 'inherit'],
})
})
} else {
await rootSpan
.traceChild('run generic install command')
.traceAsyncFn(async () => {
const runInstall = async () =>
await execa(
'pnpm',
['install', '--strict-peer-dependencies=false'],
{
cwd: installDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: process.env,
}
)
if (!areGenericDependencies(combinedDependencies)) {
await runInstall()
} else {
const cacheDir = path.join(
origRepoDir,
'test',
'tmp',
'genericInstallCache'
)
const cachedFiles = [
// We can't cache node-modules because .pnpm store must be on the same mount - we can't move it between mountpoints
// 'node_modules',
// FIXME: caching lock file caused itssues and It's not possible when we don't use turbo which we had to disable temporarily
// 'pnpm-lock.yaml',
]
if (
await fs
.access(cacheDir)
.then(() => true)
.catch(() => false)
) {
require('console').log(
'We are able to prepopulate pnpm install from cache'
)
cachedFiles.forEach((file) => {
fs.copy(
path.join(cacheDir, file),
path.join(installDir, file)
)
})
}
await runInstall()
await fs.ensureDir(cacheDir)
cachedFiles.forEach((file) => {
fs.copy(path.join(installDir, file), path.join(cacheDir, file))
})
}
})
}
await fs.remove(tmpRepoDir)
return installDir
})
}
module.exports = {
createNextInstall,
}