justfile
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJustfile 命令自动化技能
Justfile 命令自动化技能
将常用命令、Makefile、npm scripts 转换为 Justfile,便于日常开发和运维工作。
将常用命令、Makefile、npm scripts 转换为 Justfile,便于日常开发和运维工作。
快速开始
快速开始
安装 just
安装 just
bash
undefinedbash
undefinedmacOS
macOS
brew install just
brew install just
Linux
Linux
curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash
curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash
验证安装
验证安装
just --version
undefinedjust --version
undefined基本使用
基本使用
bash
just # 运行默认 recipe
just --list # 列出所有 recipes
just <recipe> # 运行指定 recipe
just -n <recipe> # 干运行(只显示命令)bash
just # 运行默认 recipe
just --list # 列出所有 recipes
just <recipe> # 运行指定 recipe
just -n <recipe> # 干运行(只显示命令)工作流程决策
工作流程决策
根据用户需求选择合适的工作流程:
用户需求
├── 已有 Makefile → 转换工作流
├── 已有 package.json → npm 转换工作流
├── 有常用 shell 命令 → 命令整理工作流
├── 新项目需要自动化 → 模板生成工作流
└── 需要了解语法 → 查阅 references/syntax.md根据用户需求选择合适的工作流程:
用户需求
├── 已有 Makefile → 转换工作流
├── 已有 package.json → npm 转换工作流
├── 有常用 shell 命令 → 命令整理工作流
├── 新项目需要自动化 → 模板生成工作流
└── 需要了解语法 → 查阅 references/syntax.md1. Makefile 转换
1. Makefile 转换
将现有 Makefile 转换为 Justfile。
自动转换
bash
python scripts/makefile_to_just.py Makefile justfile手动转换要点
- 移除 声明(just 默认都是 phony)
.PHONY - →
$(VAR){{var}} - →
$(shell cmd)`cmd` - 保持 前缀(静默执行)
@ - 文件依赖需要移除或改为 recipe 依赖
转换示例
makefile
undefined将现有 Makefile 转换为 Justfile。
自动转换
bash
python scripts/makefile_to_just.py Makefile justfile手动转换要点
- 移除 声明(just 默认都是 phony)
.PHONY - →
$(VAR){{var}} - →
$(shell cmd)`cmd` - 保持 前缀(静默执行)
@ - 文件依赖需要移除或改为 recipe 依赖
转换示例
makefile
undefinedMakefile
Makefile
.PHONY: build test
VERSION := $(shell git describe --tags)
build:
go build -ldflags "-X main.version=$(VERSION)"
test: build
go test ./...
转换为:
```just.PHONY: build test
VERSION := $(shell git describe --tags)
build:
go build -ldflags "-X main.version=$(VERSION)"
test: build
go test ./...
转换为:
```justjustfile
justfile
version :=
git describe --tagsbuild:
go build -ldflags "-X main.version={{version}}"
test: build
go test ./...
undefinedversion :=
git describe --tagsbuild:
go build -ldflags "-X main.version={{version}}"
test: build
go test ./...
undefined2. npm scripts 转换
2. npm scripts 转换
将 package.json 中的 scripts 转换为 Justfile。
自动转换
bash
python scripts/npm_to_just.py package.json justfile转换示例
json
{
"scripts": {
"dev": "vite",
"build": "vite build",
"test": "vitest",
"lint": "eslint ."
}
}转换为:
just
set dotenv-load
default: dev
dev:
npm run dev
build:
npm run build
test:
npm run test
lint:
npm run lint将 package.json 中的 scripts 转换为 Justfile。
自动转换
bash
python scripts/npm_to_just.py package.json justfile转换示例
json
{
"scripts": {
"dev": "vite",
"build": "vite build",
"test": "vitest",
"lint": "eslint ."
}
}转换为:
just
set dotenv-load
default: dev
dev:
npm run dev
build:
npm run build
test:
npm run test
lint:
npm run lint或者直接使用原始命令
或者直接使用原始命令
dev:
vite
build:
vite build
undefineddev:
vite
build:
vite build
undefined3. Shell 命令整理
3. Shell 命令整理
将常用 shell 命令整理为 Justfile。
从 history 提取
bash
python scripts/shell_to_just.py --history justfile从文件读取
bash
undefined将常用 shell 命令整理为 Justfile。
从 history 提取
bash
python scripts/shell_to_just.py --history justfile从文件读取
bash
undefined创建命令列表
创建命令列表
cat > commands.txt << 'EOF'
docker-compose up -d
docker-compose logs -f
kubectl get pods -n production
kubectl logs -f deployment/api
EOF
python scripts/shell_to_just.py commands.txt justfile
**手动整理模式**
收集用户常用命令,按功能分组:
```justcat > commands.txt << 'EOF'
docker-compose up -d
docker-compose logs -f
kubectl get pods -n production
kubectl logs -f deployment/api
EOF
python scripts/shell_to_just.py commands.txt justfile
**手动整理模式**
收集用户常用命令,按功能分组:
```just=== Docker ===
=== Docker ===
up:
docker-compose up -d
down:
docker-compose down
logs service="":
docker-compose logs -f {{service}}
up:
docker-compose up -d
down:
docker-compose down
logs service="":
docker-compose logs -f {{service}}
=== Kubernetes ===
=== Kubernetes ===
pods:
kubectl get pods -n production
logs-k8s pod:
kubectl logs -f {{pod}} -n production
undefinedpods:
kubectl get pods -n production
logs-k8s pod:
kubectl logs -f {{pod}} -n production
undefined4. 项目模板生成
4. 项目模板生成
根据项目类型生成合适的 Justfile 模板。
根据项目类型生成合适的 Justfile 模板。
识别项目类型
识别项目类型
检查项目文件来识别类型:
| 文件 | 项目类型 |
|---|---|
| Python |
| Node.js |
| Go |
| Rust |
| Docker |
| Kubernetes |
| Terraform |
检查项目文件来识别类型:
| 文件 | 项目类型 |
|---|---|
| Python |
| Node.js |
| Go |
| Rust |
| Docker |
| Kubernetes |
| Terraform |
生成模板
生成模板
查阅 获取各类项目的完整模板:
references/templates.md- Python 项目模板
- Node.js 项目模板
- Go 项目模板
- DevOps/运维模板
- 通用开发模板
- 数据科学/ML 模板
查阅 获取各类项目的完整模板:
references/templates.md- Python 项目模板
- Node.js 项目模板
- Go 项目模板
- DevOps/运维模板
- 通用开发模板
- 数据科学/ML 模板
常用 Recipe 模式
常用 Recipe 模式
带参数的 Recipe
带参数的 Recipe
just
undefinedjust
undefined必需参数
必需参数
deploy env:
kubectl apply -k overlays/{{env}}
deploy env:
kubectl apply -k overlays/{{env}}
默认值参数
默认值参数
build mode="debug":
cargo build --{{mode}}
build mode="debug":
cargo build --{{mode}}
可变参数
可变参数
test *args:
pytest {{args}}
undefinedtest *args:
pytest {{args}}
undefined依赖关系
依赖关系
just
undefinedjust
undefined简单依赖
简单依赖
release: build test
./deploy.sh
release: build test
./deploy.sh
带参数的依赖
带参数的依赖
push: (build "release")
docker push myapp:latest
undefinedpush: (build "release")
docker push myapp:latest
undefined条件执行
条件执行
just
undefinedjust
undefined跨平台
跨平台
[linux]
install:
apt install mypackage
[macos]
install:
brew install mypackage
[linux]
install:
apt install mypackage
[macos]
install:
brew install mypackage
确认提示
确认提示
[confirm("确定要部署到生产环境吗?")]
deploy-prod:
kubectl apply -k overlays/prod
undefined[confirm("确定要部署到生产环境吗?")]
deploy-prod:
kubectl apply -k overlays/prod
undefined分组
分组
just
[group('development')]
dev:
npm run dev
[group('development')]
watch:
npm run watch
[group('testing')]
test:
npm run testjust
[group('development')]
dev:
npm run dev
[group('development')]
watch:
npm run watch
[group('testing')]
test:
npm run test脚本模式
脚本模式
just
[script]
setup:
#!/usr/bin/env bash
set -euo pipefail
echo "Setting up environment..."
for pkg in curl git make; do
command -v $pkg || echo "Missing: $pkg"
donejust
[script]
setup:
#!/usr/bin/env bash
set -euo pipefail
echo "Setting up environment..."
for pkg in curl git make; do
command -v $pkg || echo "Missing: $pkg"
done最佳实践
最佳实践
1. 文件组织
1. 文件组织
just
undefinedjust
undefined头部:设置和变量
头部:设置和变量
set dotenv-load
set shell := ["bash", "-cu"]
project := "myapp"
version :=
git describe --tags --alwaysset dotenv-load
set shell := ["bash", "-cu"]
project := "myapp"
version :=
git describe --tags --always默认 recipe
默认 recipe
default: dev
default: dev
按功能分组,用注释分隔
按功能分组,用注释分隔
=== 开发 ===
=== 开发 ===
dev: ...
dev: ...
=== 构建 ===
=== 构建 ===
build: ...
build: ...
=== 测试 ===
=== 测试 ===
test: ...
undefinedtest: ...
undefined2. 命名约定
2. 命名约定
- 使用小写字母和连字符:
build-prod - 动词开头:,
run,build,testdeploy - 私有 recipe 用下划线:
_helper
- 使用小写字母和连字符:
build-prod - 动词开头:,
run,build,testdeploy - 私有 recipe 用下划线:
_helper
3. 文档注释
3. 文档注释
just
undefinedjust
undefined启动开发服务器 (端口 3000)
启动开发服务器 (端口 3000)
dev:
npm run dev
undefineddev:
npm run dev
undefined4. 变量使用
4. 变量使用
just
undefinedjust
undefined从环境变量读取,带默认值
从环境变量读取,带默认值
env := env("ENV", "development")
env := env("ENV", "development")
从命令获取
从命令获取
commit :=
git rev-parse --short HEADcommit :=
git rev-parse --short HEAD条件变量
条件变量
mode := if env == "prod" { "release" } else { "debug" }
undefinedmode := if env == "prod" { "release" } else { "debug" }
undefined参考资料
参考资料
- - Justfile 完整语法参考
references/syntax.md - - 各类项目模板集合
references/templates.md - - Makefile 转换脚本
scripts/makefile_to_just.py - - npm scripts 转换脚本
scripts/npm_to_just.py - - Shell 命令提取脚本
scripts/shell_to_just.py
- - Justfile 完整语法参考
references/syntax.md - - 各类项目模板集合
references/templates.md - - Makefile 转换脚本
scripts/makefile_to_just.py - - npm scripts 转换脚本
scripts/npm_to_just.py - - Shell 命令提取脚本
scripts/shell_to_just.py