Skip to content

xr - 运行脚本

统一的脚本运行命令,自动检测并使用项目对应的包管理器。

用法

bash
xr <script> [args...]

参数

参数描述
scriptpackage.json 中定义的脚本名称
args传递给脚本的额外参数

示例

bash
# 运行 dev 脚本
xr dev

# 运行 build 脚本
xr build

# 运行测试
xr test

# 传递额外参数
xr test --coverage
xr build --mode production

等效命令对照

xr 命令npmyarnpnpmbun
xr devnpm run devyarn devpnpm devbun run dev
xr buildnpm run buildyarn buildpnpm buildbun run build
xr testnpm run testyarn testpnpm testbun run test

包管理器检测

xr 会按以下顺序检测使用哪个包管理器:

  1. package.jsonpackageManager 字段
  2. 锁文件(pnpm-lock.yaml → yarn.lock → bun.lockb → package-lock.json)
  3. 全局安装检测(pnpm > yarn > bun > npm)
  4. 默认使用 npm

查看可用脚本

你可以在 package.jsonscripts 字段中查看所有可用脚本:

json
{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "test": "vitest",
    "lint": "eslint ."
  }
}

然后使用 xr 运行:

bash
xr dev      # 启动开发服务器
xr build    # 构建生产版本
xr preview  # 预览构建结果
xr test     # 运行测试
xr lint     # 运行代码检查

基于 MIT 许可发布