@nilejs/cli

中文文档
1.0.2发布于 2026/11/02ES Module

CLI for scaffolding and generating Nile backend projects

原始使用文档
文档目录9 节
  1. Create a project
  2. Generate code
  3. Services
  4. Actions
  5. Schemas and types
  6. Current starter
  7. Coding assistants
  8. Requirements
  9. License

@nilejs/cli

NPM Version

Create Nile Js projects and generate services, actions and schemas.

Create a project

bunx @nilejs/cli new my-app
cd my-app
bun install
cp .env.example .env
bun run dev

The server starts on http://localhost:8000. The starter stores its data in an embedded PGlite database, so there is no database server to install. The getting started guide walks through the starter step by step.

The starter includes:

  • src/index.ts: server entry point on Bun's native HTTP server, with a logger and an onBoot schema push
  • src/db/: PGlite and Drizzle client, the tasks table and a taskModel built with createModel
  • src/services/: an explicit registry and domain entry point for a tasks service with create, list, get, update and delete actions
  • Dockerfile, docker-compose.yml and docker-compose.dev.yml for container deployments
  • drizzle.config.ts, .env.example, tsconfig.json and package.json

Generate code

Run the generators from the project root. The starter's gen script calls the CLI for you.

CommandDescription
bun run gen service <name>Add a service with a sample action
bun run gen action <service> <name>Add an action to an existing service
bun run gen action <service> <name> --folderAdd an action in its own folder, for private helper modules
bun run gen schemaGenerate Zod schemas and TypeScript types from your actions

generate also has the alias gen, so bunx @nilejs/cli gen service users works outside the starter.

Services

bun run gen service users

Creates:

src/services/users/
  actions/sample.ts   Sample action with a schema, an input type and a named handler
  index.ts            Registers the service and its public actions

The CLI then asks whether to register the service in src/services/services.config.ts. If you decline, it prints the lines to add yourself.

Actions

bun run gen action users get-user

Creates src/services/users/actions/get-user.ts:

import { createAction } from "@nilejs/nile";
import { Ok } from "@nilejs/nile/fp";
import z from "zod";

const getUserSchema = z.object({
  // Describe the input this action accepts
});

type GetUserInput = z.infer<typeof getUserSchema>;

const getUserHandler = async (input: GetUserInput) => {
  // Implement users.get-user here
  return Ok({ result: input });
};

export const getUserAction = createAction<GetUserInput>({
  name: "get-user",
  description: "GetUser action for users",
  validation: getUserSchema,
  handler: getUserHandler,
});

Kebab-case names become camelCase variables and PascalCase types. Register the action by importing it in src/services/users/index.ts and adding it to the service’s actions list. The root registry imports services, not individual actions.

Schemas and types

bun run gen schema

Reads every action's validation schema from src/services/services.config.ts and writes:

  • src/generated/schemas.ts: named Zod schemas, such as tasksCreateSchema
  • src/generated/types.ts: input types, such as TasksCreatePayload
FlagDescription
-e, --entry <path>Path to the services config (detected by default)
-o, --output <path>Output directory (default: src/generated)

Every registered action is included in ServicePayloads. Actions without a validation schema accept an object payload (Record<string, unknown>), so actions such as tasks.list can be called with {}. This fallback does not add server-side validation. Schemas that cannot be converted are reported separately. Share the generated files with a frontend to validate forms with the same rules as the server.

Current starter

CLI 1.0.2 targets Nile ^1.0.4, Zod 4, TypeScript 7 and the Drizzle model helpers. The generated layout passes Nile’s strict boot checks. Database setup returns a Result and prevents startup on failure; shutdown drains Nile’s work before closing PGlite. Helpers are available as utils.createModel, and Result helpers come from @nilejs/nile/fp.

Existing apps are not rewritten when the CLI updates. For an older starter, add a service entry point at src/services/<domain>/index.ts, register its actions there, and import that service from services.config.ts. Nile’s migration notes describe the current error and startup behavior.

Coding assistants

The starter includes SKILL.md with the current Nile action pattern, service layout, Results, access policies and client calls. Ask your assistant to read it before changing the backend. After updating Nile, use the version shipped at node_modules/@nilejs/nile/SKILL.md.

Requirements

  • Bun 1.4.2 or later. The starter runs on Bun, and gen schema imports your services with Bun.

License

MIT

版本与发布记录

@nilejs/cli 共发布 10 个版本,首次发布于 2026/02/28;过去 12 个月发布了 10 个版本。

版本发布日期距今
1.0.2latest2026/11/021 天前
1.0.12026/11/021 天前
1.0.02026/10/249 天前
0.0.72026/04/196 个月前
0.0.62026/04/176 个月前
0.0.52026/03/307 个月前

关于 @nilejs/cli 的常见问题

@nilejs/cli 是什么?

用于搭建和生成Nile后端项目的CLI

如何安装 @nilejs/cli?

在项目目录运行 npm install @nilejs/cli,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 nile,可以用 npx nile 直接运行。

@nilejs/cli 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/nilejs__cli 是否存在。

@nilejs/cli 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@nilejs/cli 可以商用吗?

@nilejs/cli 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@nilejs/cli 还在维护吗?

最新版本 1.0.2 发布于 2026/11/02(1 天前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/02/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。