Finch CLI

Finch CLI 是一个命令行工具,用于创建项目、运行开发服务器、构建、运行测试以及管理数据库迁移。它封装了常用的 Dart 命令并添加了 Finch 特定的任务。

安装

dart pub global activate finch

命令

运行 finch -h 查看所有可用命令:

finch -h
✔ templates
        显示可用模板列表
✔ create
        创建新项目
        -p, --path       项目路径
        -n, --name       项目名称
        -d, --docker     使用 Docker
        -t, --template   项目模板 [simple, example,...]
✔ get
        获取项目依赖包 (dart pub get)
✔ runner
        运行项目 build runner (dart pub run build_runner build)
✔ run
        运行项目 (dart run)
        -p, --path       app 文件路径
        -a, --args       app 文件参数
✔ serve
        使用文件监视器运行项目
        -p, --path       app 文件路径
        -a, --args       app 文件参数
✔ build
        构建项目 (dart compile exe)
        -c, --cli        为 CLI 构建
        -a, --appPath    app 文件路径
        -l, --langPath   语言文件路径
        -p, --publicPath 公共路径
        -w, --widgetPath Widget 路径
        -e, --envPath    环境文件路径 (.env)
        -o, --output     输出路径
        -t, --type       构建类型 (zip, exe)
✔ migrate
        管理 MySQL 和 SQLite 迁移
        -c, --create     创建新迁移文件
        -n, --name       迁移文件名称(仅与 --create 一起使用)
        -s, --sqlite     目标为 SQLite 而非 MySQL
✔ test
        运行单元测试 (dart test)
        -r, --reporter   设置测试结果打印方式

        -h, --help       显示帮助
        -v, --version    显示 Finch 版本
        -u, --update     更新 Finch CLI

常用示例

使用 example 模板创建新项目

finch create -n my_app -t example

使用文件监视器运行开发服务器

serve 命令会监视 widget 和语言文件的变更,并在不重启服务器的情况下自动重新加载:

finch serve

或指定入口文件:

finch serve -p lib/serve.dart

构建生产二进制文件

finch build -a lib/app.dart -o ./build/app

运行数据库迁移

# 应用所有待处理的 MySQL 迁移
finch migrate --init

# 创建新的 MySQL 迁移文件
finch migrate --create --name add_users_table

# 应用 SQLite 迁移
finch migrate --init --sqlite

migrate 命令委托给正在运行的应用内置迁移系统。详情请参阅 Database Migration