-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathbase.ts
292 lines (255 loc) · 8.41 KB
/
base.ts
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os from 'os'
import path, { dirname } from 'path'
import fs from 'fs-extra'
import { NextConfig } from 'next'
import { FileRef } from '../e2e-utils'
import { ChildProcess } from 'child_process'
import { createNextInstall } from '../create-next-install'
type Event = 'stdout' | 'stderr' | 'error' | 'destroy'
export type InstallCommand = string | ((ctx: { dependencies: { [key: string]: string } }) => string)
export type PackageJson = {
[key: string]: unknown
}
export class NextInstance {
protected files:
| FileRef
| {
[filename: string]: string | FileRef
}
protected nextConfig?: NextConfig
protected installCommand?: InstallCommand
protected buildCommand?: string
protected startCommand?: string
protected dependencies?: { [name: string]: string }
protected events: { [eventName: string]: Set<any> }
public testDir: string
protected isStopping: boolean
protected isDestroyed: boolean
protected childProcess: ChildProcess
protected _url: string
protected _parsedUrl: URL
protected packageJson: PackageJson
protected packageLockPath?: string
protected basePath?: string
protected env?: Record<string, string>
public forcedPort?: string
constructor({
files,
dependencies,
nextConfig,
installCommand,
buildCommand,
startCommand,
packageJson = {},
packageLockPath,
env,
}: {
files:
| FileRef
| {
[filename: string]: string | FileRef
}
dependencies?: {
[name: string]: string
}
packageJson?: PackageJson
packageLockPath?: string
nextConfig?: NextConfig
installCommand?: InstallCommand
buildCommand?: string
startCommand?: string
env?: Record<string, string>
}) {
this.files = files
this.dependencies = dependencies
this.nextConfig = nextConfig
this.installCommand = installCommand
this.buildCommand = buildCommand
this.startCommand = startCommand
this.packageJson = packageJson
this.packageLockPath = packageLockPath
this.events = {}
this.isDestroyed = false
this.isStopping = false
this.env = env
}
protected async createTestDir({ skipInstall = false }: { skipInstall?: boolean } = {}) {
if (this.isDestroyed) {
throw new Error('next instance already destroyed')
}
const tmpDir = process.env.NEXT_TEST_DIR || (await fs.realpath(os.tmpdir()))
this.testDir = path.join(tmpDir, `next-test-${Date.now()}-${(Math.random() * 1000) | 0}`)
const finalDependencies = {
react: 'latest',
'react-dom': 'latest',
...this.dependencies,
...((this.packageJson.dependencies as object | undefined) || {}),
}
const plugin = dirname(require.resolve('@netlify/plugin-nextjs/package.json'))
const pkgScripts = (this.packageJson['scripts'] as {}) || {}
await fs.ensureDir(this.testDir)
const finalPackageJson = {
...this.packageJson,
license: 'MIT',
dependencies: {
...finalDependencies,
'@netlify/plugin-nextjs': `file:${plugin}`,
next: process.env.NEXT_TEST_VERSION || require('next/package.json').version,
},
scripts: {
build: 'next build',
...pkgScripts,
},
}
if (this.files instanceof FileRef) {
// if a FileRef is passed directly to `files` we copy the
// entire folder to the test directory
const stats = await fs.stat(this.files.fsPath)
if (!stats.isDirectory()) {
throw new Error(`FileRef passed to "files" in "createNext" is not a directory ${this.files.fsPath}`)
}
await fs.copy(this.files.fsPath, this.testDir)
} else {
for (const filename of Object.keys(this.files)) {
const item = this.files[filename]
const outputFilename = path.join(this.testDir, filename)
if (typeof item === 'string') {
await fs.ensureDir(path.dirname(outputFilename))
await fs.writeFile(outputFilename, item)
} else {
await fs.copy(item.fsPath, outputFilename)
}
}
}
await fs.writeFile(path.join(this.testDir, 'package.json'), JSON.stringify(finalPackageJson, null, 2))
if (!fs.existsSync(path.join(this.testDir, 'netlify.toml'))) {
const toml = /* toml */ `
[build]
command = "yarn build"
publish = ".next"
[[plugins]]
package = "@netlify/plugin-nextjs"
`
await fs.writeFile(path.join(this.testDir, 'netlify.toml'), toml)
}
let nextConfigFile = Object.keys(this.files).find((file) => file.startsWith('next.config.'))
if (await fs.pathExists(path.join(this.testDir, 'next.config.js'))) {
nextConfigFile = 'next.config.js'
}
if (nextConfigFile && this.nextConfig) {
throw new Error(
`nextConfig provided on "createNext()" and as a file "${nextConfigFile}", use one or the other to continue`,
)
}
if (this.nextConfig || ((global as any).isNextDeploy && !nextConfigFile)) {
const functions = []
await fs.writeFile(
path.join(this.testDir, 'next.config.js'),
`
module.exports = ` +
JSON.stringify(
{
...this.nextConfig,
} as NextConfig,
(key, val) => {
if (typeof val === 'function') {
functions.push(val.toString().replace(new RegExp(`${val.name}[\\s]{0,}\\(`), 'function('))
return `__func_${functions.length - 1}`
}
return val
},
2,
).replace(/"__func_[\d]{1,}"/g, function (str) {
return functions.shift()
}),
)
}
if ((global as any).isNextDeploy) {
const fileName = path.join(this.testDir, nextConfigFile || 'next.config.js')
const content = await fs.readFile(fileName, 'utf8')
if (content.includes('basePath')) {
this.basePath = content.match(/['"`]?basePath['"`]?:.*?['"`](.*?)['"`]/)?.[1] || ''
}
await fs.writeFile(
fileName,
`${content}\n` +
`
// alias __NEXT_TEST_MODE for next-deploy as "_" is not a valid
// env variable during deploy
if (process.env.NEXT_PRIVATE_TEST_MODE) {
process.env.__NEXT_TEST_MODE = process.env.NEXT_PRIVATE_TEST_MODE
}
`,
)
}
require('console').log(`Test directory created at ${this.testDir}`)
}
public async clean() {
if (this.childProcess) {
throw new Error(`stop() must be called before cleaning`)
}
const keptFiles = ['node_modules', 'package.json', 'yarn.lock']
for (const file of await fs.readdir(this.testDir)) {
if (!keptFiles.includes(file)) {
await fs.remove(path.join(this.testDir, file))
}
}
}
public async export(): Promise<{ exitCode?: number; cliOutput?: string }> {
return {}
}
public async setup(): Promise<void> {}
public async start(useDirArg: boolean = false): Promise<void> {}
public async destroy(): Promise<void> {
if (this.isDestroyed) {
throw new Error(`Next.js base instance already destroyed`)
}
this.isDestroyed = true
this.emit('destroy', [])
if (!process.env.NEXT_TEST_SKIP_CLEANUP) {
await fs.remove(this.testDir)
}
require('console').log(`destroyed next instance`)
}
public get url() {
return this._url
}
public get appPort() {
return this._parsedUrl.port
}
public get buildId(): string {
return ''
}
public get cliOutput(): string {
return ''
}
// TODO: block these in deploy mode
public async readFile(filename: string) {
return fs.readFile(path.join(this.testDir, filename), 'utf8')
}
public async patchFile(filename: string, content: string) {
const outputPath = path.join(this.testDir, filename)
await fs.ensureDir(path.dirname(outputPath))
return fs.writeFile(outputPath, content)
}
public async renameFile(filename: string, newFilename: string) {
return fs.rename(path.join(this.testDir, filename), path.join(this.testDir, newFilename))
}
public async deleteFile(filename: string) {
return fs.remove(path.join(this.testDir, filename))
}
public on(event: Event, cb: (...args: any[]) => any) {
if (!this.events[event]) {
this.events[event] = new Set()
}
this.events[event].add(cb)
}
public off(event: Event, cb: (...args: any[]) => any) {
this.events[event]?.delete(cb)
}
protected emit(event: Event, args: any[]) {
this.events[event]?.forEach((cb) => {
cb(...args)
})
}
}