@hile/cli 2.1.1 → 3.0.1

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.
Files changed (3) hide show
  1. package/AI.md +334 -0
  2. package/README.md +39 -134
  3. package/package.json +7 -6
package/AI.md ADDED
@@ -0,0 +1,334 @@
1
+ # AI Guide For @hile/cli
2
+
3
+
4
+
5
+ <!-- Generated by scripts/build-ai-context.mjs from docs/ai. Do not edit by hand. -->
6
+
7
+
8
+
9
+ Purpose: Run hile start, scan boot files, load auto-loaded services, and start registries from the command line.
10
+
11
+
12
+
13
+ Use this file when an AI agent installs the npm package and needs package-local examples, package selection rules, boundaries, and verification steps.
14
+
15
+
16
+
17
+ ## Package Selection
18
+
19
+
20
+
21
+ | User asks for | Use | Also read |
22
+ |---|---|---|
23
+ | Start an app, manage lifecycle, graceful shutdown | `@hile/core`, `@hile/cli`, `@hile/bootstrap` | `packages/core-lifecycle.md` |
24
+
25
+
26
+
27
+ # Core Lifecycle, Bootstrap, CLI, Logger, Redis, TypeORM
28
+
29
+ Packages: `@hile/core`, `@hile/bootstrap`, `@hile/cli`, `@hile/logger`, `@hile/ioredis`, `@hile/typeorm`.
30
+
31
+ ## Use When
32
+
33
+ Use these packages when an app needs managed startup, singleton resources, dependency ordering, environment-file loading, graceful shutdown, logs, Redis, or SQL database connections.
34
+
35
+ ## Do Not Use When
36
+
37
+ - Do not use `loadService()` at module top level.
38
+ - Do not use `auto_load_packages` for file paths. It accepts package names only.
39
+ - Do not use the default `@hile/ioredis` or `@hile/typeorm` services when one app needs multiple Redis or database connections; define separate services with separate keys.
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ pnpm add @hile/core @hile/logger @hile/ioredis @hile/typeorm
45
+ pnpm add -D @hile/cli
46
+ ```
47
+
48
+ ## Imports
49
+
50
+ ```ts
51
+ import { defineService, loadService, container } from '@hile/core'
52
+ import { createLogger } from '@hile/logger'
53
+ import redisService, { createRedis } from '@hile/ioredis'
54
+ import typeormService, { createDataSource, transaction } from '@hile/typeorm'
55
+ ```
56
+
57
+ ## Copy-Paste Example
58
+
59
+ ```ts
60
+ // src/services/http.boot.ts
61
+ import { defineService } from '@hile/core'
62
+ import { Http } from '@hile/http'
63
+
64
+ export default defineService('http', async (shutdown) => {
65
+ const http = new Http({ port: Number(process.env.HTTP_PORT ?? 3000) })
66
+ await http.load(new URL('../controllers', import.meta.url).pathname)
67
+ const close = await http.listen()
68
+ shutdown(close)
69
+ return http
70
+ })
71
+ ```
72
+
73
+ Run:
74
+
75
+ ```bash
76
+ hile start --dev --env-file .env
77
+ ```
78
+
79
+ ## More Examples
80
+
81
+ Use `package.json` auto-load for integration services that default-export a Hile service:
82
+
83
+ ```json
84
+ {
85
+ "type": "module",
86
+ "scripts": {
87
+ "dev": "hile start --dev --env-file .env",
88
+ "start": "hile start --env-file .env.prod"
89
+ },
90
+ "hile": {
91
+ "auto_load_packages": ["@hile/logger", "@hile/ioredis", "@hile/typeorm"]
92
+ }
93
+ }
94
+ ```
95
+
96
+ Inside another service, load dependencies:
97
+
98
+ ```ts
99
+ import { defineService, loadService } from '@hile/core'
100
+ import redisService from '@hile/ioredis'
101
+ import typeormService from '@hile/typeorm'
102
+
103
+ export default defineService('user.service', async () => {
104
+ const [redis, ds] = await Promise.all([
105
+ loadService(redisService),
106
+ loadService(typeormService),
107
+ ])
108
+ return { redis, ds }
109
+ })
110
+ ```
111
+
112
+ ## Compose With
113
+
114
+ - Use `@hile/http` boot services to start APIs.
115
+ - Use `@hile/model` services arrays to load TypeORM or Redis for business logic.
116
+ - Use `@hile/cache`, `@hile/redis-lock`, queues, rate limits, and idempotency with a Redis client from `@hile/ioredis`.
117
+
118
+ ## Runtime And Lifecycle Notes
119
+
120
+ - `defineService(key, fn)` registers a service factory and does not execute it.
121
+ - `loadService(service)` executes the factory on first call and returns the cached value afterwards.
122
+ - The `shutdown` callback registers teardown functions; they run in LIFO order.
123
+ - The container tracks dependencies when service factories call `loadService()`.
124
+ - `container.shutdown()` triggers registered teardowns.
125
+ - `@hile/bootstrap` loads env files first, sets `NODE_ENV`, imports auto-load packages, scans boot files, and installs exit hooks.
126
+ - `@hile/logger.createLogger()` returns `{ logger, teardown }`.
127
+ - `@hile/ioredis.createRedis()` waits for `connect`.
128
+ - `@hile/typeorm.createDataSource()` initializes a TypeORM `DataSource`.
129
+
130
+ ## Anti-Patterns
131
+
132
+ - Reusing one service key for unrelated factories.
133
+ - Missing `shutdown()` callbacks for network connections or servers.
134
+ - Assuming TypeORM env config fills entities automatically when `TYPEORM_ENTITIES` is not set.
135
+ - Calling `transaction()` and manually committing; the helper commits or rolls back internally.
136
+
137
+ ## Verification Checklist
138
+
139
+ - Boot files default-export `defineService(...)`.
140
+ - Long-lived resources register teardown callbacks.
141
+ - `auto_load_packages` contains package names, not local files.
142
+ - `hile start --dev` can find `src/**/*.boot.ts`.
143
+ - Production build emits `dist/**/*.boot.js`.
144
+
145
+ # create-hile
146
+
147
+ Package: `create-hile`.
148
+
149
+ ## Copy-Paste Example
150
+
151
+ Create a project:
152
+
153
+ ```bash
154
+ npx create-hile create my-app
155
+ cd my-app
156
+ pnpm install
157
+ pnpm run dev
158
+ ```
159
+
160
+ Skip dependency install:
161
+
162
+ ```bash
163
+ npx create-hile create my-app --skip-install
164
+ ```
165
+
166
+ ## More Examples
167
+
168
+ Template choices:
169
+
170
+ ```text
171
+ default HTTP API with @hile/http
172
+ next Next.js + Hile controllers on one port
173
+ micro-http Microservice plus HTTP endpoint
174
+ micro Pure microservice
175
+ micro-http-next Next.js + microservice + HTTP
176
+ monorepo Lerna + pnpm workspace
177
+ ```
178
+
179
+ Generated default HTTP shape:
180
+
181
+ ```text
182
+ src/
183
+ controllers/
184
+ index.controller.ts
185
+ services/
186
+ index.boot.ts
187
+ ```
188
+
189
+ ## Use When
190
+
191
+ Use `create-hile` when starting a new Hile application or workspace.
192
+
193
+ ## Do Not Use When
194
+
195
+ - Do not use generated templates as the final architecture for complex apps without introducing models, services, and context boundaries.
196
+ - Do not trust stale template README files if they mention unrelated application domains; regenerate or rewrite them from current template files.
197
+
198
+ ## Install
199
+
200
+ Usually no install is needed:
201
+
202
+ ```bash
203
+ npx create-hile create my-app
204
+ ```
205
+
206
+ ## Imports
207
+
208
+ The CLI entrypoint exports a `create` command. Application code does not import `create-hile`.
209
+
210
+ ## Compose With
211
+
212
+ - Generated apps use `@hile/cli` for `hile start`.
213
+ - HTTP templates use `@hile/core` and `@hile/http`.
214
+ - Next templates use `@hile/http-next` and `@hile/model`.
215
+ - Micro templates use `@hile/micro` and `@hile/message-loader`.
216
+
217
+ ## Runtime And Lifecycle Notes
218
+
219
+ - Templates use `_env`, `_env.prod`, and `_gitignore`; creation renames them to dotfiles.
220
+ - The CLI prompts for a template and optional dependency installation.
221
+ - The package manager preference is `pnpm`, then `yarn`, then `npm`.
222
+
223
+ ## Anti-Patterns
224
+
225
+ - Leaving template package versions stale after publishing new Hile packages.
226
+ - Shipping template READMEs copied from unrelated projects.
227
+ - Starting production without running the build command required by the selected template.
228
+
229
+ ## Verification Checklist
230
+
231
+ - New project has `"type": "module"`.
232
+ - Dev script runs `hile start --dev`.
233
+ - Production script runs `hile start` after build.
234
+ - Boot files default-export `defineService(...)`.
235
+
236
+
237
+
238
+ # Related Recipes
239
+
240
+
241
+
242
+ # New Project Scaffold
243
+
244
+ ## Complete Example
245
+
246
+ ```bash
247
+ npx create-hile create orders-api
248
+ cd orders-api
249
+ pnpm install
250
+ pnpm run dev
251
+ ```
252
+
253
+ Default HTTP controller:
254
+
255
+ ```ts
256
+ // src/controllers/index.controller.ts
257
+ import { defineController } from '@hile/http'
258
+
259
+ export default defineController('GET', async () => {
260
+ return { ok: true }
261
+ })
262
+ ```
263
+
264
+ Default HTTP boot:
265
+
266
+ ```ts
267
+ // src/services/index.boot.ts
268
+ import { defineService } from '@hile/core'
269
+ import { Http } from '@hile/http'
270
+
271
+ export default defineService('http', async (shutdown) => {
272
+ const http = new Http({ port: Number(process.env.HTTP_PORT ?? 3000) })
273
+ await http.load(new URL('../controllers', import.meta.url).pathname)
274
+ const close = await http.listen()
275
+ shutdown(close)
276
+ return http
277
+ })
278
+ ```
279
+
280
+ ## File Layout
281
+
282
+ ```text
283
+ orders-api/
284
+ src/controllers/index.controller.ts
285
+ src/services/index.boot.ts
286
+ package.json
287
+ .env
288
+ ```
289
+
290
+ ## User Intent
291
+
292
+ Use this recipe when the user wants a new Hile app or template guidance.
293
+
294
+ ## Packages To Use
295
+
296
+ - `create-hile`
297
+ - Template-selected Hile packages
298
+
299
+ ## Implementation Steps
300
+
301
+ 1. Choose a template by app shape.
302
+ 2. Make sure scripts use `hile start --dev` for development.
303
+ 3. Keep boot files under `src/**`.
304
+ 4. Add models when domain logic grows beyond a sample controller.
305
+
306
+ ## Failure And Cleanup Behavior
307
+
308
+ - Generated apps rely on `@hile/cli` to find boot files and call container shutdown.
309
+ - Production needs build output under `dist`.
310
+
311
+ ## Verification Checklist
312
+
313
+ - `package.json` has `"type": "module"`.
314
+ - `pnpm run dev` starts `hile start --dev`.
315
+ - Boot file registers server close function.
316
+ - Template README matches the selected template, not an unrelated app.
317
+
318
+
319
+
320
+ # Global Guardrails
321
+
322
+
323
+
324
+ ## Never Generate These Patterns
325
+
326
+ - Do not call `loadService()` at module top level; it starts resources during import.
327
+ - Do not default-export plain functions from `*.boot.*` files; `hile start` expects a Hile service.
328
+ - Do not set `ctx.body` and also return a controller value.
329
+ - Do not assume `@hile/http` Zod validation mutates or coerces `ctx.query`, `ctx.params`, or `ctx.request.body`.
330
+ - Do not put reusable business logic only in controllers, pages, queue workers, or message handlers.
331
+ - Do not use old message examples that append a secondary response getter; current request APIs return promises directly.
332
+ - Do not claim exactly-once delivery or execution from Redis locks, queues, idempotency, or rate limits.
333
+ - Do not use queue `jobId` as the only side-effect idempotency boundary.
334
+ - Do not log the entire async context by default.
package/README.md CHANGED
@@ -1,159 +1,64 @@
1
1
  # @hile/cli
2
2
 
3
- Hile 命令行工具,用于启动基于 `@hile/core` 的服务应用。支持通过 `package.json` 配置和/或自动扫描 `*.boot.{ts,js}` 加载服务,并在进程退出时执行优雅关闭。
3
+ <!-- Generated by scripts/build-ai-context.mjs from docs/ai. Do not edit by hand. -->
4
4
 
5
- ## 安装
5
+ Run hile start, scan boot files, load auto-loaded services, and start registries from the command line.
6
6
 
7
- ```bash
8
- pnpm add @hile/cli
9
- ```
10
-
11
- 全局使用:
12
-
13
- ```bash
14
- pnpm add -g @hile/cli
15
- ```
16
-
17
- ## 命令
18
-
19
- ### `hile start`
20
-
21
- 在当前工作目录(通常为项目根,且包含 `package.json`)启动服务。加载顺序如下:
22
-
23
- 1. `package.json` 中的 `hile.auto_load_packages`(如存在)
24
- 2. 运行时目录下的 `*.boot.ts` / `*.boot.js`
25
-
26
- 若两者都未提供可加载服务,CLI 输出 `no services to load` 并退出。每个加载项的默认导出都必须通过 `isService` 校验,否则抛出 `invalid service file`。
27
-
28
- ```bash
29
- hile start
30
- hile start --dev
31
- ```
32
-
33
- | 选项 | 说明 | 默认值 |
34
- |------|------|--------|
35
- | `-d, --dev` | 开发模式:`NODE_ENV=development`,使用 tsx,扫描 `src/` | `false` |
36
- | `-e, --env-file <path>` | 加载 env 文件到 `process.env`,可多次指定 | — |
37
-
38
- - 非 `--dev` 模式:`NODE_ENV=production`
39
- - `--dev` 模式:`NODE_ENV=development`
40
-
41
- 示例:
42
-
43
- ```bash
44
- hile start --env-file .env --env-file .env.local
45
- ```
46
-
47
- 依赖 Node 20.12+ 原生 `process.loadEnvFile()`。
48
-
49
- ### 启动日志(基于 @hile/core 事件)
50
-
51
- CLI 已接入容器事件日志(仅开发模式输出):
7
+ This README is intentionally short and example-first. The complete AI-facing guide ships in `AI.md` in this package.
52
8
 
53
- - 服务启动:`service:init` / `service:ready`(含耗时)
54
- - 服务失败:`service:error`(错误对象单独一行)
55
- - 关闭阶段:`service:shutdown:start` / `service:shutdown:done`
56
- - 容器关闭:`container:shutdown:start` / `container:shutdown:done`
9
+ ## When To Use
57
10
 
58
- TTY 环境下会使用颜色(成功绿、错误红、警告黄等);重定向或管道时自动禁用颜色,便于日志采集。
11
+ Use these packages when an app needs managed startup, singleton resources, dependency ordering, environment-file loading, graceful shutdown, logs, Redis, or SQL database connections.
59
12
 
60
- ### 其他命令
13
+ ## Install
61
14
 
62
15
  ```bash
63
- hile -v
64
- hile -h
65
- ```
66
-
67
- ## 运行时目录
68
-
69
- 扫描目录优先级:
70
-
71
- 1. `HILE_RUNTIME_DIR`(若设置)
72
- 2. `--dev` 模式:`src/`
73
- 3. 生产模式:`dist/`
74
-
75
- ```bash
76
- HILE_RUNTIME_DIR=./custom hile start
77
- ```
78
-
79
- ## package.json 配置(可选)
80
-
81
- 可在项目根目录配置 `hile.auto_load_packages`,用于在扫描 boot 文件之前先加载指定模块:
82
-
83
- ```json
84
- {
85
- "name": "my-app",
86
- "hile": {
87
- "auto_load_packages": ["@hile/http", "my-local-service"]
88
- }
89
- }
16
+ pnpm add @hile/cli
90
17
  ```
91
18
 
92
- 规则:
93
-
94
- - 数组项必须是模块名(与 `import('module')` 语义一致)
95
- - 按数组顺序加载,然后再扫描 `*.boot.{ts,js}`
96
- - 每个模块默认导出必须是合法 Hile 服务(通过 `isService`)
19
+ ## Copy-Paste Example
97
20
 
98
- ## Boot 文件规范
99
-
100
- **约定**:**`src/services/`** **`*.boot.*`** 与 **`*.service.*`** **同属 service 模块**(均 **`defineService`**):**`.boot`** = CLI **自启动**,**`.service`** = **`loadService`** **依赖加载**。二者**必须**位于 **`src/services/`**。
101
-
102
- 每个 `*.boot.ts` / `*.boot.js` 文件必须默认导出一个服务:
103
-
104
- ```typescript
105
- // src/services/database.boot.ts(*.boot 须放在 src/services/)
106
- import { defineService, loadService } from '@hile/core'
107
- import { configService } from './config.service'
21
+ ```ts
22
+ // src/services/http.boot.ts
23
+ import { defineService } from '@hile/core'
24
+ import { Http } from '@hile/http'
108
25
 
109
26
  export default defineService('http', async (shutdown) => {
110
- const config = await loadService(configService)
111
- const pool = await createPool(config.dbUrl)
112
- shutdown(() => pool.end())
113
- return pool
27
+ const http = new Http({ port: Number(process.env.HTTP_PORT ?? 3000) })
28
+ await http.load(new URL('../controllers', import.meta.url).pathname)
29
+ const close = await http.listen()
30
+ shutdown(close)
31
+ return http
114
32
  })
115
33
  ```
116
34
 
117
- 要求:
118
-
119
- - 文件名后缀必须为 `.boot.ts` 或 `.boot.js`
120
- - 必须存在 `default` 导出
121
- - 导出值必须为 `defineService` / `container.register` 返回值
122
-
123
- ## 优雅关闭
124
-
125
- 进程收到 `SIGTERM`、`SIGINT` 等信号时,CLI 通过 `registerExitHook` 注册的退出钩子会:
35
+ Run:
126
36
 
127
- 1. 先 **await** `container.shutdown()`(确保所有在 defineService 中注册的 shutdown 回调被执行)
128
- 2. **仅在其完成后**再取消事件订阅并调用 exit 退出进程
129
-
130
- 若 `shutdown()` 未完成,进程会挂起不退出;内部将 async-exit-hook 的强制退出超时设为极大值,等效于「等 shutdown 完成再退出」。
37
+ ```bash
38
+ hile start --dev --env-file .env
39
+ ```
131
40
 
132
- ## 项目结构示例
41
+ ## Boundaries
133
42
 
134
- ```text
135
- my-app/
136
- ├── src/
137
- │ └── services/
138
- │ ├── http.boot.ts
139
- │ ├── database.boot.ts
140
- │ ├── config.service.ts
141
- │ └── cache.service.ts
142
- ├── package.json
143
- └── tsconfig.json
144
- ```
43
+ - Do not use `loadService()` at module top level.
44
+ - Do not use `auto_load_packages` for file paths. It accepts package names only.
45
+ - Do not use the default `@hile/ioredis` or `@hile/typeorm` services when one app needs multiple Redis or database connections; define separate services with separate keys.
145
46
 
146
- 服务加载来源:先加载 `hile.auto_load_packages`(如有),再加载运行时目录下的 `*.boot.{ts,js}`。其余服务通过 `loadService` 按需加载。
47
+ - Reusing one service key for unrelated factories.
48
+ - Missing `shutdown()` callbacks for network connections or servers.
49
+ - Assuming TypeORM env config fills entities automatically when `TYPEORM_ENTITIES` is not set.
50
+ - Calling `transaction()` and manually committing; the helper commits or rolls back internally.
147
51
 
148
- ## 开发
52
+ ## Verify
149
53
 
150
- ```bash
151
- pnpm install
152
- pnpm build
153
- pnpm dev
154
- pnpm test
155
- ```
54
+ - Boot files default-export `defineService(...)`.
55
+ - Long-lived resources register teardown callbacks.
56
+ - `auto_load_packages` contains package names, not local files.
57
+ - `hile start --dev` can find `src/**/*.boot.ts`.
58
+ - Production build emits `dist/**/*.boot.js`.
156
59
 
157
- ## License
60
+ ## More Context
158
61
 
159
- MIT
62
+ - `AI.md` in this package: full package-local AI guide.
63
+ - Root `llms-full.txt`: full monorepo AI context.
64
+ - Root `references/`: source files copied from `docs/ai`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/cli",
3
- "version": "2.1.1",
3
+ "version": "3.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -10,7 +10,8 @@
10
10
  },
11
11
  "files": [
12
12
  "dist",
13
- "README.md"
13
+ "README.md",
14
+ "AI.md"
14
15
  ],
15
16
  "bin": {
16
17
  "hile": "./dist/index.js"
@@ -27,12 +28,12 @@
27
28
  "vitest": "^4.0.18"
28
29
  },
29
30
  "dependencies": {
30
- "@hile/bootstrap": "^2.1.1",
31
- "@hile/logger": "^2.1.1",
32
- "@hile/micro": "^2.1.1",
31
+ "@hile/bootstrap": "^3.0.0",
32
+ "@hile/logger": "^3.0.0",
33
+ "@hile/micro": "^3.0.1",
33
34
  "commander": "^14.0.3",
34
35
  "pino-pretty": "^13.1.3",
35
36
  "yaml": "^2.9.0"
36
37
  },
37
- "gitHead": "7903ae989bd001d1ed1437cb90c9e828a1909061"
38
+ "gitHead": "0985b6f8abc1f4de0a36324063585fdc3ac1375b"
38
39
  }