@apex-stack/core 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build-QRHQUUZC.js +235 -0
- package/dist/chunk-2L2T47AH.js +93 -0
- package/dist/chunk-4VG3CZ6H.js +21 -0
- package/dist/chunk-DSUIB3JH.js +144 -0
- package/dist/chunk-PAMD24NK.js +171 -0
- package/dist/chunk-QZTDKUXK.js +127 -0
- package/dist/cli.js +78 -607
- package/dist/dev-HDRJEPPN.js +38 -0
- package/dist/index.js +8 -3
- package/dist/make-4LINTKZH.js +89 -0
- package/dist/mcp-DL4J6JFJ.js +56 -0
- package/dist/migrate-NOGFOFV2.js +38 -0
- package/dist/server-VOIXBL4I.js +9 -0
- package/dist/start-VJJXI4W3.js +132 -0
- package/package.json +1 -1
- package/dist/chunk-IEXQ7E5C.js +0 -426
package/dist/cli.js
CHANGED
|
@@ -1,428 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
loadComponents,
|
|
8
|
-
matchRoute,
|
|
9
|
-
renderIslandsPage,
|
|
10
|
-
renderPage,
|
|
11
|
-
scanPages,
|
|
12
|
-
startDevServer
|
|
13
|
-
} from "./chunk-IEXQ7E5C.js";
|
|
3
|
+
banner,
|
|
4
|
+
color,
|
|
5
|
+
spinner
|
|
6
|
+
} from "./chunk-2L2T47AH.js";
|
|
14
7
|
|
|
15
8
|
// src/cli.ts
|
|
16
|
-
import {
|
|
17
|
-
import { defineCommand as defineCommand7, runMain } from "citty";
|
|
18
|
-
|
|
19
|
-
// src/commands/build.ts
|
|
20
|
-
import { cpSync, existsSync as existsSync2, mkdirSync, readdirSync as readdirSync2, rmSync, writeFileSync } from "fs";
|
|
21
|
-
import { dirname, join as join3, resolve } from "path";
|
|
22
|
-
import { apex as apex3 } from "@apex-stack/vite";
|
|
23
|
-
import { defineCommand } from "citty";
|
|
24
|
-
import { createServer as createViteServer } from "vite";
|
|
25
|
-
|
|
26
|
-
// src/build/buildClient.ts
|
|
27
|
-
import { readFileSync } from "fs";
|
|
28
|
-
import { join } from "path";
|
|
29
|
-
import { apex } from "@apex-stack/vite";
|
|
30
|
-
import { build } from "vite";
|
|
31
|
-
var VIRT = "virtual:apex-client:";
|
|
32
|
-
function entryName(pageId) {
|
|
33
|
-
return pageId.replace(/^\/pages\//, "").replace(/\.alpine$/, "").replace(/[^a-zA-Z0-9]+/g, "_");
|
|
34
|
-
}
|
|
35
|
-
async function buildClient(root, routes, outDir) {
|
|
36
|
-
const input = {};
|
|
37
|
-
for (const r of routes) input[entryName(r.pageId)] = `${VIRT}${r.pageId}`;
|
|
38
|
-
const entryPlugin = {
|
|
39
|
-
name: "apex:client-entries",
|
|
40
|
-
resolveId(id) {
|
|
41
|
-
if (id.startsWith(VIRT)) return `\0${id}`;
|
|
42
|
-
},
|
|
43
|
-
load(id) {
|
|
44
|
-
if (id.startsWith(`\0${VIRT}`)) {
|
|
45
|
-
const pageId = id.slice(`\0${VIRT}`.length);
|
|
46
|
-
return [
|
|
47
|
-
`import Alpine from 'alpinejs'`,
|
|
48
|
-
`import ${JSON.stringify(pageId)}`,
|
|
49
|
-
`window.Alpine = Alpine`,
|
|
50
|
-
`Alpine.start()`
|
|
51
|
-
].join("\n");
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
await build({
|
|
56
|
-
root,
|
|
57
|
-
logLevel: "warn",
|
|
58
|
-
plugins: [apex({ clientRuntime: "@apex-stack/core/client" }), entryPlugin],
|
|
59
|
-
build: {
|
|
60
|
-
outDir,
|
|
61
|
-
emptyOutDir: false,
|
|
62
|
-
manifest: true,
|
|
63
|
-
rollupOptions: { input }
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
const manifest = JSON.parse(readFileSync(join(outDir, ".vite", "manifest.json"), "utf8"));
|
|
67
|
-
const hrefs = /* @__PURE__ */ new Map();
|
|
68
|
-
for (const r of routes) {
|
|
69
|
-
const virt = `${VIRT}${r.pageId}`;
|
|
70
|
-
const entry = Object.values(manifest).find(
|
|
71
|
-
(m) => m.isEntry && (m.src === virt || m.src === `\0${virt}`)
|
|
72
|
-
);
|
|
73
|
-
if (entry) hrefs.set(r.pageId, `/${entry.file}`);
|
|
74
|
-
}
|
|
75
|
-
return hrefs;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// src/build/buildServer.ts
|
|
79
|
-
import { existsSync, readdirSync } from "fs";
|
|
80
|
-
import { isAbsolute, join as join2 } from "path";
|
|
81
|
-
import { apex as apex2 } from "@apex-stack/vite";
|
|
82
|
-
import { build as build2 } from "vite";
|
|
83
|
-
async function buildServer(root, routes, outDir) {
|
|
84
|
-
const ids = routes.map((r) => r.pageId);
|
|
85
|
-
const compDir = join2(root, "components");
|
|
86
|
-
if (existsSync(compDir)) {
|
|
87
|
-
for (const f of readdirSync(compDir).filter((f2) => f2.endsWith(".alpine"))) {
|
|
88
|
-
ids.push(`/components/${f}`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
const apiDir = join2(root, "server", "api");
|
|
92
|
-
if (existsSync(apiDir)) {
|
|
93
|
-
for (const f of readdirSync(apiDir).filter((f2) => /\.(ts|js|mjs)$/.test(f2))) {
|
|
94
|
-
ids.push(`/server/api/${f}`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const input = {};
|
|
98
|
-
for (const id of ids) input[entryName2(id)] = join2(root, id.slice(1));
|
|
99
|
-
const result = await build2({
|
|
100
|
-
root,
|
|
101
|
-
logLevel: "warn",
|
|
102
|
-
plugins: [apex2({ clientRuntime: "@apex-stack/core/client" })],
|
|
103
|
-
build: {
|
|
104
|
-
ssr: true,
|
|
105
|
-
target: "esnext",
|
|
106
|
-
// Node target — allow top-level await in server modules
|
|
107
|
-
outDir: join2(outDir, "server"),
|
|
108
|
-
emptyOutDir: false,
|
|
109
|
-
rollupOptions: {
|
|
110
|
-
input,
|
|
111
|
-
// Externalize every package import (bare specifier) — deps are resolved at
|
|
112
|
-
// runtime from node_modules. Only the app's own relative/absolute files are
|
|
113
|
-
// bundled. This keeps native/workspace deps (@libsql/client, drizzle, …) out.
|
|
114
|
-
external: (id) => !id.startsWith(".") && !isAbsolute(id),
|
|
115
|
-
output: { format: "esm", entryFileNames: "[name].mjs" }
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
const byFacade = /* @__PURE__ */ new Map();
|
|
120
|
-
for (const chunk of result.output) {
|
|
121
|
-
if (chunk.type === "chunk" && chunk.isEntry && chunk.facadeModuleId) {
|
|
122
|
-
byFacade.set(chunk.facadeModuleId, chunk.fileName);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
const modules = {};
|
|
126
|
-
for (const id of ids) {
|
|
127
|
-
const abs = join2(root, id.slice(1));
|
|
128
|
-
const file = byFacade.get(abs);
|
|
129
|
-
if (file) modules[id] = file;
|
|
130
|
-
}
|
|
131
|
-
return { modules };
|
|
132
|
-
}
|
|
133
|
-
function entryName2(id) {
|
|
134
|
-
return id.replace(/^\//, "").replace(/\.(alpine|ts|js|mjs)$/, "").replace(/[^a-zA-Z0-9]+/g, "_");
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// src/commands/build.ts
|
|
138
|
-
function outFile(pattern) {
|
|
139
|
-
const clean = pattern.replace(/^\//, "");
|
|
140
|
-
return clean === "" ? "index.html" : `${clean}/index.html`;
|
|
141
|
-
}
|
|
142
|
-
var buildCommand = defineCommand({
|
|
143
|
-
meta: { name: "build", description: "Prerender pages to deployable HTML + client bundles" },
|
|
144
|
-
args: {
|
|
145
|
-
root: { type: "positional", required: false, description: "Project root", default: "." },
|
|
146
|
-
outDir: { type: "string", description: "Output directory", default: "dist" },
|
|
147
|
-
islands: { type: "boolean", description: "Static-first islands mode (zero-JS static)", default: false },
|
|
148
|
-
server: { type: "boolean", description: "Build a Node server (dynamic routes + API/MCP)", default: false }
|
|
149
|
-
},
|
|
150
|
-
async run({ args }) {
|
|
151
|
-
const root = resolve(process.cwd(), args.root);
|
|
152
|
-
const outDir = resolve(root, args.outDir);
|
|
153
|
-
rmSync(outDir, { recursive: true, force: true });
|
|
154
|
-
const routes = scanPages(root);
|
|
155
|
-
const staticRoutes = routes.filter((r) => !r.isDynamic);
|
|
156
|
-
const dynamic = routes.filter((r) => r.isDynamic);
|
|
157
|
-
if (args.server) {
|
|
158
|
-
return buildServerTarget(root, outDir, args.outDir, routes);
|
|
159
|
-
}
|
|
160
|
-
const hrefs = args.islands ? /* @__PURE__ */ new Map() : await buildClient(root, staticRoutes, outDir);
|
|
161
|
-
const vite = await createViteServer({
|
|
162
|
-
root,
|
|
163
|
-
appType: "custom",
|
|
164
|
-
server: { middlewareMode: true },
|
|
165
|
-
plugins: [apex3({ clientRuntime: "@apex-stack/core/client" })]
|
|
166
|
-
});
|
|
167
|
-
try {
|
|
168
|
-
const { registry, css: componentCss } = await loadComponents(
|
|
169
|
-
root,
|
|
170
|
-
(id) => vite.ssrLoadModule(id)
|
|
171
|
-
);
|
|
172
|
-
for (const route of staticRoutes) {
|
|
173
|
-
const common = {
|
|
174
|
-
loadModule: (id) => vite.ssrLoadModule(id),
|
|
175
|
-
pageId: route.pageId,
|
|
176
|
-
url: route.pattern,
|
|
177
|
-
registry,
|
|
178
|
-
componentCss
|
|
179
|
-
};
|
|
180
|
-
const html = args.islands ? await renderIslandsPage(common) : await renderPage({ ...common, clientHref: hrefs.get(route.pageId) });
|
|
181
|
-
const dest = join3(outDir, outFile(route.pattern));
|
|
182
|
-
mkdirSync(dirname(dest), { recursive: true });
|
|
183
|
-
writeFileSync(dest, html);
|
|
184
|
-
console.log(` \u2713 ${route.pattern} \u2192 ${outFile(route.pattern)}`);
|
|
185
|
-
}
|
|
186
|
-
const pub = join3(root, "public");
|
|
187
|
-
if (existsSync2(pub)) cpSync(pub, outDir, { recursive: true });
|
|
188
|
-
console.log(
|
|
189
|
-
`
|
|
190
|
-
Built ${staticRoutes.length} page(s) \u2192 ${args.outDir}/` + (args.islands ? " (islands / static-first)" : " (prerendered + hydrated)")
|
|
191
|
-
);
|
|
192
|
-
if (dynamic.length) {
|
|
193
|
-
console.log(
|
|
194
|
-
` Skipped ${dynamic.length} dynamic route(s): ${dynamic.map((r) => r.pattern).join(", ")} (server target on the roadmap).`
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
console.log();
|
|
198
|
-
} finally {
|
|
199
|
-
await vite.close();
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
async function buildServerTarget(root, outDir, outLabel, routes) {
|
|
204
|
-
const clientHrefs = await buildClient(root, routes, outDir);
|
|
205
|
-
const server = await buildServer(root, routes, outDir);
|
|
206
|
-
const components = {};
|
|
207
|
-
const compDir = join3(root, "components");
|
|
208
|
-
if (existsSync2(compDir)) {
|
|
209
|
-
for (const f of readdirSync2(compDir).filter((f2) => f2.endsWith(".alpine"))) {
|
|
210
|
-
const sf = server.modules[`/components/${f}`];
|
|
211
|
-
if (sf) components[f.replace(/\.alpine$/, "")] = sf;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
const api = [];
|
|
215
|
-
const apiDir = join3(root, "server", "api");
|
|
216
|
-
if (existsSync2(apiDir)) {
|
|
217
|
-
for (const f of readdirSync2(apiDir).filter((f2) => /\.(ts|js|mjs)$/.test(f2))) {
|
|
218
|
-
const sf = server.modules[`/server/api/${f}`];
|
|
219
|
-
if (sf) api.push({ name: f.replace(/\.(ts|js|mjs)$/, ""), serverFile: sf });
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const manifest = {
|
|
223
|
-
islands: false,
|
|
224
|
-
routes: routes.map((r) => ({
|
|
225
|
-
...r,
|
|
226
|
-
serverFile: server.modules[r.pageId],
|
|
227
|
-
clientHref: clientHrefs.get(r.pageId)
|
|
228
|
-
})),
|
|
229
|
-
components,
|
|
230
|
-
api
|
|
231
|
-
};
|
|
232
|
-
writeFileSync(join3(outDir, "apex-manifest.json"), JSON.stringify(manifest, null, 2));
|
|
233
|
-
const pub = join3(root, "public");
|
|
234
|
-
if (existsSync2(pub)) cpSync(pub, outDir, { recursive: true });
|
|
235
|
-
console.log(
|
|
236
|
-
`
|
|
237
|
-
Built server target \u2192 ${outLabel}/ (${routes.length} route(s), ${api.length} API module(s))
|
|
238
|
-
Run it: apex start
|
|
239
|
-
`
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// src/commands/make.ts
|
|
244
|
-
import { existsSync as existsSync3, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
245
|
-
import { dirname as dirname2, join as join4, resolve as resolve2 } from "path";
|
|
246
|
-
import { defineCommand as defineCommand2 } from "citty";
|
|
247
|
-
function pageTemplate(name) {
|
|
248
|
-
return `<script server lang="ts">
|
|
249
|
-
export function loader() {
|
|
250
|
-
return { title: '${name}' }
|
|
251
|
-
}
|
|
252
|
-
</script>
|
|
253
|
-
|
|
254
|
-
<template x-data>
|
|
255
|
-
<main>
|
|
256
|
-
<h1 x-text="title"></h1>
|
|
257
|
-
</main>
|
|
258
|
-
</template>
|
|
259
|
-
|
|
260
|
-
<style scoped>
|
|
261
|
-
main { max-width: 40rem; margin: 3rem auto; font-family: system-ui, sans-serif; }
|
|
262
|
-
</style>
|
|
263
|
-
`;
|
|
264
|
-
}
|
|
265
|
-
function componentTemplate() {
|
|
266
|
-
return `<template x-data="{ count: 0 }">
|
|
267
|
-
<button @click="count++" x-text="count"></button>
|
|
268
|
-
</template>
|
|
269
|
-
|
|
270
|
-
<style scoped>
|
|
271
|
-
button { cursor: pointer; }
|
|
272
|
-
</style>
|
|
273
|
-
`;
|
|
274
|
-
}
|
|
275
|
-
function apiTemplate(name) {
|
|
276
|
-
return `import { defineApexRoute } from '@apex-stack/core'
|
|
277
|
-
import { z } from 'zod'
|
|
278
|
-
|
|
279
|
-
// GET /api/${name} \xB7 MCP tool "${name}"
|
|
280
|
-
export default defineApexRoute({
|
|
281
|
-
method: 'GET',
|
|
282
|
-
description: 'Describe what ${name} does',
|
|
283
|
-
input: { name: z.string() },
|
|
284
|
-
mcp: true,
|
|
285
|
-
handler: ({ input }) => ({ message: \`Hello, \${input.name}!\` }),
|
|
286
|
-
})
|
|
287
|
-
`;
|
|
288
|
-
}
|
|
289
|
-
function plan(kind, name, root) {
|
|
290
|
-
switch (kind) {
|
|
291
|
-
case "page":
|
|
292
|
-
return { path: join4(root, "pages", `${name}.alpine`), contents: pageTemplate(name) };
|
|
293
|
-
case "component":
|
|
294
|
-
return { path: join4(root, "components", `${name}.alpine`), contents: componentTemplate() };
|
|
295
|
-
case "api":
|
|
296
|
-
return { path: join4(root, "server", "api", `${name}.ts`), contents: apiTemplate(name) };
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
var makeCommand = defineCommand2({
|
|
300
|
-
meta: { name: "make", description: "Generate a page, component, or API route" },
|
|
301
|
-
args: {
|
|
302
|
-
kind: { type: "positional", required: true, description: "page | component | api" },
|
|
303
|
-
name: { type: "positional", required: true, description: "Name (about, Counter, todos, \u2026)" },
|
|
304
|
-
root: { type: "string", description: "Project root", default: "." }
|
|
305
|
-
},
|
|
306
|
-
run({ args }) {
|
|
307
|
-
const kind = args.kind;
|
|
308
|
-
if (kind !== "page" && kind !== "component" && kind !== "api") {
|
|
309
|
-
console.error(`
|
|
310
|
-
Unknown type "${args.kind}". Use: page | component | api
|
|
311
|
-
`);
|
|
312
|
-
process.exit(1);
|
|
313
|
-
}
|
|
314
|
-
const root = resolve2(process.cwd(), args.root);
|
|
315
|
-
const { path, contents } = plan(kind, args.name, root);
|
|
316
|
-
if (existsSync3(path)) {
|
|
317
|
-
console.error(`
|
|
318
|
-
\u2717 Already exists: ${path}
|
|
319
|
-
`);
|
|
320
|
-
process.exit(1);
|
|
321
|
-
}
|
|
322
|
-
mkdirSync2(dirname2(path), { recursive: true });
|
|
323
|
-
writeFileSync2(path, contents);
|
|
324
|
-
console.log(`
|
|
325
|
-
\u2713 Created ${path.replace(`${root}/`, "")}
|
|
326
|
-
`);
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
// src/commands/mcp.ts
|
|
331
|
-
import { defineCommand as defineCommand3 } from "citty";
|
|
332
|
-
var mcpCommand = defineCommand3({
|
|
333
|
-
meta: { name: "mcp", description: "Inspect the local MCP server (list or call tools)" },
|
|
334
|
-
args: {
|
|
335
|
-
url: { type: "string", description: "MCP endpoint URL", default: "http://localhost:3000/mcp" },
|
|
336
|
-
call: { type: "string", description: "Name of a tool to call" },
|
|
337
|
-
args: { type: "string", description: "JSON arguments for --call", default: "{}" }
|
|
338
|
-
},
|
|
339
|
-
async run({ args }) {
|
|
340
|
-
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
|
|
341
|
-
const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
342
|
-
const client = new Client({ name: "apex-mcp-cli", version: "0.0.0" });
|
|
343
|
-
try {
|
|
344
|
-
await client.connect(new StreamableHTTPClientTransport(new URL(args.url)));
|
|
345
|
-
} catch (err) {
|
|
346
|
-
console.error(
|
|
347
|
-
`
|
|
348
|
-
Could not reach an MCP server at ${args.url}
|
|
349
|
-
Is \`apex dev\` running? ${err.message}
|
|
350
|
-
`
|
|
351
|
-
);
|
|
352
|
-
process.exit(1);
|
|
353
|
-
}
|
|
354
|
-
if (args.call) {
|
|
355
|
-
const result = await client.callTool({
|
|
356
|
-
name: args.call,
|
|
357
|
-
arguments: JSON.parse(args.args)
|
|
358
|
-
});
|
|
359
|
-
console.log(`
|
|
360
|
-
\x1B[36m${args.call}\x1B[0m(${args.args}) \u2192`);
|
|
361
|
-
for (const part of result.content) {
|
|
362
|
-
console.log(" " + (part.text ?? JSON.stringify(part)));
|
|
363
|
-
}
|
|
364
|
-
console.log();
|
|
365
|
-
} else {
|
|
366
|
-
const { tools } = await client.listTools();
|
|
367
|
-
console.log(`
|
|
368
|
-
\x1B[36mMCP tools\x1B[0m at ${args.url} (${tools.length})
|
|
369
|
-
`);
|
|
370
|
-
for (const t of tools) {
|
|
371
|
-
const props = t.inputSchema?.properties;
|
|
372
|
-
const sig = props ? Object.entries(props).map(([k, v]) => `${k}: ${v.type ?? "any"}`).join(", ") : "";
|
|
373
|
-
console.log(` \u2022 \x1B[1m${t.name}\x1B[0m(${sig})`);
|
|
374
|
-
if (t.description) console.log(` ${t.description}`);
|
|
375
|
-
}
|
|
376
|
-
console.log(`
|
|
377
|
-
Call one: apex mcp --call <name> --args '{...}'
|
|
378
|
-
`);
|
|
379
|
-
}
|
|
380
|
-
await client.close();
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
// src/commands/migrate.ts
|
|
385
|
-
import { createRequire } from "module";
|
|
386
|
-
import { join as join5, resolve as resolve3 } from "path";
|
|
387
|
-
import { pathToFileURL } from "url";
|
|
388
|
-
import { defineCommand as defineCommand4 } from "citty";
|
|
389
|
-
var migrateCommand = defineCommand4({
|
|
390
|
-
meta: { name: "migrate", description: "Apply pending SQL migrations (db/migrations/*.sql)" },
|
|
391
|
-
args: {
|
|
392
|
-
db: { type: "string", description: "SQLite file path (libSQL)", default: "data.db" },
|
|
393
|
-
driver: { type: "string", description: "sqlite | postgres | pglite", default: "sqlite" },
|
|
394
|
-
url: { type: "string", description: "Connection URL (postgres) \u2014 overrides --db" },
|
|
395
|
-
dir: { type: "string", description: "Migrations directory", default: "db/migrations" },
|
|
396
|
-
root: { type: "string", description: "Project root", default: "." }
|
|
397
|
-
},
|
|
398
|
-
async run({ args }) {
|
|
399
|
-
const root = resolve3(process.cwd(), args.root);
|
|
400
|
-
let data;
|
|
401
|
-
try {
|
|
402
|
-
const require2 = createRequire(join5(root, "package.json"));
|
|
403
|
-
data = await import(pathToFileURL(require2.resolve("@apex-stack/data")).href);
|
|
404
|
-
} catch {
|
|
405
|
-
console.error("\n @apex-stack/data is not installed in this project. Run: npm i @apex-stack/data\n");
|
|
406
|
-
process.exit(1);
|
|
407
|
-
}
|
|
408
|
-
const config = args.driver === "postgres" ? { driver: "postgres", url: args.url } : args.driver === "pglite" ? { driver: "pglite", dir: args.url } : resolve3(root, args.db);
|
|
409
|
-
const handle = await data.createDb(config);
|
|
410
|
-
const applied = await data.applyMigrations(handle, resolve3(root, args.dir));
|
|
411
|
-
await handle.close();
|
|
412
|
-
console.log(
|
|
413
|
-
applied.length ? `
|
|
414
|
-
\u2713 Applied ${applied.length} migration(s): ${applied.join(", ")}
|
|
415
|
-
` : "\n \u2713 Up to date \u2014 no pending migrations.\n"
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
});
|
|
9
|
+
import { defineCommand as defineCommand2, runMain } from "citty";
|
|
419
10
|
|
|
420
11
|
// src/commands/new.ts
|
|
421
|
-
import { spawnSync } from "child_process";
|
|
422
|
-
import { cpSync
|
|
423
|
-
import { basename, join
|
|
12
|
+
import { spawn, spawnSync } from "child_process";
|
|
13
|
+
import { cpSync, existsSync, readdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
14
|
+
import { basename, join, resolve } from "path";
|
|
424
15
|
import { fileURLToPath } from "url";
|
|
425
|
-
import { defineCommand
|
|
16
|
+
import { defineCommand } from "citty";
|
|
426
17
|
var TEMPLATE_DIR = fileURLToPath(new URL("../templates/default", import.meta.url));
|
|
427
18
|
function detectPackageManager() {
|
|
428
19
|
const ua = process.env.npm_config_user_agent || "";
|
|
@@ -431,232 +22,112 @@ function detectPackageManager() {
|
|
|
431
22
|
if (ua.startsWith("bun")) return "bun";
|
|
432
23
|
return "npm";
|
|
433
24
|
}
|
|
434
|
-
function
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
25
|
+
function runSync(cmd, args, cwd) {
|
|
26
|
+
return spawnSync(cmd, args, { cwd, stdio: "ignore", shell: process.platform === "win32" }).status === 0;
|
|
27
|
+
}
|
|
28
|
+
function installAsync(pm, cwd) {
|
|
29
|
+
const args = pm === "npm" ? ["install", "--no-audit", "--no-fund"] : ["install"];
|
|
30
|
+
return new Promise((res) => {
|
|
31
|
+
const child = spawn(pm, args, { cwd, stdio: "ignore", shell: process.platform === "win32" });
|
|
32
|
+
child.on("close", (code) => res(code === 0));
|
|
33
|
+
child.on("error", () => res(false));
|
|
440
34
|
});
|
|
441
|
-
return res.status === 0;
|
|
442
35
|
}
|
|
443
|
-
var
|
|
444
|
-
cyan: (s) => `\x1B[36m${s}\x1B[0m`,
|
|
445
|
-
dim: (s) => `\x1B[2m${s}\x1B[0m`,
|
|
446
|
-
green: (s) => `\x1B[32m${s}\x1B[0m`,
|
|
447
|
-
yellow: (s) => `\x1B[33m${s}\x1B[0m`
|
|
448
|
-
};
|
|
449
|
-
var newCommand = defineCommand5({
|
|
36
|
+
var newCommand = defineCommand({
|
|
450
37
|
meta: { name: "new", description: "Scaffold a new Apex JS app" },
|
|
451
38
|
args: {
|
|
452
39
|
dir: { type: "positional", required: false, description: "Target directory", default: "apex-app" },
|
|
453
40
|
install: { type: "boolean", default: true, description: "Install dependencies (use --no-install to skip)" },
|
|
454
41
|
git: { type: "boolean", default: true, description: "Initialize a git repository (use --no-git to skip)" }
|
|
455
42
|
},
|
|
456
|
-
run({ args }) {
|
|
457
|
-
const
|
|
43
|
+
async run({ args }) {
|
|
44
|
+
const dir = String(args.dir);
|
|
45
|
+
const target = resolve(process.cwd(), dir);
|
|
458
46
|
const name = basename(target);
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
47
|
+
process.stdout.write(banner());
|
|
48
|
+
const log = console.log;
|
|
49
|
+
if (existsSync(target) && readdirSync(target).length > 0) {
|
|
50
|
+
console.error(` ${color.red("\u2717")} Target directory is not empty: ${target}
|
|
462
51
|
`);
|
|
463
52
|
process.exit(1);
|
|
464
53
|
}
|
|
465
|
-
|
|
466
|
-
const gitignore =
|
|
467
|
-
if (
|
|
54
|
+
cpSync(TEMPLATE_DIR, target, { recursive: true });
|
|
55
|
+
const gitignore = join(target, "_gitignore");
|
|
56
|
+
if (existsSync(gitignore)) renameSync(gitignore, join(target, ".gitignore"));
|
|
468
57
|
for (const rel of ["package.json", "README.md"]) {
|
|
469
|
-
const file =
|
|
470
|
-
if (
|
|
58
|
+
const file = join(target, rel);
|
|
59
|
+
if (existsSync(file)) writeFileSync(file, readFileSync(file, "utf8").replaceAll("{{name}}", name));
|
|
471
60
|
}
|
|
472
|
-
|
|
473
|
-
log(`
|
|
474
|
-
${c.cyan("Apex JS")} app created in ${args.dir}`);
|
|
61
|
+
log(` ${color.green("\u2713")} Created ${color.bold(dir)}`);
|
|
475
62
|
const pm = detectPackageManager();
|
|
476
63
|
let gitOk = false;
|
|
477
64
|
if (args.git) {
|
|
478
65
|
const hasGit = spawnSync("git", ["--version"], { stdio: "ignore", shell: process.platform === "win32" }).status === 0;
|
|
479
|
-
if (hasGit &&
|
|
480
|
-
|
|
481
|
-
|
|
66
|
+
if (hasGit && runSync("git", ["init", "-q"], target)) {
|
|
67
|
+
runSync("git", ["add", "-A"], target);
|
|
68
|
+
runSync("git", ["commit", "-m", "Initial commit from Apex JS", "--no-gpg-sign"], target);
|
|
482
69
|
gitOk = true;
|
|
483
70
|
}
|
|
484
71
|
}
|
|
72
|
+
if (gitOk) log(` ${color.green("\u2713")} Initialized a git repository`);
|
|
485
73
|
let installed = false;
|
|
486
74
|
if (args.install) {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
`);
|
|
490
|
-
|
|
491
|
-
installed = run(pm, installArgs, target);
|
|
492
|
-
if (!installed) log(`
|
|
493
|
-
${c.yellow("\u26A0")} Dependency install failed \u2014 run it yourself after cd'ing in.
|
|
494
|
-
`);
|
|
75
|
+
const sp = spinner(`Installing dependencies with ${pm}\u2026 ${color.dim("(first run can take a minute)")}`);
|
|
76
|
+
installed = await installAsync(pm, target);
|
|
77
|
+
if (installed) sp.succeed(`Dependencies installed with ${pm}`);
|
|
78
|
+
else sp.fail(`Install failed \u2014 run ${color.cyan(`${pm} install`)} inside ${dir}`);
|
|
495
79
|
}
|
|
496
80
|
const runPrefix = pm === "npm" ? "npm run" : pm;
|
|
497
|
-
const steps = [`cd ${args.dir}`];
|
|
498
|
-
if (!installed) steps.push(pm === "yarn" ? "yarn" : `${pm} install`);
|
|
499
|
-
steps.push(`apex dev ${c.dim("# start the dev server \u2192 http://localhost:3000")}`);
|
|
500
81
|
log(`
|
|
501
|
-
${
|
|
502
|
-
${
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
${
|
|
507
|
-
`)
|
|
508
|
-
}
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
// src/commands/start.ts
|
|
512
|
-
import { existsSync as existsSync6 } from "fs";
|
|
513
|
-
import { join as join8, resolve as resolve5 } from "path";
|
|
514
|
-
import { defineCommand as defineCommand6 } from "citty";
|
|
515
|
-
|
|
516
|
-
// src/prod/server.ts
|
|
517
|
-
import { createServer as createHttpServer } from "http";
|
|
518
|
-
import { existsSync as existsSync5, readFileSync as readFileSync3, statSync } from "fs";
|
|
519
|
-
import { join as join7 } from "path";
|
|
520
|
-
import { pathToFileURL as pathToFileURL2 } from "url";
|
|
521
|
-
import {
|
|
522
|
-
createApp,
|
|
523
|
-
defineEventHandler,
|
|
524
|
-
getRequestURL,
|
|
525
|
-
setResponseHeader,
|
|
526
|
-
setResponseStatus,
|
|
527
|
-
toNodeListener
|
|
528
|
-
} from "h3";
|
|
529
|
-
var MIME = {
|
|
530
|
-
".js": "text/javascript",
|
|
531
|
-
".mjs": "text/javascript",
|
|
532
|
-
".css": "text/css",
|
|
533
|
-
".html": "text/html",
|
|
534
|
-
".json": "application/json",
|
|
535
|
-
".svg": "image/svg+xml",
|
|
536
|
-
".png": "image/png",
|
|
537
|
-
".jpg": "image/jpeg",
|
|
538
|
-
".ico": "image/x-icon",
|
|
539
|
-
".woff2": "font/woff2"
|
|
540
|
-
};
|
|
541
|
-
async function startProdServer(options) {
|
|
542
|
-
const dir = options.dir;
|
|
543
|
-
const port = options.port ?? 3e3;
|
|
544
|
-
const manifest = JSON.parse(readFileSync3(join7(dir, "apex-manifest.json"), "utf8"));
|
|
545
|
-
const importServer = (relFile) => import(pathToFileURL2(join7(dir, "server", relFile)).href);
|
|
546
|
-
const registry = {};
|
|
547
|
-
let componentCss = "";
|
|
548
|
-
for (const [name, file] of Object.entries(manifest.components)) {
|
|
549
|
-
const mod = await importServer(file);
|
|
550
|
-
registry[name] = { template: mod.template, rootXData: mod.rootXData, scopeId: mod.scopeId };
|
|
551
|
-
if (mod.css) componentCss += `${mod.css}
|
|
552
|
-
`;
|
|
553
|
-
}
|
|
554
|
-
const apiEntries = [];
|
|
555
|
-
for (const { name, serverFile } of manifest.api) {
|
|
556
|
-
const mod = await importServer(serverFile);
|
|
557
|
-
apiEntries.push(...expandApiModule(name, mod.default));
|
|
558
|
-
}
|
|
559
|
-
const serverFileFor = new Map(manifest.routes.map((r) => [r.pageId, r.serverFile]));
|
|
560
|
-
const loadModule = (id) => importServer(serverFileFor.get(id));
|
|
561
|
-
const app = createApp();
|
|
562
|
-
app.use(
|
|
563
|
-
defineEventHandler((event) => {
|
|
564
|
-
if (event.method !== "GET") return;
|
|
565
|
-
const path = decodeURIComponent(getRequestURL(event).pathname);
|
|
566
|
-
if (path === "/" || path.startsWith("/api") || path === "/mcp") return;
|
|
567
|
-
const file = join7(dir, path);
|
|
568
|
-
if (!file.startsWith(dir) || !existsSync5(file) || !statSync(file).isFile()) return;
|
|
569
|
-
const ext = path.slice(path.lastIndexOf("."));
|
|
570
|
-
setResponseHeader(event, "Content-Type", MIME[ext] ?? "application/octet-stream");
|
|
571
|
-
if (path.startsWith("/assets/")) setResponseHeader(event, "Cache-Control", "public, max-age=31536000, immutable");
|
|
572
|
-
return readFileSync3(file);
|
|
573
|
-
})
|
|
574
|
-
);
|
|
575
|
-
if (apiEntries.length) app.use("/api", createApiHandler(apiEntries));
|
|
576
|
-
if (hasMcpRoutes(apiEntries)) app.use("/mcp", createMcpHandler(apiEntries));
|
|
577
|
-
app.use(
|
|
578
|
-
defineEventHandler(async (event) => {
|
|
579
|
-
const url = getRequestURL(event).pathname;
|
|
580
|
-
const matched = matchRoute(manifest.routes, url);
|
|
581
|
-
if (!matched) {
|
|
582
|
-
setResponseStatus(event, 404);
|
|
583
|
-
setResponseHeader(event, "Content-Type", "text/html");
|
|
584
|
-
return `<!DOCTYPE html><h1>404 \u2014 ${url}</h1>`;
|
|
585
|
-
}
|
|
586
|
-
const route = manifest.routes.find((r) => r.pageId === matched.pageId);
|
|
587
|
-
const render = manifest.islands ? renderIslandsPage : renderPage;
|
|
588
|
-
const html = await render({
|
|
589
|
-
loadModule,
|
|
590
|
-
pageId: matched.pageId,
|
|
591
|
-
params: matched.params,
|
|
592
|
-
url,
|
|
593
|
-
registry,
|
|
594
|
-
componentCss,
|
|
595
|
-
clientHref: route?.clientHref
|
|
596
|
-
});
|
|
597
|
-
setResponseHeader(event, "Content-Type", "text/html");
|
|
598
|
-
return html;
|
|
599
|
-
})
|
|
600
|
-
);
|
|
601
|
-
const server = createHttpServer(toNodeListener(app));
|
|
602
|
-
await new Promise((resolve7) => server.listen(port, resolve7));
|
|
603
|
-
return { server, port };
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
// src/commands/start.ts
|
|
607
|
-
var startCommand = defineCommand6({
|
|
608
|
-
meta: { name: "start", description: "Run a production build (from `apex build --server`)" },
|
|
609
|
-
args: {
|
|
610
|
-
dir: { type: "positional", required: false, description: "Build directory", default: "dist" },
|
|
611
|
-
port: { type: "string", description: "Port to listen on", default: "3000" }
|
|
612
|
-
},
|
|
613
|
-
async run({ args }) {
|
|
614
|
-
const dir = resolve5(process.cwd(), args.dir);
|
|
615
|
-
if (!existsSync6(join8(dir, "apex-manifest.json"))) {
|
|
616
|
-
console.error(`
|
|
617
|
-
No build found in ${args.dir}/. Run \`apex build --server\` first.
|
|
618
|
-
`);
|
|
619
|
-
process.exit(1);
|
|
620
|
-
}
|
|
621
|
-
const { port } = await startProdServer({ dir, port: Number(args.port) });
|
|
622
|
-
console.log(`
|
|
623
|
-
\x1B[36mApex JS\x1B[0m production server
|
|
624
|
-
\u2192 http://localhost:${port}
|
|
82
|
+
${color.bold("Next steps")}`);
|
|
83
|
+
log(` ${color.cyan(`cd ${dir}`)}`);
|
|
84
|
+
if (!installed) log(` ${color.cyan(pm === "yarn" ? "yarn" : `${pm} install`)}`);
|
|
85
|
+
log(` ${color.cyan("apex dev")} ${color.gray("# \u2192 http://localhost:3000")}`);
|
|
86
|
+
log(`
|
|
87
|
+
${color.gray("Not installed globally? Use")} ${color.cyan(`${runPrefix} dev`)}${color.gray(".")}`);
|
|
88
|
+
log(` ${color.gray("Islands mode:")} ${color.cyan("apex dev --islands")}${color.gray(" \xB7 API routes are also MCP tools at /mcp.")}
|
|
625
89
|
`);
|
|
626
90
|
}
|
|
627
91
|
});
|
|
628
92
|
|
|
629
93
|
// src/cli.ts
|
|
630
|
-
var
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
const { port: actual } = await startDevServer({ root, port, islands: args.islands });
|
|
641
|
-
console.log(`
|
|
642
|
-
\x1B[36mApex JS\x1B[0m dev server ready
|
|
643
|
-
\u2192 http://localhost:${actual}
|
|
644
|
-
`);
|
|
645
|
-
}
|
|
646
|
-
});
|
|
647
|
-
var main = defineCommand7({
|
|
94
|
+
var COMMANDS = [
|
|
95
|
+
["new", "Scaffold a new app"],
|
|
96
|
+
["dev", "Start the dev server (SSR + hydrate, API + MCP)"],
|
|
97
|
+
["build", "Build for production (static, islands, or server)"],
|
|
98
|
+
["start", "Run a production server build"],
|
|
99
|
+
["make", "Generate a page, component, or API route"],
|
|
100
|
+
["migrate", "Apply pending database migrations"],
|
|
101
|
+
["mcp", "Inspect the MCP server \u2014 list or call tools"]
|
|
102
|
+
];
|
|
103
|
+
var main = defineCommand2({
|
|
648
104
|
meta: {
|
|
649
105
|
name: "apex",
|
|
650
106
|
description: "The full-stack meta-framework for Alpine.js"
|
|
651
107
|
},
|
|
652
108
|
subCommands: {
|
|
653
109
|
new: newCommand,
|
|
654
|
-
dev,
|
|
655
|
-
build: buildCommand,
|
|
656
|
-
start: startCommand,
|
|
657
|
-
make: makeCommand,
|
|
658
|
-
migrate: migrateCommand,
|
|
659
|
-
mcp: mcpCommand
|
|
110
|
+
dev: () => import("./dev-HDRJEPPN.js").then((m) => m.devCommand),
|
|
111
|
+
build: () => import("./build-QRHQUUZC.js").then((m) => m.buildCommand),
|
|
112
|
+
start: () => import("./start-VJJXI4W3.js").then((m) => m.startCommand),
|
|
113
|
+
make: () => import("./make-4LINTKZH.js").then((m) => m.makeCommand),
|
|
114
|
+
migrate: () => import("./migrate-NOGFOFV2.js").then((m) => m.migrateCommand),
|
|
115
|
+
mcp: () => import("./mcp-DL4J6JFJ.js").then((m) => m.mcpCommand)
|
|
116
|
+
},
|
|
117
|
+
// Shown for a bare `apex` (no subcommand): the brand banner + a command menu.
|
|
118
|
+
run({ rawArgs }) {
|
|
119
|
+
if (rawArgs.length > 0) return;
|
|
120
|
+
process.stdout.write(banner());
|
|
121
|
+
const log = console.log;
|
|
122
|
+
log(` ${color.bold("Usage")} ${color.gray("apex")} ${color.cyan("<command>")} ${color.gray("[options]")}
|
|
123
|
+
`);
|
|
124
|
+
log(` ${color.bold("Commands")}`);
|
|
125
|
+
for (const [name, desc] of COMMANDS) {
|
|
126
|
+
log(` ${color.cyan(`apex ${name}`.padEnd(13))} ${color.gray(desc)}`);
|
|
127
|
+
}
|
|
128
|
+
log(`
|
|
129
|
+
${color.gray("Run")} ${color.cyan("apex <command> --help")} ${color.gray("for details.")}
|
|
130
|
+
`);
|
|
660
131
|
}
|
|
661
132
|
});
|
|
662
133
|
runMain(main);
|