> ## Documentation Index
> Fetch the complete documentation index at: https://pulian.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# CLI

> How hile start loads env files, auto-loaded packages, and boot services.

# CLI

Use `@hile/cli` when an application should start through `hile start` instead of a custom Node entrypoint.

## Copy-Paste Example

```ts theme={null}
// src/services/http.boot.ts
import { defineService } from '@hile/core'
import { Http } from '@hile/http'

export default defineService('http', async (shutdown) => {
  const http = new Http({ port: Number(process.env.HTTP_PORT ?? 3000) })
  await http.load(new URL('../controllers', import.meta.url).pathname)
  const close = await http.listen()
  shutdown(close)
  return http
})
```

Run:

```bash theme={null}
hile start --dev --env-file .env
```

## More Examples

Template choices:

```text theme={null}
default          HTTP API with @hile/http
next             Next.js + Hile controllers on one port
micro-http       Microservice plus HTTP endpoint
micro            Pure microservice
micro-http-next  Next.js + microservice + HTTP
monorepo         Lerna + pnpm workspace
```

Generated default HTTP shape:

```text theme={null}
src/
  controllers/
    index.controller.ts
  services/
    index.boot.ts
```

## Runtime And Lifecycle Notes

* `defineService(key, fn)` registers a service factory and does not execute it.
* `loadService(service)` executes the factory on first call and returns the cached value afterwards.
* The `shutdown` callback registers teardown functions; they run in LIFO order.
* The container tracks dependencies when service factories call `loadService()`.
* `container.shutdown()` triggers registered teardowns.
* `@hile/bootstrap` loads env files first, sets `NODE_ENV`, imports auto-load packages, scans boot files, and installs exit hooks.
* `@hile/logger.createLogger()` returns `{ logger, teardown }`.
* `@hile/ioredis.createRedis()` waits for `connect`.
* `@hile/typeorm.createDataSource()` initializes a TypeORM `DataSource`.

## Anti-Patterns

* Reusing one service key for unrelated factories.
* Missing `shutdown()` callbacks for network connections or servers.
* Assuming TypeORM env config fills entities automatically when `TYPEORM_ENTITIES` is not set.
* Calling `transaction()` and manually committing; the helper commits or rolls back internally.
