forked from vercel/next.js
/
next-dev.ts
80 lines (70 loc) · 2.09 KB
/
next-dev.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
import { spawn } from 'child_process'
import { NextInstance } from './base'
export class NextDevInstance extends NextInstance {
private _cliOutput: string
public get buildId() {
return 'development'
}
public async setup() {
await super.createTestDir()
}
public get cliOutput() {
return this._cliOutput || ''
}
public async start(useDirArg: boolean = false) {
if (this.childProcess) {
throw new Error('next already started')
}
let startArgs = ['yarn', 'next', useDirArg && this.testDir].filter(
Boolean
) as string[]
if (this.startCommand) {
startArgs = this.startCommand.split(' ')
}
await new Promise<void>((resolve) => {
this.childProcess = spawn(startArgs[0], startArgs.slice(1), {
cwd: useDirArg ? process.cwd() : this.testDir,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
env: {
...process.env,
...this.env,
NODE_ENV: '' as any,
PORT: this.forcedPort || '0',
__NEXT_TEST_MODE: '1',
__NEXT_TEST_WITH_DEVTOOL: '1',
},
})
this._cliOutput = ''
this.childProcess.stdout.on('data', (chunk) => {
const msg = chunk.toString()
process.stdout.write(chunk)
this._cliOutput += msg
this.emit('stdout', [msg])
})
this.childProcess.stderr.on('data', (chunk) => {
const msg = chunk.toString()
process.stderr.write(chunk)
this._cliOutput += msg
this.emit('stderr', [msg])
})
this.childProcess.on('close', (code, signal) => {
if (this.isStopping) return
if (code || signal) {
throw new Error(
`next dev exited unexpectedly with code/signal ${code || signal}`
)
}
})
const readyCb = (msg) => {
if (msg.includes('started server on') && msg.includes('url:')) {
this._url = msg.split('url: ').pop().trim()
this._parsedUrl = new URL(this._url)
this.off('stdout', readyCb)
resolve()
}
}
this.on('stdout', readyCb)
})
}
}