-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.ts
132 lines (98 loc) · 4.1 KB
/
start.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
// Built-In Modules
import { join } from "node:path";
// Internal Imports
import { build, configureForDesktop, createServices, resolveConfig } from './index.js'
import { globalTempDir, handleTemporaryDirectories, isDesktop, isMobile, onCleanup } from "./globals.js";
import { ResolvedConfig, ResolvedService, UserConfig } from "./types.js";
import { createServer } from "./vite/index.js";
// Internal Utilities
import { printHeader, printTarget } from "./utils/formatting.js"
import { updateServicesWithLocalIP } from "./utils/ip/index.js";
import { buildAllAssets } from "./build.js";
const createAllServices = (services, { root, target }) => createServices(services, { root, target, services: true, build: false }) // Run services in parallel
type ResolvedServices = Record<string, ResolvedService>
const resolveServices = (
config: ResolvedConfig
): ResolvedServices => {
const { target } = config
const isMobileTarget = isMobile(target)
if (isMobileTarget) return updateServicesWithLocalIP(config.services) // Create URLs that will be shared with the frontend
return config.services
}
export const services = async (
config: UserConfig,
resolvedServices
) => {
const resolvedConfig = await resolveConfig(config);
const { root, target } = resolvedConfig
// Build service outputs
await build(resolvedConfig, { services: resolvedServices, dev: true })
if (!resolvedServices) resolvedServices = resolveServices(resolvedConfig)
// Create services
return await createAllServices(resolvedServices, { root, target })
}
export const app = async function (
config: UserConfig
) {
const resolvedConfig = await resolveConfig(config);
const { name, root, target } = resolvedConfig
const isDesktopTarget = isDesktop(target)
const isMobileTarget = isMobile(target)
await printHeader(`${name} — ${printTarget(target)} Development`)
const resolvedServices = resolveServices(resolvedConfig)
// Temporary directory for the build
const outDir = join(root, globalTempDir)
const filesystemManager = await handleTemporaryDirectories(outDir)
const configCopy = {
...resolvedConfig,
outDir
}
// Build for mobile before moving forward
if (isMobileTarget) await build(
configCopy,
{ services: resolvedServices, dev: true }
)
// Manually clear and build the output assets
else {
await buildAllAssets(configCopy, true)
}
const activeInstances: {
frontend?: Awaited<ReturnType<typeof createServer>>,
services?: Awaited<ReturnType<typeof createAllServices>>
} = {}
let closed = false
const closeFunction = (o) => {
// If already closed, do nothing
if (closed) return
closed = true
// Close all dependent services
if (o.frontend) activeInstances.frontend?.close() // Close server first
if (o.services) activeInstances.services?.close() // Close custom services next
}
const manager: {
url?: string,
close: typeof closeFunction
} = {
close: closeFunction
}
// Configure the desktop instance
if (isDesktopTarget) await configureForDesktop(outDir, root)
// Create all services
else activeInstances.services = await services(configCopy, resolvedServices)
// Serve the frontend (if not mobile)
if (!isMobileTarget) {
const frontend = activeInstances.frontend = await createServer(configCopy, { printUrls: !isDesktopTarget })
manager.url = frontend.resolvedUrls.local[0] // Add URL to locate the server
}
const closeAll = (o) => {
filesystemManager.close()
manager.close(o)
}
onCleanup(() => {
closeAll({ services: true, frontend: true }) // Close all services and frontend on exit
})
return {
url: manager.url,
close: closeAll
}
}