2022-05-18 16:40:55 +00:00
|
|
|
|
---
|
|
|
|
|
order: 2
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# NocoBase CLI
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
NocoBase CLI 旨在帮助你开发、构建和部署 NocoBase 应用。
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
<Alert>
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
NocoBase CLI 支持 ts-node 和 node 两种运行模式
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
- ts-node 模式(默认):用于开发环境,支持实时编译,但是响应较慢
|
|
|
|
|
- node 模式:用于生产环境,响应迅速,但需要先执行 `yarn nocobase build` 将全部源码进行编译
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
</Alert>
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
## 使用说明
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase -h
|
|
|
|
|
|
|
|
|
|
Usage: nocobase [command] [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-h, --help
|
|
|
|
|
|
|
|
|
|
Commands:
|
2022-10-31 03:52:17 +00:00
|
|
|
|
create-plugin 创建插件脚手架
|
2022-05-18 16:40:55 +00:00
|
|
|
|
console
|
2022-10-31 03:52:17 +00:00
|
|
|
|
db:auth 校验数据库是否连接成功
|
|
|
|
|
db:sync 通过 collections 配置生成相关数据表和字段
|
|
|
|
|
install 安装
|
|
|
|
|
start 生产环境启动应用
|
|
|
|
|
build 编译打包
|
|
|
|
|
clean 删除编译之后的文件
|
|
|
|
|
dev 启动应用,用于开发环境,支持实时编译
|
|
|
|
|
doc 文档开发
|
|
|
|
|
test 测试
|
2022-05-18 16:40:55 +00:00
|
|
|
|
umi
|
2022-10-31 03:52:17 +00:00
|
|
|
|
upgrade 升级
|
2022-05-18 16:40:55 +00:00
|
|
|
|
help
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
## 在脚手架里应用
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
应用脚手架 `package.json` 里的 `scripts` 如下:
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"scripts": {
|
|
|
|
|
"dev": "nocobase dev",
|
|
|
|
|
"start": "nocobase start",
|
|
|
|
|
"clean": "nocobase clean",
|
|
|
|
|
"build": "nocobase build",
|
|
|
|
|
"test": "nocobase test",
|
|
|
|
|
"postinstall": "nocobase umi generate tmp"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
## 命令行扩展
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
NocoBase CLI 基于 [commander](https://github.com/tj/commander.js) 构建,你可以自由扩展命令,扩展的 command 可以写在 `app/server/index.ts` 里:
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
const app = new Application(config);
|
|
|
|
|
|
|
|
|
|
app.command('hello').action(() => {});
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
或者,写在插件里:
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
class MyPlugin extends Plugin {
|
|
|
|
|
beforeLoad() {
|
|
|
|
|
this.app.command('hello').action(() => {});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
终端运行
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase hello
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
## 内置命令行
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
按使用频率排序
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
### `dev`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
开发环境下,启动应用,代码实时编译。
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
<Alert>
|
2022-10-31 03:52:17 +00:00
|
|
|
|
NocoBase 未安装时,会自动安装(参考 install 命令)
|
2022-05-18 16:40:55 +00:00
|
|
|
|
</Alert>
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
Usage: nocobase dev [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-p, --port [port]
|
|
|
|
|
--client
|
|
|
|
|
--server
|
|
|
|
|
-h, --help
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
示例
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 启动应用,用于开发环境,实时编译
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase dev
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 只启动服务端
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase dev --server
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 只启动客户端
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase dev --client
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `start`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
生产环境下,启动应用,代码需要 yarn build。
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
<Alert>
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
- NocoBase 未安装时,会自动安装(参考 install 命令)
|
|
|
|
|
- 源码有修改时,需要重新打包(参考 build 命令)
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
</Alert>
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase start -h
|
|
|
|
|
|
|
|
|
|
Usage: nocobase start [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-p, --port
|
|
|
|
|
-s, --silent
|
|
|
|
|
-h, --help
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
示例
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 启动应用,用于生产环境,
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase start
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `install`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
安装
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase install -h
|
|
|
|
|
|
|
|
|
|
Usage: nocobase install [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-f, --force
|
|
|
|
|
-c, --clean
|
|
|
|
|
-s, --silent
|
|
|
|
|
-l, --lang [lang]
|
|
|
|
|
-e, --root-email <rootEmail>
|
|
|
|
|
-p, --root-password <rootPassword>
|
|
|
|
|
-n, --root-nickname [rootNickname]
|
|
|
|
|
-h, --help
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
示例
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 初始安装
|
|
|
|
|
yarn nocobase install -l zh-CN -e admin@nocobase.com -p admin123
|
|
|
|
|
# 删除 NocoBase 的所有数据表,并重新安装
|
|
|
|
|
yarn nocobase install -f -l zh-CN -e admin@nocobase.com -p admin123
|
|
|
|
|
# 清空数据库,并重新安装
|
|
|
|
|
yarn nocobase install -c -l zh-CN -e admin@nocobase.com -p admin123
|
2022-05-18 16:40:55 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<Alert>
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
`-f/--force` 和 `-c/--clean` 的区别
|
|
|
|
|
- `-f/--force` 删除 NocoBase 的数据表
|
|
|
|
|
- `-c/--clean` 清空数据库,所有数据表都会被删除
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
</Alert>
|
|
|
|
|
|
|
|
|
|
### `upgrade`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
升级
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
yarn nocobase upgrade
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `test`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
jest 测试,支持所有 [jest-cli](https://jestjs.io/docs/cli) 的 options,除此之外还扩展了 `-c, --db-clean` 的支持。
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase test -h
|
|
|
|
|
|
|
|
|
|
Usage: nocobase test [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
2022-10-31 03:52:17 +00:00
|
|
|
|
-c, --db-clean 运行所有测试前清空数据库
|
2022-05-18 16:40:55 +00:00
|
|
|
|
-h, --help
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
示例
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 运行所有测试文件
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase test
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 运行指定文件夹下所有测试文件
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase test packages/core/server
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 运行指定文件里的所有测试
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase test packages/core/database/src/__tests__/database.test.ts
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 运行测试前,清空数据库
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase test -c
|
|
|
|
|
yarn nocobase test packages/core/server -c
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `build`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
代码部署到生产环境前,需要将源码编译打包,如果代码有修改,也需要重新构建。
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 所有包
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase build
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 指定包
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase build app/server app/client
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `clean`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
删除编译之后的文件
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
yarn clean
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 等同于
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn rimraf -rf packages/*/*/{lib,esm,es,dist}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `doc`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
文档开发
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 启动文档
|
|
|
|
|
yarn doc --lang=zh-CN # 等同于 yarn doc dev
|
|
|
|
|
# 构建文档,默认输出到 ./docs/dist/ 目录下
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn doc build
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 查看 dist 输出的文档最终效果
|
|
|
|
|
yarn doc serve --lang=zh-CN
|
2022-05-18 16:40:55 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `db:auth`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
校验数据库是否连接成功
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase db:auth -h
|
|
|
|
|
|
|
|
|
|
Usage: nocobase db:auth [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
2022-10-31 03:52:17 +00:00
|
|
|
|
-r, --retry [retry] 重试次数
|
2022-05-18 16:40:55 +00:00
|
|
|
|
-h, --help
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `db:sync`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
通过 collections 配置生成数据表和字段
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ yarn nocobase db:sync -h
|
|
|
|
|
|
|
|
|
|
Usage: nocobase db:sync [options]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-f, --force
|
|
|
|
|
-h, --help display help for command
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `umi`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
`app/client` 基于 [umi](https://umijs.org/) 构建,可以通过 `nocobase umi` 来执行其他相关命令。
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 生成开发环境所需的 .umi 缓存
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase umi generate tmp
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `help`
|
|
|
|
|
|
2022-10-31 03:52:17 +00:00
|
|
|
|
帮助命令,也可以用 option 参数,`-h` 和 `--help`
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 查看所有 cli
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase help
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 也可以用 -h
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase -h
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 或者 --help
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase --help
|
2022-10-31 03:52:17 +00:00
|
|
|
|
# 查看 db:sync 命令的 option
|
2022-05-18 16:40:55 +00:00
|
|
|
|
yarn nocobase db:sync -h
|
|
|
|
|
```
|