-
Notifications
You must be signed in to change notification settings - Fork 366
/
framework-server.ts
79 lines (65 loc) · 2.48 KB
/
framework-server.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
import { rm } from 'node:fs/promises'
import waitPort from 'wait-port'
import { startSpinner, stopSpinner } from '../lib/spinner.js'
import { error, exit, log, NETLIFYDEVERR, NETLIFYDEVLOG } from './command-helpers.js'
import { runCommand } from './shell.js'
import { startStaticServer } from './static-server.js'
import { ServerSettings } from './types.js'
// 10 minutes
const FRAMEWORK_PORT_TIMEOUT = 6e5
interface StartReturnObject {
ipVersion?: 4 | 6
}
/**
* Start a static server if the `useStaticServer` is provided or a framework specific server
*/
export const startFrameworkServer = async function ({
cwd,
settings,
}: {
cwd: string
settings: ServerSettings
}): Promise<StartReturnObject> {
if (settings.useStaticServer) {
if (settings.command) {
runCommand(settings.command, { env: settings.env, cwd })
}
await startStaticServer({ settings })
return {}
}
log(`${NETLIFYDEVLOG} Starting Netlify Dev with ${settings.framework || 'custom config'}`)
const spinner = startSpinner({
text: `Waiting for framework port ${settings.frameworkPort}. This can be configured using the 'targetPort' property in the netlify.toml`,
})
if (settings.clearPublishDirectory && settings.dist) {
await rm(settings.dist, { recursive: true, force: true })
}
runCommand(settings.command, { env: settings.env, spinner, cwd })
let port: { open: boolean; ipVersion?: 4 | 6 } | undefined
try {
if (settings.skipWaitPort) {
port = { open: true, ipVersion: 6 }
} else {
port = await waitPort({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
port: settings.frameworkPort!,
host: 'localhost',
output: 'silent',
timeout: FRAMEWORK_PORT_TIMEOUT,
...(settings.pollingStrategies?.includes('HTTP') && { protocol: 'http' }),
})
if (!port.open) {
throw new Error(`Timed out waiting for port '${settings.frameworkPort}' to be open`)
}
}
stopSpinner({ error: false, spinner })
} catch (error_) {
stopSpinner({ error: true, spinner })
log(NETLIFYDEVERR, `Netlify Dev could not start or connect to localhost:${settings.frameworkPort}.`)
log(NETLIFYDEVERR, `Please make sure your framework server is running on port ${settings.frameworkPort}`)
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
exit(1)
}
return { ipVersion: port?.ipVersion }
}