-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.ts
More file actions
296 lines (260 loc) · 8.16 KB
/
Copy pathbootstrap.ts
File metadata and controls
296 lines (260 loc) · 8.16 KB
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
293
294
295
296
export const DEFAULT_PROJECT_DIR = ""
export const DEFAULT_CONTROL_PLANE_URL = "https://spawn-dock.w3voice.net"
export const DEFAULT_CLAIM_PATH = "/v1/bootstrap/claim"
export const DEFAULT_INSPECT_PATH = "/api/pairing/inspect"
export const DEFAULT_TEMPLATE_REPO = "https://github.com/SpawnDock/tma-project.git"
export const DEFAULT_TEMPLATE_BRANCH = "master"
export const GENERATED_PACKAGE_MANAGER = "pnpm@10.32.1"
export const TEMPLATE_ID = "nextjs-template"
/**
* Pinned @spawn-dock/* versions on npm for generated projects.
* Bump together; when you publish a unified 1.0.0-beta.x line to npm, set all three to that tag.
*/
export const SPAWN_DOCK_NPM_VERSIONS = {
"@spawn-dock/dev-tunnel": "1.0.9",
"@spawn-dock/mcp": "1.0.5",
} as const
export interface CliOptions {
readonly token: string
readonly projectId?: string
readonly controlPlaneUrl: string
readonly claimPath: string
readonly projectDir: string
readonly templateRepo: string
readonly templateBranch: string
}
export interface ProjectContext {
readonly projectDir: string
readonly projectSlug: string
readonly projectName: string
readonly templateId: string
}
export interface BootstrapClaim {
readonly projectId: string
readonly projectSlug: string
readonly controlPlaneUrl: string
readonly previewOrigin: string
readonly telegramMiniAppUrl?: string
readonly deviceSecret: string
readonly mcpApiKey: string
readonly apiToken?: string
readonly localPort: number
}
export interface PairingTokenInspection {
readonly projectId: string
readonly projectSlug: string
}
export interface BootstrapSummary {
readonly projectDir: string
readonly projectName: string
readonly previewOrigin: string
readonly mcpAgents: ReadonlyArray<string>
}
export interface GeneratedFile {
readonly path: string
readonly content: string
}
export interface OverlayFile {
readonly path: string
readonly content: string
}
export const DEFAULT_MCP_AGENTS = ["OpenCode", "Claude Code"] as const
export const MIN_NODE_MAJOR = 20
export const normalizeDisplayName = (value: string): string =>
value
.split(/[^a-zA-Z0-9]+/g)
.filter(Boolean)
.map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1).toLowerCase())
.join(" ")
export const resolveProjectContext = (projectDir: string): ProjectContext => {
const projectSlug = projectDir.split(/[\\/]/g).filter(Boolean).at(-1) ?? projectDir
return {
projectDir,
projectSlug,
projectName: normalizeDisplayName(projectSlug),
templateId: TEMPLATE_ID,
}
}
export const resolvePreviewPath = (previewOrigin: string): string => {
const url = new URL(previewOrigin)
const normalizedPath = url.pathname.replace(/\/$/, "")
return normalizedPath.length > 0 ? normalizedPath : ""
}
export const resolvePreviewHost = (previewOrigin: string): string =>
new URL(previewOrigin).host
export const buildMcpServerUrl = (controlPlaneUrl: string): string => {
const url = new URL(controlPlaneUrl)
const normalizedPath = url.pathname.replace(/\/$/, "")
url.pathname = normalizedPath.length > 0 ? `${normalizedPath}/mcp/sse` : "/mcp/sse"
return url.toString()
}
export const resolveClaimPath = (claimPath: string, projectId?: string): string => {
const normalizedClaimPath = claimPath.startsWith("/") ? claimPath : `/${claimPath}`
if (projectId && normalizedClaimPath.includes(":projectId")) {
return normalizedClaimPath.replace(":projectId", projectId)
}
if (projectId && normalizedClaimPath === DEFAULT_CLAIM_PATH) {
return `/api/projects/${projectId}/claim`
}
return normalizedClaimPath
}
export const validateNodeMajorVersion = (version: string): string | null => {
const match = /^v?(\d+)/.exec(version)
const major = match ? Number.parseInt(match[1] ?? "", 10) : Number.NaN
if (!Number.isFinite(major) || major >= MIN_NODE_MAJOR) {
return null
}
return `SpawnDock requires Node.js ${MIN_NODE_MAJOR}+ (detected ${version}). Install a newer Node release from https://nodejs.org/.`
}
export const buildCodexMcpCommandArgs = (mcpServerUrl: string, mcpApiKey: string): ReadonlyArray<string> => [
"mcp",
"add",
"spawndock",
"--env",
`MCP_SERVER_URL=${mcpServerUrl}`,
"--env",
`MCP_SERVER_API_KEY=${mcpApiKey}`,
"--",
"npx",
"-y",
"@spawn-dock/mcp",
]
export const buildTonConnectManifest = (
context: ProjectContext,
claim: BootstrapClaim,
): string =>
`${JSON.stringify(
{
url: claim.previewOrigin,
name: context.projectName,
iconUrl: `${claim.previewOrigin}/favicon.ico`,
},
null,
2,
)}\n`
export const buildProjectMcpConfig = (): string =>
`${JSON.stringify(
{
mcpServers: {
spawndock: {
type: "stdio",
command: "node",
args: ["./spawndock/mcp.mjs"],
},
},
},
null,
2,
)}\n`
export const buildOpenCodeConfig = (): string =>
`${JSON.stringify(
{
$schema: "https://opencode.ai/config.json",
mcp: {
spawndock: {
type: "local",
command: ["node", "./spawndock/mcp.mjs"],
enabled: true,
},
},
},
null,
2,
)}\n`
export const buildGeneratedFiles = (
context: ProjectContext,
claim: BootstrapClaim,
): ReadonlyArray<GeneratedFile> => {
const previewPath = resolvePreviewPath(claim.previewOrigin)
const previewHost = resolvePreviewHost(claim.previewOrigin)
const mcpServerUrl = buildMcpServerUrl(claim.controlPlaneUrl)
const appConfig = {
templateId: context.templateId,
projectId: claim.projectId,
projectSlug: claim.projectSlug,
projectName: context.projectName,
controlPlaneUrl: claim.controlPlaneUrl,
previewOrigin: claim.previewOrigin,
...(claim.telegramMiniAppUrl ? { telegramMiniAppUrl: claim.telegramMiniAppUrl } : {}),
previewPath,
previewHost,
localPort: claim.localPort,
deviceSecret: claim.deviceSecret,
mcpServerUrl,
mcpApiKey: claim.mcpApiKey,
...(claim.apiToken ? { apiToken: claim.apiToken } : {}),
}
const env = {
SPAWNDOCK_CONTROL_PLANE_URL: claim.controlPlaneUrl,
SPAWNDOCK_PREVIEW_ORIGIN: claim.previewOrigin,
SPAWNDOCK_PREVIEW_PATH: previewPath,
SPAWNDOCK_ASSET_PREFIX: previewPath,
SPAWNDOCK_PREVIEW_HOST: previewHost,
SPAWNDOCK_SERVER_ACTIONS_ALLOWED_ORIGINS: previewHost,
SPAWNDOCK_DEVICE_SECRET: claim.deviceSecret,
SPAWNDOCK_PROJECT_ID: claim.projectId,
SPAWNDOCK_PROJECT_SLUG: claim.projectSlug,
SPAWNDOCK_ALLOWED_DEV_ORIGINS: claim.previewOrigin,
...(claim.apiToken ? { SPAWNDOCK_API_TOKEN: claim.apiToken } : {}),
}
return [
{
path: "spawndock.config.json",
content: `${JSON.stringify(appConfig, null, 2)}\n`,
},
{
path: ".env.local",
content: `${Object.entries(env)
.map(([key, value]) => `${key}=${value}`)
.join("\n")}\n`,
},
{
path: "spawndock.dev-tunnel.json",
content: `${JSON.stringify(
{
controlPlane: claim.controlPlaneUrl,
projectSlug: claim.projectSlug,
deviceSecret: claim.deviceSecret,
port: claim.localPort,
},
null,
2,
)}\n`,
},
{
path: "public/tonconnect-manifest.json",
content: buildTonConnectManifest(context, claim),
},
{
path: ".mcp.json",
content: buildProjectMcpConfig(),
},
{
path: "opencode.json",
content: buildOpenCodeConfig(),
},
]
}
export const patchPackageJsonContent = (input: string): string => {
const packageJson = JSON.parse(input) as {
dependencies?: Record<string, string>
devDependencies?: Record<string, string>
packageManager?: string
scripts?: Record<string, string>
}
packageJson.packageManager = GENERATED_PACKAGE_MANAGER
packageJson.scripts = {
...(packageJson.scripts ?? {}),
dev: "node ./spawndock/dev.mjs",
"dev:next": "node ./spawndock/next.mjs",
"dev:tunnel": "node ./spawndock/tunnel.mjs",
"publish:github-pages": "node ./spawndock/publish.mjs",
agent: "node ./spawndock/agent.mjs",
"agent:session": "node ./spawndock/session.mjs",
}
packageJson.devDependencies = {
...(packageJson.devDependencies ?? {}),
...SPAWN_DOCK_NPM_VERSIONS,
}
return `${JSON.stringify(packageJson, null, 2)}\n`
}