/
utils.ts
34 lines (28 loc) · 998 Bytes
/
utils.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
import type arg from 'next/dist/compiled/arg/index.js'
export function printAndExit(message: string, code = 1) {
if (code === 0) {
console.log(message)
} else {
console.error(message)
}
process.exit(code)
}
export function getNodeOptionsWithoutInspect() {
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '')
}
export function getPort(args: arg.Result<arg.Spec>): number {
if (process.env.__NEXT_FORCED_PORT) {
// we allow the server to use a random port while testing
// instead of attempting to find a random port and then hope
// it doesn't become occupied before we leverage it
return parseInt(process.env.__NEXT_FORCED_PORT, 10) || 0
} else if (args['--port'] != null) {
return args['--port']
}
const parsed = process.env.PORT != null && parseInt(process.env.PORT, 10)
if (typeof parsed === 'number' && !Number.isNaN(parsed)) {
return parsed
}
return 3000
}