@brickkit/cli 0.4.1 → 0.5.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 +42 -138
- package/dist/bin.d.ts.map +1 -1
- package/dist/bin.js +52 -99
- package/dist/bin.js.map +1 -1
- package/dist/client-contract-file.js +135 -0
- package/dist/client-contract-file.js.map +1 -0
- package/dist/dev-worker.js +2 -4
- package/dist/dev-worker.js.map +1 -1
- package/dist/index.d.ts +43 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -6
- package/dist/native-client-contract.js +46 -0
- package/dist/native-client-contract.js.map +1 -0
- package/dist/runner.js +64 -78
- package/dist/runner.js.map +1 -1
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +48 -12
- package/dist/vite.js.map +1 -1
- package/package.json +3 -9
- package/dist/build.js +0 -82
- package/dist/build.js.map +0 -1
- package/dist/client-contract.js +0 -268
- package/dist/client-contract.js.map +0 -1
- package/dist/compiler.d.ts +0 -3
- package/dist/compiler.js +0 -1
- package/dist/compiler2.js +0 -10
- package/dist/compiler2.js.map +0 -1
- package/dist/emit.d.ts +0 -7
- package/dist/emit.d.ts.map +0 -1
- package/dist/port-utils.js +0 -13
- package/dist/port-utils.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,111 +1,47 @@
|
|
|
1
1
|
# @brickkit/cli
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
The `brick` command for Effect-native Brick applications: dev server,
|
|
4
|
+
production start, bundling, OpenAPI and client-contract generation, and a Vite
|
|
5
|
+
plugin that keeps the browser contract in sync with the backend.
|
|
6
6
|
|
|
7
7
|
You need Bun 1.2 or later.
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
bun add @brickkit/cli
|
|
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);
|
|
10
|
+
bun add -d @brickkit/cli
|
|
18
11
|
```
|
|
19
12
|
|
|
20
13
|
## Commands
|
|
21
14
|
|
|
22
15
|
Every command except `dev` and `start` takes a required `<entry>`: a module
|
|
23
|
-
whose default export is
|
|
16
|
+
whose default export is the value returned by `Brick.app(...)`. The entry must
|
|
17
|
+
not start a listener itself.
|
|
24
18
|
|
|
25
19
|
```sh
|
|
26
20
|
brick dev [entry] [--port 4000]
|
|
27
21
|
brick start [entry] [--port 4000]
|
|
28
|
-
brick build <entry> [--outdir dist/brick] [--port <number>]
|
|
22
|
+
brick build <entry> [--outdir dist/brick] [--port <number>]
|
|
29
23
|
brick gen openapi <entry> [--output openapi.json]
|
|
30
24
|
brick gen client <entry> [--output _brick/contract.ts]
|
|
31
25
|
brick info <entry>
|
|
32
26
|
```
|
|
33
27
|
|
|
34
|
-
|
|
28
|
+
`dev` and `start` resolve the port from `--port`, then `PORT`, then 4000.
|
|
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.
|
|
35
32
|
|
|
36
33
|
```ts
|
|
37
|
-
|
|
38
|
-
import {
|
|
34
|
+
// src/app.ts
|
|
35
|
+
import { Brick } from "@brickkit/core";
|
|
36
|
+
import { Layer } from "effect";
|
|
37
|
+
import { usersService } from "./services/users";
|
|
39
38
|
|
|
40
|
-
|
|
39
|
+
export default Brick.app({ services: [usersService], layer: Layer.empty });
|
|
41
40
|
```
|
|
42
41
|
|
|
43
|
-
|
|
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:
|
|
42
|
+
## Typed browser client with Vite
|
|
49
43
|
|
|
50
|
-
|
|
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
|
-
```
|
|
44
|
+
Add `brickClient` before your framework plugins:
|
|
109
45
|
|
|
110
46
|
```ts
|
|
111
47
|
// vite.config.ts
|
|
@@ -119,77 +55,45 @@ export default defineConfig({
|
|
|
119
55
|
});
|
|
120
56
|
```
|
|
121
57
|
|
|
122
|
-
Vite
|
|
123
|
-
production builds
|
|
124
|
-
|
|
125
|
-
|
|
58
|
+
Vite writes `_brick/contract.ts` before the frontend loads and before
|
|
59
|
+
production builds, and regenerates it when backend files change. Unchanged
|
|
60
|
+
output is not rewritten. Add `_brick/` to `.gitignore`; change its location
|
|
61
|
+
with the `output` option.
|
|
62
|
+
|
|
63
|
+
The generated module exports the API definition and the client factories:
|
|
126
64
|
|
|
127
65
|
```ts
|
|
128
|
-
|
|
129
|
-
import { createBrickClient } from "@brickkit/core/client";
|
|
130
|
-
import { contract } from "../_brick/contract";
|
|
66
|
+
import { api, createClient } from "../_brick/contract";
|
|
131
67
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
contract,
|
|
135
|
-
});
|
|
68
|
+
const { client } = createClient(api, { baseUrl: "http://localhost:4000" });
|
|
69
|
+
await client.users.getUser({ payload: { id: "usr_42" } });
|
|
136
70
|
```
|
|
137
71
|
|
|
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
|
-
|
|
144
72
|
For TanStack Start or another embedded server, import the app from
|
|
145
73
|
`virtual:brick-app` in server code and forward requests to `app.fetch(request)`.
|
|
146
|
-
|
|
147
|
-
|
|
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.
|
|
74
|
+
Add `/// <reference types="@brickkit/cli/vite/client" />` to your Vite
|
|
75
|
+
environment declarations. See `examples/pages` for a complete integration.
|
|
160
76
|
|
|
161
|
-
|
|
77
|
+
Generation evaluates the app entry, so anything it needs must be available at
|
|
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.
|
|
162
81
|
|
|
163
|
-
|
|
82
|
+
## Without Vite
|
|
164
83
|
|
|
165
84
|
```sh
|
|
166
|
-
brick gen client src/app.ts
|
|
167
85
|
brick gen client src/app.ts --output frontend/src/contract.ts
|
|
168
86
|
```
|
|
169
87
|
|
|
170
|
-
The
|
|
171
|
-
|
|
172
|
-
|
|
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:
|
|
88
|
+
The command evaluates the entry, creates parent directories, and leaves an
|
|
89
|
+
unchanged file untouched. Run it before your frontend's dev server, build, and
|
|
90
|
+
type check; a package script keeps this simple:
|
|
184
91
|
|
|
185
92
|
```json
|
|
186
|
-
{
|
|
187
|
-
"scripts": {
|
|
188
|
-
"gen:client": "brick gen client src/app.ts"
|
|
189
|
-
}
|
|
190
|
-
}
|
|
93
|
+
{ "scripts": { "gen:client": "brick gen client src/app.ts" } }
|
|
191
94
|
```
|
|
192
95
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
96
|
+
`generateNativeClientContract(app, options)` exposes the generator for other
|
|
97
|
+
tooling.
|
|
98
|
+
|
|
99
|
+
Source: <https://github.com/Chandraprakash-Darji/brick>.
|
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":";;wBAoCgB,iBAAiB"}
|
package/dist/bin.js
CHANGED
|
@@ -1,38 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
import { n as startDevServer, r as startProdServer } from "./runner.js";
|
|
3
|
-
import { n as
|
|
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";
|
|
2
|
+
import { a as loadApplication, i as parsePort, n as startDevServer, r as startProdServer } from "./runner.js";
|
|
3
|
+
import { n as buildApplication, t as writeClientContract } from "./client-contract-file.js";
|
|
10
4
|
import { Command, InvalidArgumentError } from "commander";
|
|
11
|
-
//#region
|
|
12
|
-
|
|
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
|
-
}
|
|
5
|
+
//#region package.json
|
|
6
|
+
var version = "0.5.0";
|
|
33
7
|
//#endregion
|
|
34
8
|
//#region src/bin.ts
|
|
35
|
-
const CLI_VERSION = "0.4.1";
|
|
36
9
|
const PORT_HELP = "Port to listen on (CLI --port overrides PORT env, default 4000)";
|
|
37
10
|
const PORT_RESOLUTION_HELP = `
|
|
38
11
|
Port Resolution (dev/start commands):
|
|
@@ -42,106 +15,86 @@ Port Resolution (dev/start commands):
|
|
|
42
15
|
- Valid range: 1-65535
|
|
43
16
|
|
|
44
17
|
Examples:
|
|
45
|
-
brick dev app.ts
|
|
46
|
-
brick
|
|
47
|
-
PORT=3000 brick dev app.ts # port 3000 (from env)
|
|
48
|
-
brick dev app.ts --port 6000 # port 6000 (CLI overrides PORT=3000)
|
|
18
|
+
brick dev app.ts --port 5000
|
|
19
|
+
PORT=3000 brick start app.ts
|
|
49
20
|
`;
|
|
50
21
|
function parsePortOption(value) {
|
|
51
22
|
try {
|
|
52
23
|
return parsePort(value, void 0);
|
|
53
|
-
} catch (
|
|
54
|
-
throw new InvalidArgumentError(
|
|
24
|
+
} catch (error) {
|
|
25
|
+
throw new InvalidArgumentError(error instanceof Error ? error.message : String(error));
|
|
55
26
|
}
|
|
56
27
|
}
|
|
57
28
|
function parseFileOption(value, usage) {
|
|
58
29
|
if (value.startsWith("-")) throw new InvalidArgumentError(`${usage} (got flag-like value '${value}')`);
|
|
59
30
|
return value;
|
|
60
31
|
}
|
|
61
|
-
function resolveDevPort(cliPort) {
|
|
62
|
-
if (cliPort !== void 0) return cliPort;
|
|
63
|
-
return parsePort(void 0, process.env.PORT);
|
|
64
|
-
}
|
|
65
32
|
function createProgram() {
|
|
66
|
-
const program = new Command();
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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({
|
|
33
|
+
const program = new Command().name("brick").description("Brick Effect application tooling").version(version).showHelpAfterError().addHelpText("after", PORT_RESOLUTION_HELP);
|
|
34
|
+
for (const command of ["dev", "start"]) program.command(command).description(command === "dev" ? "Start development server with live reload" : "Start production server").argument("<entry>", "Application entry file").option("--port <number>", PORT_HELP, parsePortOption).addHelpText("after", PORT_RESOLUTION_HELP).action(async (entry, opts) => {
|
|
35
|
+
const port = parsePort(opts.port === void 0 ? void 0 : String(opts.port), process.env.PORT);
|
|
36
|
+
await (command === "dev" ? startDevServer : startProdServer)({
|
|
86
37
|
entry,
|
|
87
38
|
port
|
|
88
39
|
});
|
|
89
40
|
});
|
|
90
|
-
program.command("build").description("
|
|
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.");
|
|
41
|
+
program.command("build").description("Bundle an Effect application into a Bun server").argument("<entry>", "Application entry file").option("--outdir <dir>", "Output directory", (value) => parseFileOption(value, "Usage: brick build <app.ts> [--outdir <directory>]")).option("--port <number>", "Default listening port", parsePortOption).action(async (entry, opts) => {
|
|
92
42
|
const result = await buildApplication({
|
|
93
43
|
entry,
|
|
94
|
-
|
|
95
|
-
port: opts.port,
|
|
96
|
-
aot: opts.aot
|
|
44
|
+
...opts
|
|
97
45
|
});
|
|
98
|
-
for (const warning of result.ir.diagnostics) console.warn(`[Brick compiler] ${warning}`);
|
|
99
46
|
console.log(`Compiled ${result.ir.services.length} services / ${result.ir.routes.length} routes`);
|
|
100
47
|
console.log(`IR: ${result.outdir}/brick-ir.json`);
|
|
101
48
|
console.log(`Run: bun ${result.entry}`);
|
|
102
49
|
});
|
|
103
|
-
const gen = program.command("gen").description("Generate artifacts from
|
|
104
|
-
|
|
105
|
-
process.exit(1);
|
|
50
|
+
const gen = program.command("gen").description("Generate artifacts from an application entry").argument("[subcommand]", "Generator to run (openapi, client)").action((subcommand) => {
|
|
51
|
+
throw new Error(`Unknown generator: ${subcommand ?? "(none)"}. Available: openapi, client`);
|
|
106
52
|
});
|
|
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", (
|
|
108
|
-
const app = await
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
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", (value) => parseFileOption(value, "Usage: brick gen openapi <entry> [--output <file>]")).action(async (entry, opts) => {
|
|
54
|
+
const app = await loadApplication(entry);
|
|
55
|
+
try {
|
|
56
|
+
const json = JSON.stringify(app.openApi(), null, 2);
|
|
57
|
+
if (opts.output) {
|
|
58
|
+
await Bun.write(opts.output, json);
|
|
59
|
+
console.log(`OpenAPI 3.1 specification written to ${opts.output}`);
|
|
60
|
+
} else console.log(json);
|
|
61
|
+
} finally {
|
|
62
|
+
await app.dispose();
|
|
63
|
+
}
|
|
118
64
|
});
|
|
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)", (
|
|
120
|
-
|
|
121
|
-
console.log(`Client contract written to ${output}`);
|
|
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)", (value) => parseFileOption(value, "Usage: brick gen client <entry> [--output <file>]")).action(async (entry, opts) => {
|
|
66
|
+
console.log(`Client contract written to ${await writeClientContract(entry, opts.output)}`);
|
|
122
67
|
});
|
|
123
68
|
program.command("info").alias("services").description("Export architecture JSON schema").argument("<entry>", "Application entry file").action(async (entry) => {
|
|
124
|
-
const app = await
|
|
125
|
-
|
|
126
|
-
|
|
69
|
+
const app = await loadApplication(entry);
|
|
70
|
+
try {
|
|
71
|
+
console.log(JSON.stringify({
|
|
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
|
+
}
|
|
127
86
|
});
|
|
128
87
|
return program;
|
|
129
88
|
}
|
|
130
89
|
async function main() {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
90
|
+
try {
|
|
91
|
+
await createProgram().parseAsync(process.argv);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("Fatal error:", error);
|
|
94
|
+
process.exit(1);
|
|
136
95
|
}
|
|
137
|
-
await program.parseAsync(process.argv);
|
|
138
96
|
}
|
|
139
|
-
main()
|
|
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
|
-
});
|
|
97
|
+
await main();
|
|
145
98
|
//#endregion
|
|
146
99
|
export { createProgram };
|
|
147
100
|
|
package/dist/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,MA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;CAEH,OAAO;AACT;AAEA,eAAe,OAAO;CACpB,MAAM,UAAU,cAAc;CAG9B,IADiB,QAAQ,KAAK,MAAM,CACzB,CAAC,CAAC,WAAW,GAAG;EACzB,MAAM,OAAO,UAAU,KAAA,GAAW,QAAQ,IAAI,IAAI;EAClD,MAAM,eAAe,EAAE,KAAK,CAAC;EAC7B;CACF;CACA,MAAM,QAAQ,WAAW,QAAQ,IAAI;AACvC;AAEA,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"}
|
|
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,MAA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AQ;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"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { a as loadApplication } from "./runner.js";
|
|
2
|
+
import { t as generateNativeClientContract } from "./native-client-contract.js";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
//#region src/native-build.ts
|
|
7
|
+
async function buildNativeApplication(options, app) {
|
|
8
|
+
const entry = resolve(options.entry);
|
|
9
|
+
const outdir = resolve(options.outdir ?? "dist/brick");
|
|
10
|
+
await mkdir(dirname(outdir), { recursive: true });
|
|
11
|
+
const staging = await mkdtemp(join(dirname(outdir), ".brick-native-build-"));
|
|
12
|
+
try {
|
|
13
|
+
await Bun.write(join(staging, "entry.ts"), `import app from ${JSON.stringify(entry)};
|
|
14
|
+
export { app };
|
|
15
|
+
if (import.meta.main) {
|
|
16
|
+
const port = Number(process.env.PORT ?? ${options.port});
|
|
17
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Invalid PORT: must be between 1 and 65535");
|
|
18
|
+
let server;
|
|
19
|
+
try {
|
|
20
|
+
await app.ready();
|
|
21
|
+
server = Bun.serve({ port, fetch: request => app.fetch(request) });
|
|
22
|
+
} catch (error) {
|
|
23
|
+
await app.dispose();
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
let closing;
|
|
27
|
+
const stop = () => closing ??= (async () => {
|
|
28
|
+
try { await server.stop(true); } finally { await app.dispose(); }
|
|
29
|
+
})();
|
|
30
|
+
const onSignal = () => void stop().then(() => process.exit(0), error => {
|
|
31
|
+
console.error(error);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
process.once("SIGINT", onSignal);
|
|
35
|
+
process.once("SIGTERM", onSignal);
|
|
36
|
+
console.log("Brick server listening on http://localhost:" + server.port);
|
|
37
|
+
}
|
|
38
|
+
`);
|
|
39
|
+
const result = await Bun.build({
|
|
40
|
+
entrypoints: [join(staging, "entry.ts")],
|
|
41
|
+
target: "bun",
|
|
42
|
+
format: "esm",
|
|
43
|
+
outdir: staging,
|
|
44
|
+
naming: "server.js",
|
|
45
|
+
sourcemap: "external"
|
|
46
|
+
});
|
|
47
|
+
if (!result.success) throw new AggregateError(result.logs, "Brick: native application bundling failed");
|
|
48
|
+
const services = app.services.map((service) => ({
|
|
49
|
+
name: service.name,
|
|
50
|
+
actions: Object.values(service.actions).map((action) => ({
|
|
51
|
+
name: action.endpoint.identifier,
|
|
52
|
+
method: action.method,
|
|
53
|
+
path: action.path
|
|
54
|
+
}))
|
|
55
|
+
}));
|
|
56
|
+
const ir = {
|
|
57
|
+
version: 2,
|
|
58
|
+
target: "effect-http-api",
|
|
59
|
+
services,
|
|
60
|
+
routes: services.flatMap((service) => service.actions.map((action) => ({
|
|
61
|
+
service: service.name,
|
|
62
|
+
...action
|
|
63
|
+
}))),
|
|
64
|
+
diagnostics: []
|
|
65
|
+
};
|
|
66
|
+
await Bun.write(join(staging, "brick-ir.json"), JSON.stringify(ir, null, 2) + "\n");
|
|
67
|
+
await mkdir(outdir, { recursive: true });
|
|
68
|
+
for (const name of [
|
|
69
|
+
"brick-ir.json",
|
|
70
|
+
"server.js.map",
|
|
71
|
+
"server.js"
|
|
72
|
+
]) await rename(join(staging, name), join(outdir, name));
|
|
73
|
+
return {
|
|
74
|
+
outdir,
|
|
75
|
+
entry: join(outdir, "server.js"),
|
|
76
|
+
ir
|
|
77
|
+
};
|
|
78
|
+
} finally {
|
|
79
|
+
await rm(staging, {
|
|
80
|
+
recursive: true,
|
|
81
|
+
force: true
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/build.ts
|
|
87
|
+
/** Bundle the application and its scoped Effect dependencies into a Bun server. */
|
|
88
|
+
async function buildApplication(options) {
|
|
89
|
+
const entry = resolve(options.entry);
|
|
90
|
+
if (!await Bun.file(entry).exists()) throw new Error(`Brick: entry not found: ${entry}`);
|
|
91
|
+
const outdir = resolve(options.outdir ?? "dist/brick");
|
|
92
|
+
if (entry === join(outdir, "server.js")) throw new Error("Brick: output would overwrite the application entry");
|
|
93
|
+
const port = options.port ?? 4e3;
|
|
94
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Brick: port must be an integer between 1 and 65535");
|
|
95
|
+
const app = await loadApplication(entry);
|
|
96
|
+
try {
|
|
97
|
+
return await buildNativeApplication({
|
|
98
|
+
entry,
|
|
99
|
+
outdir,
|
|
100
|
+
port
|
|
101
|
+
}, app);
|
|
102
|
+
} finally {
|
|
103
|
+
await app.dispose();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/client-contract-file.ts
|
|
108
|
+
/** Generate a client contract without loading Vite or starting a server. */
|
|
109
|
+
async function writeClientContract(entryPath, outputPath) {
|
|
110
|
+
const entry = resolve(entryPath);
|
|
111
|
+
const output = resolve(outputPath ?? "_brick/contract.ts");
|
|
112
|
+
if (entry === output) throw new Error("Brick: contract output must not overwrite the app entry.");
|
|
113
|
+
const app = await loadApplication(entry);
|
|
114
|
+
const source = await generateNativeClientContract(app, {
|
|
115
|
+
entry,
|
|
116
|
+
output
|
|
117
|
+
}).finally(() => app.dispose());
|
|
118
|
+
if (await readFile(output, "utf8").catch((error) => {
|
|
119
|
+
if (error.code !== "ENOENT") throw error;
|
|
120
|
+
}) !== source) {
|
|
121
|
+
await mkdir(dirname(output), { recursive: true });
|
|
122
|
+
const temporary = join(dirname(output), `.brick-contract-${randomUUID()}.tmp`);
|
|
123
|
+
try {
|
|
124
|
+
await writeFile(temporary, source, { flag: "wx" });
|
|
125
|
+
await rename(temporary, output);
|
|
126
|
+
} finally {
|
|
127
|
+
await rm(temporary, { force: true });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return output;
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { buildApplication as n, writeClientContract as t };
|
|
134
|
+
|
|
135
|
+
//# sourceMappingURL=client-contract-file.js.map
|