@brickkit/cli 0.6.0 → 0.7.0
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/README.md +138 -42
- package/dist/bin.d.ts.map +1 -1
- package/dist/bin.js +99 -52
- package/dist/bin.js.map +1 -1
- package/dist/build.js +82 -0
- package/dist/build.js.map +1 -0
- package/dist/client-contract.js +268 -0
- package/dist/client-contract.js.map +1 -0
- package/dist/compiler.d.ts +3 -0
- package/dist/compiler.js +1 -0
- package/dist/compiler2.js +10 -0
- package/dist/compiler2.js.map +1 -0
- package/dist/dev-worker.js +4 -2
- package/dist/dev-worker.js.map +1 -1
- package/dist/emit.d.ts +7 -0
- package/dist/emit.d.ts.map +1 -0
- package/dist/index.d.ts +13 -43
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/port-utils.js +13 -0
- package/dist/port-utils.js.map +1 -0
- package/dist/runner.js +78 -64
- package/dist/runner.js.map +1 -1
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +12 -48
- package/dist/vite.js.map +1 -1
- package/package.json +9 -3
- package/dist/client-contract-file.js +0 -135
- package/dist/client-contract-file.js.map +0 -1
- package/dist/native-client-contract.js +0 -46
- package/dist/native-client-contract.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,47 +1,111 @@
|
|
|
1
1
|
# @brickkit/cli
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
CLI and server engine for the Brick-TS framework. Mounts services,
|
|
4
|
+
actions, and resources as a typed HTTP API (Elysia) with OpenAPI 3.1,
|
|
5
|
+
Scalar/Swagger docs, request logging, and MCP routes.
|
|
6
6
|
|
|
7
7
|
You need Bun 1.2 or later.
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
bun add
|
|
10
|
+
bun add @brickkit/cli @brickkit/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { brick } from "@brickkit/core";
|
|
15
|
+
|
|
16
|
+
const app = brick({ title: "Pages API", version: "1.0.0" });
|
|
17
|
+
app.listen(3333);
|
|
11
18
|
```
|
|
12
19
|
|
|
13
20
|
## Commands
|
|
14
21
|
|
|
15
22
|
Every command except `dev` and `start` takes a required `<entry>`: a module
|
|
16
|
-
whose default export is
|
|
17
|
-
not start a listener itself.
|
|
23
|
+
whose default export is a `BrickApp` that does not call `.listen()`.
|
|
18
24
|
|
|
19
25
|
```sh
|
|
20
26
|
brick dev [entry] [--port 4000]
|
|
21
27
|
brick start [entry] [--port 4000]
|
|
22
|
-
brick build <entry> [--outdir dist/brick] [--port <number>]
|
|
28
|
+
brick build <entry> [--outdir dist/brick] [--port <number>] [--no-aot]
|
|
23
29
|
brick gen openapi <entry> [--output openapi.json]
|
|
24
30
|
brick gen client <entry> [--output _brick/contract.ts]
|
|
25
31
|
brick info <entry>
|
|
26
32
|
```
|
|
27
33
|
|
|
28
|
-
|
|
29
|
-
`brick build` bundles the app into a Bun server and writes `brick-ir.json`
|
|
30
|
-
(version 2, target `effect-http-api`) describing services and routes.
|
|
31
|
-
`brick info` prints the same architecture JSON without starting anything.
|
|
34
|
+
Build the backend directly from registered services:
|
|
32
35
|
|
|
33
36
|
```ts
|
|
34
|
-
|
|
35
|
-
import {
|
|
36
|
-
import { Layer } from "effect";
|
|
37
|
-
import { usersService } from "./services/users";
|
|
37
|
+
import { brick } from "@brickkit/core";
|
|
38
|
+
import { pagesService, billingService } from "./services";
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
const app = brick({ services: [pagesService, billingService] });
|
|
40
41
|
```
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
The typed HTTP client is available from `@brickkit/core/client`. See
|
|
44
|
+
`www/content/docs/(brick)/typed-client.mdx` for routing and error handling.
|
|
45
|
+
|
|
46
|
+
The `brick` binary ships in this package for local dev and inspection.
|
|
47
|
+
`dev` and `start` accept an optional entry and resolve the port from
|
|
48
|
+
`--port`, then `PORT`, then 4000:
|
|
43
49
|
|
|
44
|
-
|
|
50
|
+
```sh
|
|
51
|
+
brick dev --port 4000
|
|
52
|
+
brick dev src/app.ts --port 4000
|
|
53
|
+
brick gen openapi src/app.ts --output openapi.json
|
|
54
|
+
brick gen client src/app.ts
|
|
55
|
+
brick info src/app.ts
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Source: <https://github.com/Chandraprakash-Darji/brick>.
|
|
59
|
+
|
|
60
|
+
## Action routes
|
|
61
|
+
|
|
62
|
+
Each action exposes exactly one route: `method` defaults to POST, and `path`
|
|
63
|
+
defaults to `/api/{service}/{action}` (or your configured prefix). Explicit
|
|
64
|
+
methods are honored regardless of the action name. A custom `path` includes
|
|
65
|
+
its own prefix and replaces the default path. OpenAPI describes the same
|
|
66
|
+
method and path, including path parameters.
|
|
67
|
+
|
|
68
|
+
To expose a read action over GET, declare `method: "GET"`. Action names beginning
|
|
69
|
+
with `get`, `list`, `find`, or `read` no longer add a GET alias. Custom paths no
|
|
70
|
+
longer expose default action aliases or an additional method. Resource CRUD
|
|
71
|
+
routes keep their REST methods.
|
|
72
|
+
|
|
73
|
+
## Compiler
|
|
74
|
+
|
|
75
|
+
The server compiles specialized JavaScript handlers for all resource CRUD and
|
|
76
|
+
custom actions at startup, pre-renders documentation, and caches OpenAPI and
|
|
77
|
+
architecture JSON with definition/endpoint invalidation. Health retains live
|
|
78
|
+
uptime and timestamps. Build a
|
|
79
|
+
module whose default export is a `BrickApp` into a runnable Bun server with:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
brick build src/app.ts --outdir dist/brick
|
|
83
|
+
bun dist/brick/server.js
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Use the same app entry as `brick gen client`. The build preserves its selected
|
|
87
|
+
services, prefix, docs settings, endpoints, and Elysia hooks. Keep `.listen()`
|
|
88
|
+
in a separate server bootstrap.
|
|
89
|
+
|
|
90
|
+
This beta branch pins Elysia `2.0.0-beta.29`. `brick build` also precompiles
|
|
91
|
+
Elysia's HTTP handlers with its Bun AOT plugin and removes the runtime HTTP
|
|
92
|
+
compiler. Use `brick build src/app.ts --no-aot` for a regular bundle.
|
|
93
|
+
Development and direct `brick()` calls still use runtime HTTP compilation;
|
|
94
|
+
both modes share the same services and Brick handlers.
|
|
95
|
+
|
|
96
|
+
## Automatic typed client with Vite
|
|
97
|
+
|
|
98
|
+
Add `brickClient` before your framework plugins. Its entry can be any module
|
|
99
|
+
whose default export is a `BrickApp`; construct the app without calling
|
|
100
|
+
`.listen()` there:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
// src/api/app.ts
|
|
104
|
+
import { brick } from "@brickkit/core";
|
|
105
|
+
import { pagesService } from "./services";
|
|
106
|
+
|
|
107
|
+
export default brick({ prefix: "/v2", services: [pagesService] });
|
|
108
|
+
```
|
|
45
109
|
|
|
46
110
|
```ts
|
|
47
111
|
// vite.config.ts
|
|
@@ -55,45 +119,77 @@ export default defineConfig({
|
|
|
55
119
|
});
|
|
56
120
|
```
|
|
57
121
|
|
|
58
|
-
Vite
|
|
59
|
-
production builds
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
The generated module exports the API definition and the client factories:
|
|
122
|
+
Vite creates `_brick/contract.ts` before loading the frontend and before
|
|
123
|
+
production builds. The prefix and selected services come from the app. Backend
|
|
124
|
+
edits regenerate the file; unchanged metadata does not rewrite it. There is no
|
|
125
|
+
separate generation command or manually maintained action list.
|
|
64
126
|
|
|
65
127
|
```ts
|
|
66
|
-
|
|
128
|
+
// src/client.ts
|
|
129
|
+
import { createBrickClient } from "@brickkit/core/client";
|
|
130
|
+
import { contract } from "../_brick/contract";
|
|
67
131
|
|
|
68
|
-
const
|
|
69
|
-
|
|
132
|
+
export const api = createBrickClient({
|
|
133
|
+
baseUrl: "http://localhost:8085",
|
|
134
|
+
contract,
|
|
135
|
+
});
|
|
70
136
|
```
|
|
71
137
|
|
|
138
|
+
The generated file contains transport types and route metadata, with no runtime
|
|
139
|
+
imports or backend handlers. HTTP actions are grouped by service name; dotted
|
|
140
|
+
action names become nested namespaces. Resources use their names at the root
|
|
141
|
+
and their normal REST routes. MCP-only tools are excluded. Add `_brick/` to
|
|
142
|
+
`.gitignore`; customize its location with `output` in the plugin options.
|
|
143
|
+
|
|
72
144
|
For TanStack Start or another embedded server, import the app from
|
|
73
145
|
`virtual:brick-app` in server code and forward requests to `app.fetch(request)`.
|
|
74
|
-
|
|
75
|
-
|
|
146
|
+
Vite tracks the entry and its dependencies for backend hot reload. Browser
|
|
147
|
+
imports of this server module are rejected. Add
|
|
148
|
+
`/// <reference types="@brickkit/cli/vite/client" />` to your Vite environment
|
|
149
|
+
declarations. See `examples/pages` for a complete Start integration.
|
|
150
|
+
|
|
151
|
+
Generation evaluates the app entry, so the configuration it needs to construct
|
|
152
|
+
the app must be available in development and at build time. Keep startup jobs
|
|
153
|
+
and connection-opening initialization in request handlers or your server
|
|
154
|
+
bootstrap, outside the app definition module. Input/output types
|
|
155
|
+
come from JSON transport schemas; absent schemas produce `unknown`. Unresolved
|
|
156
|
+
schema references and non-JSON transport types fail generation explicitly.
|
|
157
|
+
This integration requires Vite 8; the normal server entry point has no Vite
|
|
158
|
+
runtime dependency. `generateClientContract(app)` exposes the source generator
|
|
159
|
+
for non-Vite tooling.
|
|
76
160
|
|
|
77
|
-
|
|
78
|
-
development and build time. Keep startup jobs and connection-opening work in
|
|
79
|
-
layers or your server bootstrap, not at module scope. The Vite integration
|
|
80
|
-
requires Vite 8.
|
|
161
|
+
## Generate a typed client without Vite
|
|
81
162
|
|
|
82
|
-
|
|
163
|
+
Use the standalone Bun CLI with any module whose default export is a `BrickApp`:
|
|
83
164
|
|
|
84
165
|
```sh
|
|
166
|
+
brick gen client src/app.ts
|
|
85
167
|
brick gen client src/app.ts --output frontend/src/contract.ts
|
|
86
168
|
```
|
|
87
169
|
|
|
88
|
-
The
|
|
89
|
-
|
|
90
|
-
|
|
170
|
+
The default output is `_brick/contract.ts`, relative to the working directory.
|
|
171
|
+
The app's selected services and prefix determine its contents, just as with the
|
|
172
|
+
Vite plugin. The command evaluates the entry without calling `.listen()`; keep
|
|
173
|
+
the listener in a separate server bootstrap. Vite is not required or loaded.
|
|
174
|
+
|
|
175
|
+
Repeated transport type fragments are emitted as shared `BrickSchema_*` aliases
|
|
176
|
+
when the declarations reduce source size. Names are derived from the emitted
|
|
177
|
+
shape, so runtime-only constraints such as string length bounds do not prevent
|
|
178
|
+
sharing. Scalars stay inline, and reference targets retain document-scoped
|
|
179
|
+
aliases; equivalent recursive graphs in separate documents remain separate.
|
|
180
|
+
This applies to both standalone and Vite generation.
|
|
181
|
+
|
|
182
|
+
Run generation before your frontend's development server, build, and type check,
|
|
183
|
+
and rerun after changing backend definitions. You can add a package script:
|
|
91
184
|
|
|
92
185
|
```json
|
|
93
|
-
{
|
|
186
|
+
{
|
|
187
|
+
"scripts": {
|
|
188
|
+
"gen:client": "brick gen client src/app.ts"
|
|
189
|
+
}
|
|
190
|
+
}
|
|
94
191
|
```
|
|
95
192
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
Source: <https://github.com/Chandraprakash-Darji/brick>.
|
|
193
|
+
Import the generated `contract` with `createBrickClient({ baseUrl, contract })`
|
|
194
|
+
as shown above. The command creates parent directories and leaves an unchanged
|
|
195
|
+
contract file untouched. Add the generated output to `.gitignore`.
|
package/dist/bin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.d.ts","names":[],"sources":["../src/bin.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"bin.d.ts","names":[],"sources":["../src/bin.ts"],"mappings":";;wBAkDgB,iBAAiB"}
|
package/dist/bin.js
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
import {
|
|
3
|
-
import { n as
|
|
2
|
+
import { n as startDevServer, r as startProdServer } from "./runner.js";
|
|
3
|
+
import { n as loadBrickApp, t as buildApplication } from "./build.js";
|
|
4
|
+
import { t as generateClientContract } from "./client-contract.js";
|
|
5
|
+
import { t as parsePort } from "./port-utils.js";
|
|
6
|
+
import { generateOpenApiSpec, getGlobalRegistry } from "@brickkit/core";
|
|
7
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
8
|
+
import { dirname, join, resolve } from "node:path";
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
4
10
|
import { Command, InvalidArgumentError } from "commander";
|
|
5
|
-
//#region
|
|
6
|
-
|
|
11
|
+
//#region src/client-contract-file.ts
|
|
12
|
+
/** Generate a client contract without loading Vite or starting a server. */
|
|
13
|
+
async function writeClientContract(entryPath, outputPath) {
|
|
14
|
+
const entry = resolve(entryPath);
|
|
15
|
+
const output = resolve(outputPath ?? "_brick/contract.ts");
|
|
16
|
+
if (entry === output) throw new Error("Brick: contract output must not overwrite the app entry.");
|
|
17
|
+
const app = await loadBrickApp(entry);
|
|
18
|
+
const source = generateClientContract(app);
|
|
19
|
+
if (await readFile(output, "utf8").catch((error) => {
|
|
20
|
+
if (error.code !== "ENOENT") throw error;
|
|
21
|
+
}) !== source) {
|
|
22
|
+
await mkdir(dirname(output), { recursive: true });
|
|
23
|
+
const temporary = join(dirname(output), `.brick-contract-${randomUUID()}.tmp`);
|
|
24
|
+
try {
|
|
25
|
+
await writeFile(temporary, source, { flag: "wx" });
|
|
26
|
+
await rename(temporary, output);
|
|
27
|
+
} finally {
|
|
28
|
+
await rm(temporary, { force: true });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return output;
|
|
32
|
+
}
|
|
7
33
|
//#endregion
|
|
8
34
|
//#region src/bin.ts
|
|
35
|
+
const CLI_VERSION = "0.7.0";
|
|
9
36
|
const PORT_HELP = "Port to listen on (CLI --port overrides PORT env, default 4000)";
|
|
10
37
|
const PORT_RESOLUTION_HELP = `
|
|
11
38
|
Port Resolution (dev/start commands):
|
|
@@ -15,86 +42,106 @@ Port Resolution (dev/start commands):
|
|
|
15
42
|
- Valid range: 1-65535
|
|
16
43
|
|
|
17
44
|
Examples:
|
|
18
|
-
brick dev app.ts
|
|
19
|
-
|
|
45
|
+
brick dev app.ts # port 4000 (default)
|
|
46
|
+
brick dev app.ts --port 5000 # port 5000 (explicit)
|
|
47
|
+
PORT=3000 brick dev app.ts # port 3000 (from env)
|
|
48
|
+
brick dev app.ts --port 6000 # port 6000 (CLI overrides PORT=3000)
|
|
20
49
|
`;
|
|
21
50
|
function parsePortOption(value) {
|
|
22
51
|
try {
|
|
23
52
|
return parsePort(value, void 0);
|
|
24
|
-
} catch (
|
|
25
|
-
throw new InvalidArgumentError(
|
|
53
|
+
} catch (err) {
|
|
54
|
+
throw new InvalidArgumentError(err.message);
|
|
26
55
|
}
|
|
27
56
|
}
|
|
28
57
|
function parseFileOption(value, usage) {
|
|
29
58
|
if (value.startsWith("-")) throw new InvalidArgumentError(`${usage} (got flag-like value '${value}')`);
|
|
30
59
|
return value;
|
|
31
60
|
}
|
|
61
|
+
function resolveDevPort(cliPort) {
|
|
62
|
+
if (cliPort !== void 0) return cliPort;
|
|
63
|
+
return parsePort(void 0, process.env.PORT);
|
|
64
|
+
}
|
|
32
65
|
function createProgram() {
|
|
33
|
-
const program = new Command()
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
66
|
+
const program = new Command();
|
|
67
|
+
program.name("brick").description("🧱 Brick-TS CLI").version(CLI_VERSION).showHelpAfterError().addHelpText("after", `
|
|
68
|
+
Usage:
|
|
69
|
+
brick dev [entry] [--port <number>] Start development server with live reload
|
|
70
|
+
brick start [entry] [--port <number>] Start production server
|
|
71
|
+
brick build <app.ts> [--outdir <dir>] [--port <number>] [--no-aot] Compile a default-exported BrickApp into a Bun server
|
|
72
|
+
brick gen openapi <entry> [--output <file>] Generate OpenAPI 3.1 JSON specification
|
|
73
|
+
brick gen client <entry> [--output <file>] Generate a typed browser client contract
|
|
74
|
+
brick info <entry> Export architecture JSON schema
|
|
75
|
+
${PORT_RESOLUTION_HELP}`);
|
|
76
|
+
program.command("dev").description("Start development server with live reload").argument("[entry]", "Application entry file").option("--port <number>", PORT_HELP, parsePortOption).addHelpText("after", PORT_RESOLUTION_HELP).action(async (entry, opts) => {
|
|
77
|
+
const port = resolveDevPort(opts.port);
|
|
78
|
+
await startDevServer({
|
|
79
|
+
entry,
|
|
80
|
+
port
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
program.command("start").description("Start production server").argument("[entry]", "Application entry file").option("--port <number>", PORT_HELP, parsePortOption).addHelpText("after", PORT_RESOLUTION_HELP).action(async (entry, opts) => {
|
|
84
|
+
const port = resolveDevPort(opts.port);
|
|
85
|
+
await startProdServer({
|
|
37
86
|
entry,
|
|
38
87
|
port
|
|
39
88
|
});
|
|
40
89
|
});
|
|
41
|
-
program.command("build").description("
|
|
90
|
+
program.command("build").description("Compile a default-exported BrickApp into a Bun server").argument("<entry>", "Application entry file (must default-export a BrickApp)").option("--outdir <dir>", "Output directory", (v) => parseFileOption(v, "Usage: brick build <app.ts> [--outdir <directory>]")).option("--port <number>", "Default listening port", parsePortOption).option("--no-aot", "Disable ahead-of-time compilation").option("--prefix <path>", "DEPRECATED: configure prefix in brick() app instead").option("--no-request-logging", "DEPRECATED: configure requestLogging in brick() app instead").action(async (entry, opts) => {
|
|
91
|
+
if (opts.prefix !== void 0 || opts.requestLogging === false) throw new Error("Brick: configure prefix and requestLogging in your default-exported brick() app instead of build flags.");
|
|
42
92
|
const result = await buildApplication({
|
|
43
93
|
entry,
|
|
44
|
-
|
|
94
|
+
outdir: opts.outdir,
|
|
95
|
+
port: opts.port,
|
|
96
|
+
aot: opts.aot
|
|
45
97
|
});
|
|
98
|
+
for (const warning of result.ir.diagnostics) console.warn(`[Brick compiler] ${warning}`);
|
|
46
99
|
console.log(`Compiled ${result.ir.services.length} services / ${result.ir.routes.length} routes`);
|
|
47
100
|
console.log(`IR: ${result.outdir}/brick-ir.json`);
|
|
48
101
|
console.log(`Run: bun ${result.entry}`);
|
|
49
102
|
});
|
|
50
|
-
const gen = program.command("gen").description("Generate artifacts from
|
|
51
|
-
|
|
103
|
+
const gen = program.command("gen").description("Generate artifacts from a BrickApp entry").argument("[subcommand]", "Generator to run (openapi, client)").action((subcommand) => {
|
|
104
|
+
console.error(`Unknown generator: ${subcommand ?? "(none)"}. Available: openapi, client`);
|
|
105
|
+
process.exit(1);
|
|
52
106
|
});
|
|
53
|
-
gen.command("openapi").description("Generate OpenAPI 3.1 JSON specification").argument("<entry>", "Application entry file").option("--output <file>", "Write specification to file instead of stdout", (
|
|
54
|
-
const app = await
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
107
|
+
gen.command("openapi").description("Generate OpenAPI 3.1 JSON specification").argument("<entry>", "Application entry file").option("--output <file>", "Write specification to file instead of stdout", (v) => parseFileOption(v, "Usage: brick gen openapi <entry> [--output <file>]")).action(async (entry, opts) => {
|
|
108
|
+
const app = await loadBrickApp(entry);
|
|
109
|
+
const spec = generateOpenApiSpec({
|
|
110
|
+
services: [...app.definition.services],
|
|
111
|
+
prefix: app.definition.prefix
|
|
112
|
+
});
|
|
113
|
+
const jsonOutput = JSON.stringify(spec, null, 2);
|
|
114
|
+
if (opts.output) {
|
|
115
|
+
await Bun.write(opts.output, jsonOutput);
|
|
116
|
+
console.log(`✅ OpenAPI 3.1 specification written to ${opts.output}`);
|
|
117
|
+
} else console.log(jsonOutput);
|
|
64
118
|
});
|
|
65
|
-
gen.command("client").description("Generate a typed browser client contract").argument("<entry>", "Application entry file").option("--output <file>", "Write contract to file (default _brick/contract.ts)", (
|
|
66
|
-
|
|
119
|
+
gen.command("client").description("Generate a typed browser client contract").argument("<entry>", "Application entry file").option("--output <file>", "Write contract to file (default _brick/contract.ts)", (v) => parseFileOption(v, "Usage: brick gen client <entry> [--output <file>]")).action(async (entry, opts) => {
|
|
120
|
+
const output = await writeClientContract(entry, opts.output);
|
|
121
|
+
console.log(`Client contract written to ${output}`);
|
|
67
122
|
});
|
|
68
123
|
program.command("info").alias("services").description("Export architecture JSON schema").argument("<entry>", "Application entry file").action(async (entry) => {
|
|
69
|
-
const app = await
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
version: 2,
|
|
73
|
-
target: "effect-http-api",
|
|
74
|
-
services: app.services.map((service) => ({
|
|
75
|
-
name: service.name,
|
|
76
|
-
actions: Object.values(service.actions).map((action) => ({
|
|
77
|
-
name: action.endpoint.identifier,
|
|
78
|
-
method: action.method,
|
|
79
|
-
path: action.path
|
|
80
|
-
}))
|
|
81
|
-
}))
|
|
82
|
-
}, null, 2));
|
|
83
|
-
} finally {
|
|
84
|
-
await app.dispose();
|
|
85
|
-
}
|
|
124
|
+
const app = await loadBrickApp(entry);
|
|
125
|
+
const architecture = getGlobalRegistry().exportArchitecture(app.definition);
|
|
126
|
+
console.log(JSON.stringify(architecture, null, 2));
|
|
86
127
|
});
|
|
87
128
|
return program;
|
|
88
129
|
}
|
|
89
130
|
async function main() {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
131
|
+
const program = createProgram();
|
|
132
|
+
if (process.argv.slice(2).length === 0) {
|
|
133
|
+
const port = parsePort(void 0, process.env.PORT);
|
|
134
|
+
await startDevServer({ port });
|
|
135
|
+
return;
|
|
95
136
|
}
|
|
137
|
+
await program.parseAsync(process.argv);
|
|
96
138
|
}
|
|
97
|
-
|
|
139
|
+
main().catch((err) => {
|
|
140
|
+
if (err?.exitCode !== void 0) process.exit(err.exitCode);
|
|
141
|
+
if (err?.code?.startsWith?.("commander.")) process.exit(1);
|
|
142
|
+
console.error("Fatal error:", err);
|
|
143
|
+
process.exit(1);
|
|
144
|
+
});
|
|
98
145
|
//#endregion
|
|
99
146
|
export { createProgram };
|
|
100
147
|
|
package/dist/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.js","names":["cliPkg.version"],"sources":["../package.json","../src/bin.ts"],"sourcesContent":["","#!/usr/bin/env bun\nimport { Command, InvalidArgumentError } from \"commander\";\nimport { startDevServer, startProdServer } from \"./runner\";\nimport { buildApplication } from \"./build\";\nimport { writeClientContract } from \"./client-contract-file\";\nimport { parsePort } from \"./port-utils\";\nimport { loadApplication } from \"./app-entry\";\nimport cliPkg from \"../package.json\";\n\nconst PORT_HELP =\n \"Port to listen on (CLI --port overrides PORT env, default 4000)\";\nconst PORT_RESOLUTION_HELP = `\nPort Resolution (dev/start commands):\n - CLI --port flag takes precedence\n - Falls back to PORT environment variable\n - Defaults to 4000 if neither is set\n - Valid range: 1-65535\n\nExamples:\n brick dev app.ts --port 5000\n PORT=3000 brick start app.ts\n`;\nfunction parsePortOption(value: string): number {\n try {\n return parsePort(value, undefined);\n } catch (error) {\n throw new InvalidArgumentError(\n error instanceof Error ? error.message : String(error),\n );\n }\n}\nfunction parseFileOption(value: string, usage: string): string {\n if (value.startsWith(\"-\"))\n throw new InvalidArgumentError(`${usage} (got flag-like value '${value}')`);\n return value;\n}\nexport function createProgram(): Command {\n const program = new Command()\n .name(\"brick\")\n .description(\"Brick Effect application tooling\")\n .version(cliPkg.version)\n .showHelpAfterError()\n .addHelpText(\"after\", PORT_RESOLUTION_HELP);\n for (const command of [\"dev\", \"start\"] as const) {\n program\n .command(command)\n .description(\n command === \"dev\"\n ? \"Start development server with live reload\"\n : \"Start production server\",\n )\n .argument(\"<entry>\", \"Application entry file\")\n .option(\"--port <number>\", PORT_HELP, parsePortOption)\n .addHelpText(\"after\", PORT_RESOLUTION_HELP)\n .action(async (entry: string, opts: { port?: number }) => {\n const port = parsePort(\n opts.port === undefined ? undefined : String(opts.port),\n process.env.PORT,\n );\n await (command === \"dev\" ? startDevServer : startProdServer)({\n entry,\n port,\n });\n });\n }\n program\n .command(\"build\")\n .description(\"Bundle an Effect application into a Bun server\")\n .argument(\"<entry>\", \"Application entry file\")\n .option(\"--outdir <dir>\", \"Output directory\", (value) =>\n parseFileOption(\n value,\n \"Usage: brick build <app.ts> [--outdir <directory>]\",\n ),\n )\n .option(\"--port <number>\", \"Default listening port\", parsePortOption)\n .action(async (entry: string, opts: { outdir?: string; port?: number }) => {\n const result = await buildApplication({ entry, ...opts });\n console.log(\n `Compiled ${result.ir.services.length} services / ${result.ir.routes.length} routes`,\n );\n console.log(`IR: ${result.outdir}/brick-ir.json`);\n console.log(`Run: bun ${result.entry}`);\n });\n const gen = program\n .command(\"gen\")\n .description(\"Generate artifacts from an application entry\")\n .argument(\"[subcommand]\", \"Generator to run (openapi, client)\")\n .action((subcommand: string | undefined) => {\n throw new Error(\n `Unknown generator: ${subcommand ?? \"(none)\"}. Available: openapi, client`,\n );\n });\n gen\n .command(\"openapi\")\n .description(\"Generate OpenAPI 3.1 JSON specification\")\n .argument(\"<entry>\", \"Application entry file\")\n .option(\n \"--output <file>\",\n \"Write specification to file instead of stdout\",\n (value) =>\n parseFileOption(\n value,\n \"Usage: brick gen openapi <entry> [--output <file>]\",\n ),\n )\n .action(async (entry: string, opts: { output?: string }) => {\n const app = await loadApplication(entry);\n try {\n const json = JSON.stringify(app.openApi(), null, 2);\n if (opts.output) {\n await Bun.write(opts.output, json);\n console.log(`OpenAPI 3.1 specification written to ${opts.output}`);\n } else console.log(json);\n } finally {\n await app.dispose();\n }\n });\n gen\n .command(\"client\")\n .description(\"Generate a typed browser client contract\")\n .argument(\"<entry>\", \"Application entry file\")\n .option(\n \"--output <file>\",\n \"Write contract to file (default _brick/contract.ts)\",\n (value) =>\n parseFileOption(\n value,\n \"Usage: brick gen client <entry> [--output <file>]\",\n ),\n )\n .action(async (entry: string, opts: { output?: string }) => {\n console.log(\n `Client contract written to ${await writeClientContract(entry, opts.output)}`,\n );\n });\n program\n .command(\"info\")\n .alias(\"services\")\n .description(\"Export architecture JSON schema\")\n .argument(\"<entry>\", \"Application entry file\")\n .action(async (entry: string) => {\n const app = await loadApplication(entry);\n try {\n console.log(\n JSON.stringify(\n {\n version: 2,\n target: \"effect-http-api\",\n services: app.services.map((service) => ({\n name: service.name,\n actions: Object.values(service.actions).map((action) => ({\n name: action.endpoint.identifier,\n method: action.method,\n path: action.path,\n })),\n })),\n },\n null,\n 2,\n ),\n );\n } finally {\n await app.dispose();\n }\n });\n return program;\n}\nasync function main() {\n try {\n await createProgram().parseAsync(process.argv);\n } catch (error) {\n console.error(\"Fatal error:\", error);\n process.exit(1);\n }\n}\nawait main();\n"],"mappings":";;;;;;;;ACSA,MAAM,YACJ;AACF,MAAM,uBAAuB;;;;;;;;;;;AAW7B,SAAS,gBAAgB,OAAuB;CAC9C,IAAI;EACF,OAAO,UAAU,OAAO,KAAA,CAAS;CACnC,SAAS,OAAO;EACd,MAAM,IAAI,qBACR,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CACvD;CACF;AACF;AACA,SAAS,gBAAgB,OAAe,OAAuB;CAC7D,IAAI,MAAM,WAAW,GAAG,GACtB,MAAM,IAAI,qBAAqB,GAAG,MAAM,yBAAyB,MAAM,GAAG;CAC5E,OAAO;AACT;AACA,SAAgB,gBAAyB;CACvC,MAAM,UAAU,IAAI,QAAQ,CAAC,CAC1B,KAAK,OAAO,CAAC,CACb,YAAY,kCAAkC,CAAC,CAC/C,QAAQA,OAAc,CAAC,CACvB,mBAAmB,CAAC,CACpB,YAAY,SAAS,oBAAoB;CAC5C,KAAK,MAAM,WAAW,CAAC,OAAO,OAAO,GACnC,QACG,QAAQ,OAAO,CAAC,CAChB,YACC,YAAY,QACR,8CACA,yBACN,CAAC,CACA,SAAS,WAAW,wBAAwB,CAAC,CAC7C,OAAO,mBAAmB,WAAW,eAAe,CAAC,CACrD,YAAY,SAAS,oBAAoB,CAAC,CAC1C,OAAO,OAAO,OAAe,SAA4B;EACxD,MAAM,OAAO,UACX,KAAK,SAAS,KAAA,IAAY,KAAA,IAAY,OAAO,KAAK,IAAI,GACtD,QAAQ,IAAI,IACd;EACA,OAAO,YAAY,QAAQ,iBAAiB,gBAAA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CACvD,SAAS,WAAW,wBAAwB,CAAC,CAC7C,OACC,mBACA,wDACC,UACC,gBACE,OACA,mDACF,CACJ,CAAC,CACA,OAAO,OAAO,OAAe,SAA8B;EAC1D,QAAQ,IACN,8BAA8B,MAAM,oBAAoB,OAAO,KAAK,MAAM,GAC5E;CACF,CAAC;CACH,QACG,QAAQ,MAAM,CAAC,CACf,MAAM,UAAU,CAAC,CACjB,YAAY,iCAAiC,CAAC,CAC9C,SAAS,WAAW,wBAAwB,CAAC,CAC7C,OAAO,OAAO,UAAkB;EAC/B,MAAM,MAAM,MAAM,gBAAgB,KAAK;EACvC,IAAI;GACF,QAAQ,IACN,KAAK,UACH;IACE,SAAS;IACT,QAAQ;IACR,UAAU,IAAI,SAAS,KAAK,aAAa;KACvC,MAAM,QAAQ;KACd,SAAS,OAAO,OAAO,QAAQ,OAAO,CAAC,CAAC,KAAK,YAAY;MACvD,MAAM,OAAO,SAAS;MACtB,QAAQ,OAAO;MACf,MAAM,OAAO;KACf,EAAE;IACJ,EAAE;GACJ,GACA,MACA,CACF,CACF;EACF,UAAU;GACR,MAAM,IAAI,QAAQ;EACpB;CACF,CAAC;CACH,OAAO;AACT;AACA,eAAe,OAAO;CACpB,IAAI;EACF,MAAM,cAAc,CAAC,CAAC,WAAW,QAAQ,IAAI;CAC/C,SAAS,OAAO;EACd,QAAQ,MAAM,gBAAgB,KAAK;EACnC,QAAQ,KAAK,CAAC;CAChB;AACF;AACA,MAAM,KAAK"}
|
|
1
|
+
{"version":3,"file":"bin.js","names":["cliPkg.version"],"sources":["../src/client-contract-file.ts","../package.json","../src/bin.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { mkdir, readFile, rename, rm, writeFile } from \"node:fs/promises\";\nimport { dirname, join, resolve } from \"node:path\";\nimport { generateClientContract } from \"./client-contract\";\nimport { loadBrickApp } from \"./app-entry\";\n\n/** Generate a client contract without loading Vite or starting a server. */\nexport async function writeClientContract(\n entryPath: string,\n outputPath?: string,\n) {\n const entry = resolve(entryPath);\n const output = resolve(outputPath ?? \"_brick/contract.ts\");\n if (entry === output)\n throw new Error(\"Brick: contract output must not overwrite the app entry.\");\n const app = await loadBrickApp(entry);\n const source = generateClientContract(app);\n const previous = await readFile(output, \"utf8\").catch((error) => {\n if (error.code !== \"ENOENT\") throw error;\n return undefined;\n });\n if (previous !== source) {\n await mkdir(dirname(output), { recursive: true });\n const temporary = join(\n dirname(output),\n `.brick-contract-${randomUUID()}.tmp`,\n );\n try {\n await writeFile(temporary, source, { flag: \"wx\" });\n await rename(temporary, output);\n } finally {\n await rm(temporary, { force: true });\n }\n }\n return output;\n}\n","","#!/usr/bin/env bun\nimport { Command, InvalidArgumentError } from \"commander\";\nimport { startDevServer, startProdServer } from \"./runner\";\nimport { buildApplication } from \"./build\";\nimport { writeClientContract } from \"./client-contract-file\";\nimport { generateOpenApiSpec, getGlobalRegistry } from \"@brickkit/core\";\nimport { parsePort } from \"./port-utils\";\nimport { loadBrickApp } from \"./app-entry\";\nimport cliPkg from \"../package.json\";\n\n// Single source of truth for the CLI version (inlined at build time).\nconst CLI_VERSION: string = cliPkg.version;\n\nconst PORT_HELP =\n \"Port to listen on (CLI --port overrides PORT env, default 4000)\";\n\nconst PORT_RESOLUTION_HELP = `\nPort Resolution (dev/start commands):\n - CLI --port flag takes precedence\n - Falls back to PORT environment variable\n - Defaults to 4000 if neither is set\n - Valid range: 1-65535\n\nExamples:\n brick dev app.ts # port 4000 (default)\n brick dev app.ts --port 5000 # port 5000 (explicit)\n PORT=3000 brick dev app.ts # port 3000 (from env)\n brick dev app.ts --port 6000 # port 6000 (CLI overrides PORT=3000)\n`;\n\nfunction parsePortOption(value: string): number {\n try {\n return parsePort(value, undefined);\n } catch (err) {\n throw new InvalidArgumentError((err as Error).message);\n }\n}\n\nfunction parseFileOption(value: string, usage: string): string {\n if (value.startsWith(\"-\")) {\n throw new InvalidArgumentError(`${usage} (got flag-like value '${value}')`);\n }\n return value;\n}\n\nfunction resolveDevPort(cliPort: number | undefined): number {\n if (cliPort !== undefined) return cliPort;\n return parsePort(undefined, process.env.PORT);\n}\n\nexport function createProgram(): Command {\n const program = new Command();\n program\n .name(\"brick\")\n .description(\"🧱 Brick-TS CLI\")\n .version(CLI_VERSION)\n .showHelpAfterError()\n .addHelpText(\n \"after\",\n `\nUsage:\n brick dev [entry] [--port <number>] Start development server with live reload\n brick start [entry] [--port <number>] Start production server\n brick build <app.ts> [--outdir <dir>] [--port <number>] [--no-aot] Compile a default-exported BrickApp into a Bun server\n brick gen openapi <entry> [--output <file>] Generate OpenAPI 3.1 JSON specification\n brick gen client <entry> [--output <file>] Generate a typed browser client contract\n brick info <entry> Export architecture JSON schema\n${PORT_RESOLUTION_HELP}`,\n );\n\n program\n .command(\"dev\")\n .description(\"Start development server with live reload\")\n .argument(\"[entry]\", \"Application entry file\")\n .option(\"--port <number>\", PORT_HELP, parsePortOption)\n .addHelpText(\"after\", PORT_RESOLUTION_HELP)\n .action(async (entry: string | undefined, opts: { port?: number }) => {\n const port = resolveDevPort(opts.port);\n await startDevServer({ entry, port });\n });\n\n program\n .command(\"start\")\n .description(\"Start production server\")\n .argument(\"[entry]\", \"Application entry file\")\n .option(\"--port <number>\", PORT_HELP, parsePortOption)\n .addHelpText(\"after\", PORT_RESOLUTION_HELP)\n .action(async (entry: string | undefined, opts: { port?: number }) => {\n const port = resolveDevPort(opts.port);\n await startProdServer({ entry, port });\n });\n\n program\n .command(\"build\")\n .description(\"Compile a default-exported BrickApp into a Bun server\")\n .argument(\n \"<entry>\",\n \"Application entry file (must default-export a BrickApp)\",\n )\n .option(\"--outdir <dir>\", \"Output directory\", (v: string) =>\n parseFileOption(v, \"Usage: brick build <app.ts> [--outdir <directory>]\"),\n )\n .option(\"--port <number>\", \"Default listening port\", parsePortOption)\n .option(\"--no-aot\", \"Disable ahead-of-time compilation\")\n .option(\n \"--prefix <path>\",\n \"DEPRECATED: configure prefix in brick() app instead\",\n )\n .option(\n \"--no-request-logging\",\n \"DEPRECATED: configure requestLogging in brick() app instead\",\n )\n .action(\n async (\n entry: string,\n opts: {\n outdir?: string;\n port?: number;\n aot?: boolean;\n prefix?: string;\n requestLogging?: boolean;\n },\n ) => {\n if (opts.prefix !== undefined || opts.requestLogging === false) {\n throw new Error(\n \"Brick: configure prefix and requestLogging in your default-exported brick() app instead of build flags.\",\n );\n }\n const result = await buildApplication({\n entry,\n outdir: opts.outdir,\n port: opts.port,\n aot: opts.aot,\n });\n for (const warning of result.ir.diagnostics)\n console.warn(`[Brick compiler] ${warning}`);\n console.log(\n `Compiled ${result.ir.services.length} services / ${result.ir.routes.length} routes`,\n );\n console.log(`IR: ${result.outdir}/brick-ir.json`);\n console.log(`Run: bun ${result.entry}`);\n },\n );\n\n const gen = program\n .command(\"gen\")\n .description(\"Generate artifacts from a BrickApp entry\")\n .argument(\"[subcommand]\", \"Generator to run (openapi, client)\")\n .action((subcommand: string | undefined) => {\n console.error(\n `Unknown generator: ${subcommand ?? \"(none)\"}. Available: openapi, client`,\n );\n process.exit(1);\n });\n\n gen\n .command(\"openapi\")\n .description(\"Generate OpenAPI 3.1 JSON specification\")\n .argument(\"<entry>\", \"Application entry file\")\n .option(\n \"--output <file>\",\n \"Write specification to file instead of stdout\",\n (v: string) =>\n parseFileOption(\n v,\n \"Usage: brick gen openapi <entry> [--output <file>]\",\n ),\n )\n .action(async (entry: string, opts: { output?: string }) => {\n const app = await loadBrickApp(entry);\n const spec = generateOpenApiSpec({\n services: [...app.definition.services],\n prefix: app.definition.prefix,\n });\n const jsonOutput = JSON.stringify(spec, null, 2);\n if (opts.output) {\n await Bun.write(opts.output, jsonOutput);\n console.log(`✅ OpenAPI 3.1 specification written to ${opts.output}`);\n } else {\n console.log(jsonOutput);\n }\n });\n\n gen\n .command(\"client\")\n .description(\"Generate a typed browser client contract\")\n .argument(\"<entry>\", \"Application entry file\")\n .option(\n \"--output <file>\",\n \"Write contract to file (default _brick/contract.ts)\",\n (v: string) =>\n parseFileOption(v, \"Usage: brick gen client <entry> [--output <file>]\"),\n )\n .action(async (entry: string, opts: { output?: string }) => {\n const output = await writeClientContract(entry, opts.output);\n console.log(`Client contract written to ${output}`);\n });\n\n program\n .command(\"info\")\n .alias(\"services\")\n .description(\"Export architecture JSON schema\")\n .argument(\"<entry>\", \"Application entry file\")\n .action(async (entry: string) => {\n const app = await loadBrickApp(entry);\n const architecture = getGlobalRegistry().exportArchitecture(\n app.definition,\n );\n console.log(JSON.stringify(architecture, null, 2));\n });\n\n return program;\n}\n\nasync function main() {\n const program = createProgram();\n // Preserve legacy behavior: bare `brick` starts the dev server.\n const userArgs = process.argv.slice(2);\n if (userArgs.length === 0) {\n const port = parsePort(undefined, process.env.PORT);\n await startDevServer({ port });\n return;\n }\n await program.parseAsync(process.argv);\n}\n\nmain().catch((err) => {\n // Commander already prints validation errors (missing arg, unknown option,\n // invalid port) and exits. Only print Fatal error for action failures.\n if (err?.exitCode !== undefined) {\n process.exit(err.exitCode);\n }\n if (err?.code?.startsWith?.(\"commander.\")) {\n process.exit(1);\n }\n console.error(\"Fatal error:\", err);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;;AAOA,eAAsB,oBACpB,WACA,YACA;CACA,MAAM,QAAQ,QAAQ,SAAS;CAC/B,MAAM,SAAS,QAAQ,cAAc,oBAAoB;CACzD,IAAI,UAAU,QACZ,MAAM,IAAI,MAAM,0DAA0D;CAC5E,MAAM,MAAM,MAAM,aAAa,KAAK;CACpC,MAAM,SAAS,uBAAuB,GAAG;CAKzC,IAAI,MAJmB,SAAS,QAAQ,MAAM,CAAC,CAAC,OAAO,UAAU;EAC/D,IAAI,MAAM,SAAS,UAAU,MAAM;CAErC,CAAC,MACgB,QAAQ;EACvB,MAAM,MAAM,QAAQ,MAAM,GAAG,EAAE,WAAW,KAAK,CAAC;EAChD,MAAM,YAAY,KAChB,QAAQ,MAAM,GACd,mBAAmB,WAAW,EAAE,KAClC;EACA,IAAI;GACF,MAAM,UAAU,WAAW,QAAQ,EAAE,MAAM,KA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KAAK,CAAC,CAAC,OAAO,QAAQ;CAGpB,IAAI,KAAK,aAAa,KAAA,GACpB,QAAQ,KAAK,IAAI,QAAQ;CAE3B,IAAI,KAAK,MAAM,aAAa,YAAY,GACtC,QAAQ,KAAK,CAAC;CAEhB,QAAQ,MAAM,gBAAgB,GAAG;CACjC,QAAQ,KAAK,CAAC;AAChB,CAAC"}
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { i as emitCompiledApplication, n as compileBrickApplication } from "./compiler2.js";
|
|
2
|
+
import { requireBrickApp } from "@brickkit/core";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { mkdir, mkdtemp, rename, rm } from "node:fs/promises";
|
|
5
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
|
+
import { aot } from "elysia/plugin/aot/bun";
|
|
7
|
+
//#region src/app-entry.ts
|
|
8
|
+
async function loadBrickApp(entry) {
|
|
9
|
+
const module = await import(pathToFileURL(resolve(entry)).href);
|
|
10
|
+
return requireBrickApp(module.default);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/build.ts
|
|
14
|
+
/** Bundle an exported BrickApp with generated handlers and its original configuration. */
|
|
15
|
+
async function buildApplication(options) {
|
|
16
|
+
const entry = resolve(options.entry);
|
|
17
|
+
if (!await Bun.file(entry).exists()) throw new Error(`Brick compiler: entry not found: ${entry}`);
|
|
18
|
+
const outdir = resolve(options.outdir ?? "dist/brick");
|
|
19
|
+
if (entry === join(outdir, "server.js") || entry === join(outdir, "routes.js")) throw new Error("Brick compiler: output would overwrite the application entry");
|
|
20
|
+
const port = options.port ?? 4e3;
|
|
21
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Brick compiler: port must be an integer between 1 and 65535");
|
|
22
|
+
const app = await loadBrickApp(entry);
|
|
23
|
+
const services = [...app.definition.services];
|
|
24
|
+
const compilation = compileBrickApplication({
|
|
25
|
+
services,
|
|
26
|
+
prefix: app.definition.prefix
|
|
27
|
+
});
|
|
28
|
+
await mkdir(dirname(outdir), { recursive: true });
|
|
29
|
+
const staging = await mkdtemp(join(dirname(outdir), ".brick-build-"));
|
|
30
|
+
try {
|
|
31
|
+
await Bun.write(join(staging, "routes.js"), emitCompiledApplication(compilation.ir));
|
|
32
|
+
const source = `import { loadCompiledBrickApp } from "@brickkit/core";
|
|
33
|
+
import { ir, bind } from "./routes.js";
|
|
34
|
+
export const app = await loadCompiledBrickApp(() => import(${JSON.stringify(entry)}), ir, bind);
|
|
35
|
+
if (import.meta.main) {
|
|
36
|
+
const port = Number(process.env.PORT ?? ${port});
|
|
37
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Invalid PORT: must be between 1 and 65535");
|
|
38
|
+
app.listen(port);
|
|
39
|
+
console.log("Brick compiled server listening on http://localhost:" + app.server.port);
|
|
40
|
+
const stop = async () => { await app.stop(); process.exit(0); };
|
|
41
|
+
process.once("SIGINT", stop);
|
|
42
|
+
process.once("SIGTERM", stop);
|
|
43
|
+
}
|
|
44
|
+
`;
|
|
45
|
+
await Bun.write(join(staging, "entry.ts"), source);
|
|
46
|
+
const result = await Bun.build({
|
|
47
|
+
entrypoints: [join(staging, "entry.ts")],
|
|
48
|
+
target: "bun",
|
|
49
|
+
format: "esm",
|
|
50
|
+
outdir: staging,
|
|
51
|
+
naming: "server.js",
|
|
52
|
+
sourcemap: "external",
|
|
53
|
+
plugins: options.aot === false ? [] : [aot(join(staging, "entry.ts"), {
|
|
54
|
+
target: "bun",
|
|
55
|
+
strip: true
|
|
56
|
+
})]
|
|
57
|
+
});
|
|
58
|
+
if (!result.success) throw new AggregateError(result.logs, "Brick compiler: bundling failed");
|
|
59
|
+
await Bun.write(join(staging, "brick-ir.json"), JSON.stringify(compilation.ir, null, 2) + "\n");
|
|
60
|
+
await mkdir(outdir, { recursive: true });
|
|
61
|
+
for (const name of [
|
|
62
|
+
"routes.js",
|
|
63
|
+
"brick-ir.json",
|
|
64
|
+
"server.js.map",
|
|
65
|
+
"server.js"
|
|
66
|
+
]) await rename(join(staging, name), join(outdir, name));
|
|
67
|
+
return {
|
|
68
|
+
outdir,
|
|
69
|
+
entry: join(outdir, "server.js"),
|
|
70
|
+
ir: compilation.ir
|
|
71
|
+
};
|
|
72
|
+
} finally {
|
|
73
|
+
await rm(staging, {
|
|
74
|
+
recursive: true,
|
|
75
|
+
force: true
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { loadBrickApp as n, buildApplication as t };
|
|
81
|
+
|
|
82
|
+
//# sourceMappingURL=build.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.js","names":[],"sources":["../src/app-entry.ts","../src/build.ts"],"sourcesContent":["import { resolve } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport { isBrickApp, requireBrickApp, type BrickApp } from \"@brickkit/core\";\n\nexport { isBrickApp, requireBrickApp, type BrickApp };\n\nexport async function loadBrickApp(entry: string): Promise<BrickApp> {\n const module = await import(pathToFileURL(resolve(entry)).href);\n return requireBrickApp(module.default);\n}\n","import { mkdtemp, mkdir, rename, rm } from \"node:fs/promises\";\nimport { dirname, join, resolve } from \"node:path\";\nimport { compileBrickApplication, emitCompiledApplication } from \"./compiler\";\nimport { aot } from \"elysia/plugin/aot/bun\";\nimport { loadBrickApp } from \"./app-entry\";\n\nexport interface BuildApplicationOptions {\n /** A module whose default export is a BrickApp, without starting a server. */\n entry: string;\n outdir?: string;\n port?: number;\n /** Precompile Elysia's HTTP handlers at build time (default true). */\n aot?: boolean;\n}\n\n/** Bundle an exported BrickApp with generated handlers and its original configuration. */\nexport async function buildApplication(options: BuildApplicationOptions) {\n const entry = resolve(options.entry);\n if (!(await Bun.file(entry).exists()))\n throw new Error(`Brick compiler: entry not found: ${entry}`);\n const outdir = resolve(options.outdir ?? \"dist/brick\");\n if (\n entry === join(outdir, \"server.js\") ||\n entry === join(outdir, \"routes.js\")\n )\n throw new Error(\n \"Brick compiler: output would overwrite the application entry\",\n );\n const port = options.port ?? 4000;\n if (!Number.isInteger(port) || port < 1 || port > 65535)\n throw new Error(\n \"Brick compiler: port must be an integer between 1 and 65535\",\n );\n const app = await loadBrickApp(entry);\n const services = [...app.definition.services];\n const compilation = compileBrickApplication({\n services,\n prefix: app.definition.prefix,\n });\n await mkdir(dirname(outdir), { recursive: true });\n const staging = await mkdtemp(join(dirname(outdir), \".brick-build-\"));\n try {\n await Bun.write(\n join(staging, \"routes.js\"),\n emitCompiledApplication(compilation.ir),\n );\n const source = `import { loadCompiledBrickApp } from \"@brickkit/core\";\nimport { ir, bind } from \"./routes.js\";\nexport const app = await loadCompiledBrickApp(() => import(${JSON.stringify(entry)}), ir, bind);\nif (import.meta.main) {\n const port = Number(process.env.PORT ?? ${port});\n if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error(\"Invalid PORT: must be between 1 and 65535\");\n app.listen(port);\n console.log(\"Brick compiled server listening on http://localhost:\" + app.server.port);\n const stop = async () => { await app.stop(); process.exit(0); };\n process.once(\"SIGINT\", stop);\n process.once(\"SIGTERM\", stop);\n}\n`;\n await Bun.write(join(staging, \"entry.ts\"), source);\n const result = await Bun.build({\n entrypoints: [join(staging, \"entry.ts\")],\n target: \"bun\",\n format: \"esm\",\n outdir: staging,\n naming: \"server.js\",\n sourcemap: \"external\",\n plugins:\n options.aot === false\n ? []\n : [aot(join(staging, \"entry.ts\"), { target: \"bun\", strip: true })],\n });\n if (!result.success)\n throw new AggregateError(result.logs, \"Brick compiler: bundling failed\");\n await Bun.write(\n join(staging, \"brick-ir.json\"),\n JSON.stringify(compilation.ir, null, 2) + \"\\n\",\n );\n await mkdir(outdir, { recursive: true });\n for (const name of [\n \"routes.js\",\n \"brick-ir.json\",\n \"server.js.map\",\n \"server.js\",\n ])\n await rename(join(staging, name), join(outdir, name));\n return { outdir, entry: join(outdir, \"server.js\"), ir: compilation.ir };\n } finally {\n await rm(staging, { recursive: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