@20baht/cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +214 -2
- package/dist/application/create.js +72 -0
- package/dist/application/generate.js +239 -0
- package/dist/application/ports.js +1 -0
- package/dist/args.js +49 -0
- package/dist/cli.js +207 -0
- package/dist/domain/blueprint.js +6 -0
- package/dist/domain/create-plan.js +59 -0
- package/dist/domain/deploy-plan.js +82 -0
- package/dist/domain/form-dsl.js +120 -0
- package/dist/domain/generate-plan.js +60 -0
- package/dist/domain/naming.js +18 -0
- package/dist/domain/remote-plan.js +153 -0
- package/dist/domain/template-path.js +19 -0
- package/dist/infrastructure/fs-reader.js +19 -0
- package/dist/infrastructure/fs-writer.js +27 -0
- package/dist/infrastructure/shell-runner.js +32 -0
- package/dist/infrastructure/spawn-plan.js +35 -0
- package/dist/infrastructure/template-renderer.js +16 -0
- package/dist/infrastructure/template-source.js +28 -0
- package/package.json +53 -4
- package/templates/angular/README.md.hbs +46 -0
- package/templates/angular/_dependency-cruiser.mjs +51 -0
- package/templates/angular/apps/server/package.json +30 -0
- package/templates/angular/apps/server/src/app.ts +14 -0
- package/templates/angular/apps/server/src/index.ts +8 -0
- package/templates/angular/apps/server/test/app.test.ts +12 -0
- package/templates/angular/apps/server/tsconfig.json +7 -0
- package/templates/angular/apps/server/tsconfig.types.json +12 -0
- package/templates/angular/apps/shell/_editorconfig +17 -0
- package/templates/angular/apps/shell/angular.json +115 -0
- package/templates/angular/apps/shell/federation.config.mjs +44 -0
- package/templates/angular/apps/shell/package.json +43 -0
- package/templates/angular/apps/shell/public/federation.manifest.json +1 -0
- package/templates/angular/apps/shell/src/app/app.config.ts +20 -0
- package/templates/angular/apps/shell/src/app/app.css +4 -0
- package/templates/angular/apps/shell/src/app/app.html.hbs +16 -0
- package/templates/angular/apps/shell/src/app/app.routes.ts.hbs +39 -0
- package/templates/angular/apps/shell/src/app/app.ts +57 -0
- package/templates/angular/apps/shell/src/app/core/federation/load-remote.ts +31 -0
- package/templates/angular/apps/shell/src/app/core/federation/manifest.spec.ts +46 -0
- package/templates/angular/apps/shell/src/app/core/federation/manifest.ts +37 -0
- package/templates/angular/apps/shell/src/app/core/federation/remote-loader.ts +20 -0
- package/templates/angular/apps/shell/src/app/core/federation/timeout.ts +17 -0
- package/templates/angular/apps/shell/src/app/modules/home/home.component.ts.hbs +46 -0
- package/templates/angular/apps/shell/src/app/modules/unavailable/unavailable.component.ts +39 -0
- package/templates/angular/apps/shell/src/bootstrap.ts +25 -0
- package/templates/angular/apps/shell/src/environments/environment.development.ts +10 -0
- package/templates/angular/apps/shell/src/environments/environment.ts +10 -0
- package/templates/angular/apps/shell/src/index.html.hbs +12 -0
- package/templates/angular/apps/shell/src/main.ts +43 -0
- package/templates/angular/apps/shell/src/styles.css +19 -0
- package/templates/angular/apps/shell/tsconfig.app.json +10 -0
- package/templates/angular/apps/shell/tsconfig.federation.json +4 -0
- package/templates/angular/apps/shell/tsconfig.json.hbs +39 -0
- package/templates/angular/apps/shell/tsconfig.spec.json +9 -0
- package/templates/angular/bahtui.json +6 -0
- package/templates/angular/eslint.config.ts.hbs +28 -0
- package/templates/angular/knip.ts +30 -0
- package/templates/angular/package.json.hbs +52 -0
- package/templates/angular/packages/kit/package.json.hbs +27 -0
- package/templates/angular/packages/kit/src/api.ts +21 -0
- package/templates/angular/packages/kit/src/errors.ts +28 -0
- package/templates/angular/packages/kit/src/index.ts +2 -0
- package/templates/angular/packages/kit/test/errors.test.ts +28 -0
- package/templates/angular/packages/kit/tsconfig.json +9 -0
- package/templates/create/README.md.hbs +32 -0
- package/templates/create/_github/workflows/ci.yml +56 -0
- package/templates/create/_gitignore +6 -0
- package/templates/create/_husky/pre-commit +3 -0
- package/templates/create/_prettierignore +3 -0
- package/templates/create/_prettierrc.json +4 -0
- package/templates/create/apps/api/package.json.hbs +21 -0
- package/templates/create/apps/api/src/index.test.ts.hbs +10 -0
- package/templates/create/apps/api/src/index.ts.hbs +14 -0
- package/templates/create/apps/api/tsconfig.json +7 -0
- package/templates/create/apps/web/index.html.hbs +20 -0
- package/templates/create/apps/web/package.json.hbs +24 -0
- package/templates/create/apps/web/src/App.tsx.hbs +31 -0
- package/templates/create/apps/web/src/app.css +27 -0
- package/templates/create/apps/web/src/health.test.ts.hbs +28 -0
- package/templates/create/apps/web/src/health.ts.hbs +14 -0
- package/templates/create/apps/web/src/main.tsx.hbs +20 -0
- package/templates/create/apps/web/src/vite-env.d.ts.hbs +1 -0
- package/templates/create/apps/web/tsconfig.json +8 -0
- package/templates/create/apps/web/vite.config.ts.hbs +6 -0
- package/templates/create/bahtui.json.hbs +7 -0
- package/templates/create/eslint.config.js +30 -0
- package/templates/create/knip.json +9 -0
- package/templates/create/libs/shared/eden/index.ts.hbs +9 -0
- package/templates/create/libs/shared/eden/package.json.hbs +15 -0
- package/templates/create/libs/shared/eden/tsconfig.json +4 -0
- package/templates/create/package.json.hbs +47 -0
- package/templates/create/tsconfig.base.json +17 -0
- package/templates/deploy/deploy/__project__.service.hbs +23 -0
- package/templates/deploy/deploy/env.example.hbs +4 -0
- package/templates/deploy/deploy/install.sh.hbs +58 -0
- package/templates/deploy/deploy/nginx.conf.hbs +67 -0
- package/templates/deploy/scripts/deploy.ts.hbs +33 -0
- package/templates/deploy/scripts/preview.ts.hbs +57 -0
- package/templates/deploy/scripts/release.ts.hbs +48 -0
- package/templates/new/CLAUDE.md +3 -0
- package/templates/new/README.md.hbs +38 -0
- package/templates/new/_dependency-cruiser.mjs +9 -0
- package/templates/new/_github/actions/setup/action.yml +15 -0
- package/templates/new/_github/workflows/ci.yml +122 -0
- package/templates/new/_gitignore +12 -0
- package/templates/new/_gitleaks.toml +6 -0
- package/templates/new/_husky/commit-msg +1 -0
- package/templates/new/_husky/pre-commit +8 -0
- package/templates/new/_husky/pre-push +1 -0
- package/templates/new/_jscpd.json +20 -0
- package/templates/new/_mcp.json +9 -0
- package/templates/new/_prettierignore +5 -0
- package/templates/new/apps/_gitkeep +0 -0
- package/templates/new/commitlint.config.js +1 -0
- package/templates/new/eslint.config.ts.hbs +7 -0
- package/templates/new/knip.ts +7 -0
- package/templates/new/lint-staged.config.js +1 -0
- package/templates/new/package.json.hbs +51 -0
- package/templates/new/packages/_gitkeep +0 -0
- package/templates/new/renovate.json +23 -0
- package/templates/new/scripts/dev.ts +54 -0
- package/templates/new/scripts/each.ts +29 -0
- package/templates/new/scripts/workspaces.ts +24 -0
- package/templates/new/tsconfig.base.json +3 -0
- package/templates/new/tsconfig.json +7 -0
- package/templates/remote/_editorconfig +17 -0
- package/templates/remote/angular.json.hbs +95 -0
- package/templates/remote/federation.config.mjs.hbs +48 -0
- package/templates/remote/package.json.hbs +38 -0
- package/templates/remote/src/app/__name__.routes.ts.hbs +12 -0
- package/templates/remote/src/app/modules/__name__/__name__.component.ts.hbs +37 -0
- package/templates/remote/src/app/services/__name__.service.ts.hbs +14 -0
- package/templates/remote/src/index.html.hbs +11 -0
- package/templates/remote/src/main.ts.hbs +3 -0
- package/templates/remote/tsconfig.app.json +10 -0
- package/templates/remote/tsconfig.federation.json +4 -0
- package/templates/remote/tsconfig.json.hbs +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nattapong Promthong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,215 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @20baht/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Scaffolding CLI for bun workspaces and [bahtui](https://github.com/bank20baht/20baht) form
|
|
4
|
+
projects. Ships the `bahtui` binary.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
bun add -g @20baht/cli # or: npm i -g @20baht/cli
|
|
8
|
+
bahtui --help
|
|
9
|
+
|
|
10
|
+
# one-off, no install
|
|
11
|
+
bunx @20baht/cli new my-workspace
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires Node ≥ 20.12 (or bun). `bun` and `git` must be on `PATH` for the post-scaffold
|
|
15
|
+
steps.
|
|
16
|
+
|
|
17
|
+
## Commands
|
|
18
|
+
|
|
19
|
+
| Command | What it does |
|
|
20
|
+
| ---------------------------------------- | ----------------------------------------------------------------- |
|
|
21
|
+
| `bahtui new <name>` | Bare bun workspace in `./<name>` — toolchain only, no apps |
|
|
22
|
+
| `bahtui create <name>` | Full DDD monorepo in `./<name>` — React + Elysia + Eden |
|
|
23
|
+
| `bahtui create <name> --angular` | Angular Native Federation host + one remote + Elysia + shared kit |
|
|
24
|
+
| `bahtui init` | Write `bahtui.json` into an existing repo (enables the generators) |
|
|
25
|
+
| `bahtui g model <domain>` | `libs/domain/<domain>/model` package |
|
|
26
|
+
| `bahtui g form <domain>/<name> --fields` | Engine-validated `FormDefinition` JSON inside that model package |
|
|
27
|
+
| `bahtui g remote <name>` | Angular workspace: another remote, wired into the host and checks |
|
|
28
|
+
| `bahtui g deploy [nginx]` | Angular workspace: nginx + systemd deploy, release/deploy/preview |
|
|
29
|
+
|
|
30
|
+
### `bahtui new <name>`
|
|
31
|
+
|
|
32
|
+
Angular-style project bootstrap. Generates a bun workspace with the shared toolchain and
|
|
33
|
+
nothing else — you add `apps/*` and `packages/*` yourself.
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
bahtui new acme
|
|
37
|
+
cd acme
|
|
38
|
+
bun run check # lint + format:check + knip + typecheck — green on the empty repo
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
What you get:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
package.json workspaces apps/* + packages/*, check script, prettier config ref
|
|
45
|
+
tsconfig.base.json extends @20baht/strict/tsconfig/base.json (strict + noUncheckedIndexedAccess + exactOptionalPropertyTypes ...)
|
|
46
|
+
tsconfig.json typechecks scripts/ (extend the base from each workspace)
|
|
47
|
+
eslint.config.ts one line: @20baht/strict/eslint `base` (ESLint 10, typescript-eslint strict, sonarjs, prettier)
|
|
48
|
+
lint-staged.config.js · commitlint.config.js re-exports from @20baht/strict
|
|
49
|
+
knip.ts dead-code / unused-dependency checks (@20baht/strict/knip)
|
|
50
|
+
commitlint.config.js Conventional Commits
|
|
51
|
+
.husky/ pre-commit → gitleaks + lint-staged · commit-msg → commitlint · pre-push → bun run check:fast
|
|
52
|
+
.gitleaks.toml · .jscpd.json · .dependency-cruiser.mjs · renovate.json · .github/ (CI: lint, typecheck, knip/madge/deps, dup, test, build, gitleaks, audit, commitlint)
|
|
53
|
+
scripts/dev.ts runs every apps/*/ "dev" script in parallel with prefixed output
|
|
54
|
+
scripts/each.ts `bun run scripts/each.ts <script>` in every workspace that defines it
|
|
55
|
+
.mcp.json, CLAUDE.md zvec_grep code-search MCP wiring for Claude Code
|
|
56
|
+
apps/, packages/ empty (.gitkeep)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
By default the CLI runs `git init`, `bun install`, then makes the first commit — so the
|
|
60
|
+
husky hooks prove themselves before you touch anything. Flags:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
--skip-install (alias --no-install) skip bun install and the initial commit
|
|
64
|
+
--skip-git (alias --no-git) skip git init and the initial commit
|
|
65
|
+
--dry-run print the file list, write nothing
|
|
66
|
+
--force write into a non-empty directory
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Add a workspace by creating `apps/<name>/package.json` with `dev` / `build` / `typecheck`
|
|
70
|
+
scripts; `bun run dev`, `bun run build` and `bun run typecheck` pick it up automatically.
|
|
71
|
+
Add a matching entry to `knip.ts` so dead-code checks cover it.
|
|
72
|
+
|
|
73
|
+
### `bahtui create <name>`
|
|
74
|
+
|
|
75
|
+
Same flags as `new`, but scaffolds a complete stack:
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
apps/web React 19 + Vite on @20baht/ui (tokens, <baht-*> elements, dark toggle)
|
|
79
|
+
apps/api Elysia — exports `type App`, the Eden type anchor
|
|
80
|
+
libs/shared/eden treaty<App> — the one typed HTTP client
|
|
81
|
+
bahtui.json generator config (see below)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
bahtui create shop
|
|
86
|
+
cd shop
|
|
87
|
+
bun run dev # web on :5173, api on :3001
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `bahtui create <name> --angular`
|
|
91
|
+
|
|
92
|
+
Same flags. The `new` toolchain, plus microfrontends on
|
|
93
|
+
[Native Federation](https://www.npmjs.com/package/@angular-architects/native-federation):
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
apps/shell host (:4200): @20baht/angular app shell, navigator built from the
|
|
97
|
+
federation manifest, a fallback page for each remote that cannot load
|
|
98
|
+
apps/hello-web remote (:4201): one page calling GET /api/hello, exposed as ./Routes
|
|
99
|
+
apps/server Elysia under /api; exports `type App`, emitted to dist/types for the web apps
|
|
100
|
+
packages/kit typed ngx-eden client + error text, compiled into each app as @<name>/kit
|
|
101
|
+
bahtui.json { "frontend": "angular", "apps": { "web": "apps/shell", ... } }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`eslint.config.ts` adds the `angular()` and `bahtui()` presets for the web apps, `knip.ts` a
|
|
105
|
+
workspace per app, `.dependency-cruiser.mjs` "apps never import each other", "core/, services/,
|
|
106
|
+
shared/ know no page" and "packages know no app". The generated repo passes its own
|
|
107
|
+
`bun run check` (`bun run --filter @20baht/cli accept:angular` proves it: scaffold, install,
|
|
108
|
+
`g remote`, check). What it gets right on purpose:
|
|
109
|
+
|
|
110
|
+
- `features.ignoreUnusedDeps: false` in every `federation.config.mjs` (its scan refuses the kit,
|
|
111
|
+
which lives outside the app folder), so every dependency is shared and each app lists
|
|
112
|
+
@20baht/ui's optional peers `marked`, `dompurify`, `flatpickr`; `/^marked\/bin/` is skipped.
|
|
113
|
+
- `fileReplacements` on `esbuild:development`, not `build:development`.
|
|
114
|
+
- Server types reach the web apps as built declarations, and the server's imports are
|
|
115
|
+
extensionless (Angular's compiler rejects `./x.ts` specifiers).
|
|
116
|
+
- A remote that is down at boot gets a fallback page under its URL prefix.
|
|
117
|
+
|
|
118
|
+
### `bahtui g remote <name>`
|
|
119
|
+
|
|
120
|
+
In a `create --angular` workspace: `apps/<name>-web` on the next free port, mounted at
|
|
121
|
+
`/<name>`, plus a line in `eslint.config.ts`, `knip.ts`, `.dependency-cruiser.mjs`, the dev
|
|
122
|
+
manifest (`apps/shell/src/environments/environment.development.ts`) and the deployed one
|
|
123
|
+
(`apps/shell/public/federation.manifest.json`). The TS/JS files carry a `// bahtui:remotes` line
|
|
124
|
+
the entry goes above; if one is gone, nothing is written and the error names the file. Run
|
|
125
|
+
`bun install` afterwards.
|
|
126
|
+
|
|
127
|
+
### `bahtui g deploy [nginx]`
|
|
128
|
+
|
|
129
|
+
In a `create --angular` workspace, one web server for everything:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
deploy/nginx.conf host at /, remotes at /remotes/<app>/, /federation.manifest.json from
|
|
133
|
+
/etc/<name>/ (edit it there to add or repoint a remote), /api/ proxied
|
|
134
|
+
deploy/<name>.service systemd unit running the bundled server as the <name> user
|
|
135
|
+
deploy/env.example /etc/<name>/env, written once
|
|
136
|
+
deploy/install.sh runs on the target as root: nginx, bun and the user on the first run,
|
|
137
|
+
then swaps the release in and restarts (safe to rerun)
|
|
138
|
+
scripts/release.ts bun run release: build, lay the result out in .release/
|
|
139
|
+
scripts/deploy.ts bun run deploy root@host: release, tar over ssh, install.sh
|
|
140
|
+
scripts/preview.ts bun run preview: serve .release/ like nginx does, on :8080
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
It adds the three scripts to `package.json` and `.release` to `.gitignore`, `.prettierignore` and
|
|
144
|
+
the eslint ignores. It refuses an existing `deploy/` (or existing script names) without `--force`.
|
|
145
|
+
|
|
146
|
+
### `bahtui init`
|
|
147
|
+
|
|
148
|
+
Writes `bahtui.json` into the current directory so `bahtui g …` knows where things live.
|
|
149
|
+
|
|
150
|
+
```sh
|
|
151
|
+
bahtui init # frontend react, backend elysia
|
|
152
|
+
bahtui init --frontend none # no React feature slices
|
|
153
|
+
bahtui init --backend none # no Elysia plugins
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
```json
|
|
157
|
+
{
|
|
158
|
+
"apps": { "web": "apps/web", "api": "apps/api" },
|
|
159
|
+
"libsDir": "libs",
|
|
160
|
+
"frontend": "react",
|
|
161
|
+
"backend": "elysia",
|
|
162
|
+
"edenClient": "libs/shared/eden"
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### `bahtui g model <domain>`
|
|
167
|
+
|
|
168
|
+
Creates `<libsDir>/domain/<domain>/model` — a framework-free package for entities and form
|
|
169
|
+
JSON. `<domain>` is kebab-case.
|
|
170
|
+
|
|
171
|
+
### `bahtui g form <domain>/<name> --fields "<dsl>"`
|
|
172
|
+
|
|
173
|
+
Builds a `FormDefinition`, validates it with
|
|
174
|
+
[`@20baht/validator`](https://www.npmjs.com/package/@20baht/validator) (the same
|
|
175
|
+
engine the browser components run), and writes
|
|
176
|
+
`<libsDir>/domain/<domain>/model/src/<name>.form.json`.
|
|
177
|
+
|
|
178
|
+
```sh
|
|
179
|
+
bahtui g form ticket/create \
|
|
180
|
+
--fields "title:text:required,priority:dropdown:options=low|medium|high,qty:number:min=1"
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Field DSL — comma-separated `key:type[:modifier…]`:
|
|
184
|
+
|
|
185
|
+
| Part | Values |
|
|
186
|
+
| ---------- | -------------------------------------------------------------------------------------------------------------- |
|
|
187
|
+
| `key` | `[a-z][a-z0-9_]*`; the label is derived (`due_date` → "Due Date") |
|
|
188
|
+
| `type` | `text` `textarea` `markdown` `number` `currency` `email` `password` `dropdown` `multiselect` `checkbox` `radio` `toggle` `date` `file` `rating` `range` `reference` `tags` |
|
|
189
|
+
| modifiers | `required` · `options=a\|b\|c` · `min=N` · `max=N` · `step=N` · `ref=LOOKUP` · `flag=NAME` |
|
|
190
|
+
|
|
191
|
+
Invalid DSL (unknown type, empty options, non-numeric `min`) fails before anything is
|
|
192
|
+
written.
|
|
193
|
+
|
|
194
|
+
## Global flags
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
--dry-run print what would be generated, write nothing
|
|
198
|
+
--force overwrite existing files / write into non-empty dirs
|
|
199
|
+
-h, --help
|
|
200
|
+
-v, --version
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## How it works
|
|
204
|
+
|
|
205
|
+
Every command produces a **blueprint** — plain data listing files and post-commands — then
|
|
206
|
+
adapters write it to disk and run the commands. `--dry-run` prints the blueprint. Template
|
|
207
|
+
files ending in `.hbs` get `{{name}}` substitution; everything else is copied verbatim, and a
|
|
208
|
+
leading `_` in a template filename becomes `.` (so `.gitignore` survives `npm pack`).
|
|
209
|
+
|
|
210
|
+
Generated file paths are confined to the target directory; a hostile `bahtui.json` cannot
|
|
211
|
+
write outside the project root.
|
|
212
|
+
|
|
213
|
+
## License
|
|
214
|
+
|
|
215
|
+
MIT
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ANGULAR_HOST, overlayTemplates, planCreate, PRESET_TEMPLATE_SETS, } from '../domain/create-plan.js';
|
|
2
|
+
import { withFirstRemote } from '../domain/remote-plan.js';
|
|
3
|
+
import { validateProjectName } from '../domain/naming.js';
|
|
4
|
+
function logDryRun(blueprint, deps) {
|
|
5
|
+
deps.log.info(`dry-run — would create in ${blueprint.root}:`);
|
|
6
|
+
for (const f of blueprint.files)
|
|
7
|
+
deps.log.info(` ${f.path}`);
|
|
8
|
+
for (const c of blueprint.postCommands)
|
|
9
|
+
deps.log.info(` $ ${c.cmd} ${c.args.join(' ')}`);
|
|
10
|
+
}
|
|
11
|
+
function runPostCommands(blueprint, input, deps) {
|
|
12
|
+
for (const c of blueprint.postCommands) {
|
|
13
|
+
deps.log.step(c.label);
|
|
14
|
+
try {
|
|
15
|
+
deps.runner.run(c.cmd, c.args, blueprint.root);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
const retry = `${c.cmd} ${c.args.join(' ')}`;
|
|
19
|
+
throw new CreateError(`${err instanceof Error ? err.message : String(err)}\nproject files are written — cd ${input.name} and rerun: ${retry}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function logNextSteps(input, deps) {
|
|
24
|
+
deps.log.info('');
|
|
25
|
+
deps.log.info(`Done. Next:`);
|
|
26
|
+
deps.log.info(` cd ${input.name}`);
|
|
27
|
+
if (!input.install)
|
|
28
|
+
deps.log.info(' bun install');
|
|
29
|
+
for (const line of NEXT_STEPS[input.preset])
|
|
30
|
+
deps.log.info(` ${line}`);
|
|
31
|
+
}
|
|
32
|
+
export function runCreate(input, deps) {
|
|
33
|
+
const errors = validateProjectName(input.name);
|
|
34
|
+
if (errors.length > 0) {
|
|
35
|
+
for (const e of errors)
|
|
36
|
+
deps.log.error(e);
|
|
37
|
+
throw new CreateError('Invalid project name.');
|
|
38
|
+
}
|
|
39
|
+
const render = (raw, data) => deps.renderer.render(raw, data);
|
|
40
|
+
const sets = PRESET_TEMPLATE_SETS[input.preset].map((set) => deps.templates.load(set));
|
|
41
|
+
const planned = planCreate(input, overlayTemplates(sets), render);
|
|
42
|
+
const blueprint = input.preset === 'angular'
|
|
43
|
+
? withFirstRemote(planned, input.name, ANGULAR_HOST, deps.templates.load('remote'), render)
|
|
44
|
+
: planned;
|
|
45
|
+
if (input.dryRun) {
|
|
46
|
+
logDryRun(blueprint, deps);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
deps.writer.ensureUsableDir(blueprint.root, input.force);
|
|
50
|
+
deps.writer.writeFiles(blueprint);
|
|
51
|
+
deps.log.step(`${blueprint.files.length} files written to ${blueprint.root}`);
|
|
52
|
+
runPostCommands(blueprint, input, deps);
|
|
53
|
+
logNextSteps(input, deps);
|
|
54
|
+
}
|
|
55
|
+
const NEXT_STEPS = {
|
|
56
|
+
full: [
|
|
57
|
+
'bun run dev # web :5173 + api :3001',
|
|
58
|
+
'bun run lint && bun run test && bun run build',
|
|
59
|
+
],
|
|
60
|
+
angular: [
|
|
61
|
+
'bun run dev # shell :4200, hello-web :4201, server :3001',
|
|
62
|
+
'bahtui g remote <name> # add another remote',
|
|
63
|
+
'bun run check # lint + format:check + typecheck + madge + deps + knip + dup + build + test',
|
|
64
|
+
],
|
|
65
|
+
workspace: [
|
|
66
|
+
'mkdir -p apps/<name> && add a package.json with dev/build/typecheck scripts',
|
|
67
|
+
'bun run dev # runs every apps/*/ dev script in parallel',
|
|
68
|
+
'bun run check # lint + format:check + typecheck + madge + deps + knip + dup + build + test',
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
export class CreateError extends Error {
|
|
72
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { buildFormDefinition, FormDslError } from '../domain/form-dsl.js';
|
|
2
|
+
import { DEFAULT_CONFIG, modelDir, planForm, planInit, planModel, validateDomainName, } from '../domain/generate-plan.js';
|
|
3
|
+
import { DEFAULT_API_PORT, DeployPlanError, planDeploy } from '../domain/deploy-plan.js';
|
|
4
|
+
import { nextRemotePort, planRemoteFiles, remoteFor, RemoteWiringError, validateRemoteName, wireRemote, wiringPaths, } from '../domain/remote-plan.js';
|
|
5
|
+
export class GenerateError extends Error {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* bahtui.json path-ish fields feed directly into fs paths (modelDir(), eden
|
|
9
|
+
* client wiring). A cloned repo's bahtui.json is untrusted input — reject
|
|
10
|
+
* anything that could escape the project root before it ever reaches a path
|
|
11
|
+
* join (defense in depth alongside DiskFileWriter's own root confinement).
|
|
12
|
+
*/
|
|
13
|
+
const SAFE_RELATIVE_PATH = /^[A-Za-z0-9._/-]+$/;
|
|
14
|
+
function validateConfigPath(field, value) {
|
|
15
|
+
if (!SAFE_RELATIVE_PATH.test(value)) {
|
|
16
|
+
throw new GenerateError(`bahtui.json "${field}" contains invalid characters: ${JSON.stringify(value)}.`);
|
|
17
|
+
}
|
|
18
|
+
if (value.startsWith('/')) {
|
|
19
|
+
throw new GenerateError(`bahtui.json "${field}" must be a relative path: ${JSON.stringify(value)}.`);
|
|
20
|
+
}
|
|
21
|
+
if (value.split('/').includes('..')) {
|
|
22
|
+
throw new GenerateError(`bahtui.json "${field}" must not contain ".." segments: ${JSON.stringify(value)}.`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function isPlainObject(value) {
|
|
26
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
function loadConfig(root, reader) {
|
|
29
|
+
const raw = reader.read(`${root}/bahtui.json`);
|
|
30
|
+
if (raw === null) {
|
|
31
|
+
throw new GenerateError('No bahtui.json here — run "bahtui init" (or create) first.');
|
|
32
|
+
}
|
|
33
|
+
let parsed;
|
|
34
|
+
try {
|
|
35
|
+
parsed = JSON.parse(raw);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
throw new GenerateError('bahtui.json is not valid JSON.');
|
|
39
|
+
}
|
|
40
|
+
if (!isPlainObject(parsed)) {
|
|
41
|
+
throw new GenerateError('bahtui.json must contain a JSON object.');
|
|
42
|
+
}
|
|
43
|
+
// Hand-edited: check the shape of every field read later, so a wrong one is named here.
|
|
44
|
+
const text = (field, value) => {
|
|
45
|
+
if (typeof value !== 'string') {
|
|
46
|
+
throw new GenerateError(`bahtui.json "${field}" must be a string.`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
if (parsed.libsDir !== undefined)
|
|
50
|
+
text('libsDir', parsed.libsDir);
|
|
51
|
+
if (parsed.edenClient !== undefined)
|
|
52
|
+
text('edenClient', parsed.edenClient);
|
|
53
|
+
if (parsed.apps !== undefined) {
|
|
54
|
+
if (!isPlainObject(parsed.apps))
|
|
55
|
+
throw new GenerateError('bahtui.json "apps" must be an object.');
|
|
56
|
+
for (const [app, path] of Object.entries(parsed.apps))
|
|
57
|
+
text(`apps.${app}`, path);
|
|
58
|
+
}
|
|
59
|
+
const config = { ...DEFAULT_CONFIG, ...parsed };
|
|
60
|
+
validateConfigPath('libsDir', config.libsDir);
|
|
61
|
+
if (config.edenClient !== undefined)
|
|
62
|
+
validateConfigPath('edenClient', config.edenClient);
|
|
63
|
+
return config;
|
|
64
|
+
}
|
|
65
|
+
function emit(blueprint, dryRun, deps) {
|
|
66
|
+
if (dryRun) {
|
|
67
|
+
deps.log.info(`dry-run — would write in ${blueprint.root}:`);
|
|
68
|
+
for (const f of blueprint.files)
|
|
69
|
+
deps.log.info(` ${f.path}`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
deps.writer.writeFiles(blueprint);
|
|
73
|
+
for (const f of blueprint.files)
|
|
74
|
+
deps.log.step(f.path);
|
|
75
|
+
}
|
|
76
|
+
/** `bahtui init` (PRD P2): bahtui.json for an existing repo, paths detected. */
|
|
77
|
+
export function runInit(input, deps) {
|
|
78
|
+
if (deps.reader.exists(`${input.root}/bahtui.json`) && !input.force && !input.dryRun) {
|
|
79
|
+
throw new GenerateError('bahtui.json already exists — use --force to overwrite.');
|
|
80
|
+
}
|
|
81
|
+
const config = {
|
|
82
|
+
...DEFAULT_CONFIG,
|
|
83
|
+
frontend: input.frontend,
|
|
84
|
+
backend: input.backend,
|
|
85
|
+
apps: {
|
|
86
|
+
...(deps.reader.exists(`${input.root}/apps/web`) ? { web: 'apps/web' } : {}),
|
|
87
|
+
...(deps.reader.exists(`${input.root}/apps/api`) ? { api: 'apps/api' } : {}),
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
if (input.backend === null)
|
|
91
|
+
delete config.edenClient;
|
|
92
|
+
emit(planInit(input.root, config), input.dryRun, deps);
|
|
93
|
+
if (!input.dryRun)
|
|
94
|
+
deps.log.info('bahtui.json written — `bahtui g model <domain>` is ready.');
|
|
95
|
+
}
|
|
96
|
+
/** `bahtui g model|form` (PRD P3). Engine-validates before any write. */
|
|
97
|
+
export function runGenerate(input, deps) {
|
|
98
|
+
const config = loadConfig(input.root, deps.reader);
|
|
99
|
+
if (input.kind === 'model') {
|
|
100
|
+
const domain = input.target;
|
|
101
|
+
const errors = validateDomainName(domain);
|
|
102
|
+
if (errors.length > 0)
|
|
103
|
+
throw new GenerateError(errors.join(' '));
|
|
104
|
+
const dir = `${input.root}/${modelDir(config, domain)}`;
|
|
105
|
+
if (deps.reader.exists(dir) && !input.force) {
|
|
106
|
+
throw new GenerateError(`${modelDir(config, domain)} already exists — use --force.`);
|
|
107
|
+
}
|
|
108
|
+
emit(planModel(input.root, config, domain), input.dryRun, deps);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
// form: <domain>/<name>
|
|
112
|
+
const [domain, name, ...rest] = input.target.split('/');
|
|
113
|
+
if (!domain || !name || rest.length > 0) {
|
|
114
|
+
throw new GenerateError('g form needs <domain>/<name>, e.g. "ticket/create-ticket".');
|
|
115
|
+
}
|
|
116
|
+
const nameErrors = [...validateDomainName(domain), ...validateDomainName(name)];
|
|
117
|
+
if (nameErrors.length > 0)
|
|
118
|
+
throw new GenerateError(nameErrors.join(' '));
|
|
119
|
+
if (!input.fields) {
|
|
120
|
+
throw new GenerateError('g form needs --fields, e.g. --fields "title:text:required,qty:number:min=1".');
|
|
121
|
+
}
|
|
122
|
+
if (!deps.reader.exists(`${input.root}/${modelDir(config, domain)}`)) {
|
|
123
|
+
throw new GenerateError(`${modelDir(config, domain)} does not exist — run "bahtui g model ${domain}" first.`);
|
|
124
|
+
}
|
|
125
|
+
let form;
|
|
126
|
+
try {
|
|
127
|
+
form = buildFormDefinition(domain, name, input.fields); // invalid spec = zero files
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
if (err instanceof FormDslError)
|
|
131
|
+
throw new GenerateError(err.message);
|
|
132
|
+
throw err;
|
|
133
|
+
}
|
|
134
|
+
const filePath = `${input.root}/${modelDir(config, domain)}/src/${name}.form.json`;
|
|
135
|
+
if (deps.reader.exists(filePath) && !input.force) {
|
|
136
|
+
throw new GenerateError(`${name}.form.json already exists — use --force.`);
|
|
137
|
+
}
|
|
138
|
+
emit(planForm(input.root, config, domain, name, form), input.dryRun, deps);
|
|
139
|
+
}
|
|
140
|
+
function readWiring(root, host, reader) {
|
|
141
|
+
const paths = wiringPaths(host);
|
|
142
|
+
const read = (path) => {
|
|
143
|
+
const text = reader.read(`${root}/${path}`);
|
|
144
|
+
if (text === null)
|
|
145
|
+
throw new GenerateError(`${path} is missing — g remote edits it.`);
|
|
146
|
+
return text;
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
eslint: read(paths.eslint),
|
|
150
|
+
knip: read(paths.knip),
|
|
151
|
+
depcruise: read(paths.depcruise),
|
|
152
|
+
devManifest: read(paths.devManifest),
|
|
153
|
+
manifest: read(paths.manifest),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function projectName(root, reader) {
|
|
157
|
+
const raw = reader.read(`${root}/package.json`);
|
|
158
|
+
let parsed;
|
|
159
|
+
try {
|
|
160
|
+
parsed = raw === null ? undefined : JSON.parse(raw);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
throw new GenerateError('package.json is not valid JSON.');
|
|
164
|
+
}
|
|
165
|
+
const name = isPlainObject(parsed) ? parsed.name : undefined;
|
|
166
|
+
if (typeof name !== 'string' || !/^[a-z][a-z0-9-]*$/.test(name)) {
|
|
167
|
+
throw new GenerateError('package.json needs the project name (it scopes the @<name>/kit path).');
|
|
168
|
+
}
|
|
169
|
+
return name;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* `bahtui g remote <name>` in an Angular workspace (`bahtui create --angular`): a new Native
|
|
173
|
+
* Federation remote under `apps/<name>-web`, added to the host's manifests, eslint, knip and
|
|
174
|
+
* dependency-cruiser. Nothing is written when any of those files cannot take it.
|
|
175
|
+
*/
|
|
176
|
+
export function runGenerateRemote(input, deps) {
|
|
177
|
+
const config = loadConfig(input.root, deps.reader);
|
|
178
|
+
if (config.frontend !== 'angular') {
|
|
179
|
+
throw new GenerateError('g remote needs an Angular workspace (bahtui.json "frontend": "angular", from bahtui create --angular).');
|
|
180
|
+
}
|
|
181
|
+
const host = config.apps.web;
|
|
182
|
+
if (host === undefined)
|
|
183
|
+
throw new GenerateError('bahtui.json needs "apps.web", the host app.');
|
|
184
|
+
validateConfigPath('apps.web', host);
|
|
185
|
+
const errors = validateRemoteName(input.name);
|
|
186
|
+
if (errors.length > 0)
|
|
187
|
+
throw new GenerateError(errors.join(' '));
|
|
188
|
+
const current = readWiring(input.root, host, deps.reader);
|
|
189
|
+
const remote = remoteFor(input.name, nextRemotePort(current.devManifest));
|
|
190
|
+
if (deps.reader.exists(`${input.root}/apps/${remote.app}`) && !input.force) {
|
|
191
|
+
throw new GenerateError(`apps/${remote.app} already exists — use --force.`);
|
|
192
|
+
}
|
|
193
|
+
const files = planned(RemoteWiringError, () => [
|
|
194
|
+
...planRemoteFiles(remote, projectName(input.root, deps.reader), deps.templates.load('remote'), (raw, data) => deps.renderer.render(raw, data)),
|
|
195
|
+
...wireRemote(current, host, remote),
|
|
196
|
+
]);
|
|
197
|
+
emitThen(input, files, deps, `apps/${remote.app} is on :${remote.port} and mounted at /${remote.name}. Run bun install, then bun run dev.`);
|
|
198
|
+
}
|
|
199
|
+
/** Runs a domain plan, reporting its own error class as a GenerateError. */
|
|
200
|
+
function planned(domainError, plan) {
|
|
201
|
+
try {
|
|
202
|
+
return plan();
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
if (err instanceof domainError)
|
|
206
|
+
throw new GenerateError(err.message);
|
|
207
|
+
throw err;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** Writes (or, on --dry-run, lists) the files, then says what comes next. */
|
|
211
|
+
function emitThen(input, files, deps, next) {
|
|
212
|
+
emit({ root: input.root, files, postCommands: [] }, input.dryRun, deps);
|
|
213
|
+
if (!input.dryRun)
|
|
214
|
+
deps.log.info(next);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* `bahtui g deploy [nginx]` in an Angular workspace: `deploy/` (nginx.conf, a systemd unit,
|
|
218
|
+
* env.example, install.sh) and `scripts/{release,deploy,preview}.ts`, wired into package.json,
|
|
219
|
+
* `.gitignore`, `.prettierignore` and eslint. Nothing is written when an edit cannot apply.
|
|
220
|
+
*/
|
|
221
|
+
export function runGenerateDeploy(input, deps) {
|
|
222
|
+
const config = loadConfig(input.root, deps.reader);
|
|
223
|
+
if (config.frontend !== 'angular' || config.apps.web === undefined) {
|
|
224
|
+
throw new GenerateError('g deploy needs an Angular workspace (bahtui.json "frontend": "angular", from bahtui create --angular).');
|
|
225
|
+
}
|
|
226
|
+
validateConfigPath('apps.web', config.apps.web);
|
|
227
|
+
const host = config.apps.web.split('/').pop() ?? config.apps.web;
|
|
228
|
+
if (deps.reader.exists(`${input.root}/deploy`) && !input.force) {
|
|
229
|
+
throw new GenerateError('deploy/ already exists — use --force.');
|
|
230
|
+
}
|
|
231
|
+
const read = (path) => deps.reader.read(`${input.root}/${path}`) ?? '';
|
|
232
|
+
const files = planned(DeployPlanError, () => planDeploy({ project: projectName(input.root, deps.reader), host, apiPort: DEFAULT_API_PORT }, {
|
|
233
|
+
packageJson: read('package.json'),
|
|
234
|
+
gitignore: read('.gitignore'),
|
|
235
|
+
prettierignore: read('.prettierignore'),
|
|
236
|
+
eslint: read('eslint.config.ts'),
|
|
237
|
+
}, deps.templates.load('deploy'), (raw, data) => deps.renderer.render(raw, data), input.force));
|
|
238
|
+
emitThen(input, files, deps, 'Next: bun run release (builds .release/), bun run preview (serves it on :8080), bun run deploy root@<host>.');
|
|
239
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/args.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Boolean flags recognized anywhere on the command line (no value). */
|
|
2
|
+
export const KNOWN_FLAGS = new Set([
|
|
3
|
+
'-v',
|
|
4
|
+
'--version',
|
|
5
|
+
'-h',
|
|
6
|
+
'--help',
|
|
7
|
+
'--dry-run',
|
|
8
|
+
'--force',
|
|
9
|
+
'--no-install',
|
|
10
|
+
'--no-git',
|
|
11
|
+
'--skip-install',
|
|
12
|
+
'--skip-git',
|
|
13
|
+
'--angular',
|
|
14
|
+
]);
|
|
15
|
+
/**
|
|
16
|
+
* Value-carrying flags pulled out before positional parsing. Supports both
|
|
17
|
+
* `--flag value` and `--flag=value`. If the token after `--flag value` looks
|
|
18
|
+
* like another flag (starts with `-`), the option is treated as missing
|
|
19
|
+
* rather than swallowing that flag as the value.
|
|
20
|
+
*/
|
|
21
|
+
export function takeOption(argv, flag) {
|
|
22
|
+
const eqPrefix = `${flag}=`;
|
|
23
|
+
const eqIndex = argv.findIndex((a) => a.startsWith(eqPrefix));
|
|
24
|
+
if (eqIndex !== -1) {
|
|
25
|
+
const value = argv[eqIndex].slice(eqPrefix.length);
|
|
26
|
+
argv.splice(eqIndex, 1);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
const i = argv.indexOf(flag);
|
|
30
|
+
if (i === -1)
|
|
31
|
+
return undefined;
|
|
32
|
+
const value = argv[i + 1];
|
|
33
|
+
if (value === undefined || value.startsWith('-')) {
|
|
34
|
+
argv.splice(i, 1);
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
argv.splice(i, 2);
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
export function parseArgs(rawArgv) {
|
|
41
|
+
const argv = [...rawArgv];
|
|
42
|
+
const fields = takeOption(argv, '--fields');
|
|
43
|
+
const frontend = takeOption(argv, '--frontend');
|
|
44
|
+
const backend = takeOption(argv, '--backend');
|
|
45
|
+
const args = argv.filter((a) => !a.startsWith('-'));
|
|
46
|
+
const flags = new Set(argv.filter((a) => a.startsWith('-')));
|
|
47
|
+
const [command, name, sub] = args;
|
|
48
|
+
return { fields, frontend, backend, flags, command, name, sub };
|
|
49
|
+
}
|