bt-vite-deploy
bt-vite-deploy
Deploy Vite/Vue projects to BT Panel (宝塔面板) with a single command.
在当前目录查找 dist.zip(不存在则自动打包 dist/ 目录)→ 上传到宝塔 → 远程解压 → 清理。
安装
pnpm add -D bt-vite-deploy
快速开始
1. 一键初始化
安装后,在项目根目录执行:
npx bt-deploy init
这会自动:
- 创建
bt-deploy.config.ts配置模板 - 向
package.json添加"deploy": "bt-deploy"脚本
2. 编辑配置
打开 bt-deploy.config.ts,填写你的宝塔面板信息:
import { defineConfig } from 'bt-vite-deploy';
export default defineConfig({
panel: 'https://tx.example.com:12345',
remoteDir: '/www/wwwroot/example.com',
// localDir: '.', // 查找 dist.zip 的目录,默认当前目录
// timeout: 120, // 请求超时(秒),默认 120
// cleanRemote: true, // 部署前清理远程目录,默认 true
});
也支持
.js、.mjs、.cjs、.json格式,或在package.json中添加btDeploy字段。
3. 设置 API 密钥
# Windows
set BT_KEY=你的宝塔API密钥
# Linux / macOS
export BT_KEY=你的宝塔API密钥
也可在配置文件中直接设置
key字段(不推荐提交到版本控制)。
4. 构建并部署
# 构建项目(生成 dist/ 目录)
pnpm run build
# 执行部署(自动查找 dist.zip,不存在则从 dist/ 打包)
pnpm run deploy
部署流程
固定 4 步,上传 dist.zip 一个文件:
- 在
localDir(默认当前目录)中查找dist.zip- 存在 → 直接使用
- 不存在 → 查找
dist/目录,自动打包为dist.zip - 两者都不存在 → 报错中止
- 列出远程目录文件
- 批量删除远程所有文件(移入回收站)
- 上传
dist.zip→ 远程解压 → 删除远程dist.zip(自动生成的本地dist.zip也会清理)
配置选项
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
panel |
string |
(必填) | 宝塔面板地址(不带末尾斜杠) |
remoteDir |
string |
(必填) | 服务器端目标目录 |
key |
string |
process.env.BT_KEY |
API 密钥 |
localDir |
string |
'.' |
查找 dist.zip 的目录 |
timeout |
number |
120 |
请求超时时间(秒) |
cleanRemote |
boolean |
true |
部署前是否清理远程目录 |
CLI 用法
bt-deploy init # 创建配置文件 + 添加 deploy 脚本
bt-deploy # 执行部署
bt-deploy --no-clean # 不清理远程目录
bt-deploy --local-dir build # 指定查找 dist.zip 的目录
bt-deploy --help # 查看帮助
bt-deploy --version # 查看版本
配置文件中的所有选项都可通过 CLI 参数覆盖:
bt-deploy --panel https://panel.example.com:8888 --remote-dir /www/wwwroot/site.com
API 密钥获取
- 登录宝塔面板
- 进入 面板设置 → API 接口
- 复制 API 密钥
- 确保 API 接口已开启,且你的 IP 在白名单中
认证机制
request_time = 当前 Unix 时间戳(秒)
request_token = MD5(request_time + MD5(API_KEY))
每次请求将 request_time 和 request_token 作为表单参数发送。
编程式使用
除了 CLI,也可以在代码中直接调用:
import { deploy, loadConfig, initConfig } from 'bt-vite-deploy';
// 初始化配置文件
await initConfig();
// 执行部署
const config = await loadConfig();
await deploy(config);
宝塔Linux面板入门【人人都能上手的服务器运维面板】
https://www.bilibili.com/cheese/play/ss22181
License
MIT
Related Packages
vue-cli-plugin-s3-deploy-cleanup
A vue-cli plugin that helps cleanup old build artifacts from S3 by tagging them...
2,109
6
Version History
| Version | Released | License |
|---|---|---|
| 1.5.3 | MIT | |
| 1.5.2 | MIT | |
| 1.5.1 | MIT | |
| 1.5.0 | MIT | |
| 1.4.0 | MIT | |
| 1.3.0 | MIT | |
| 1.2.0 | MIT | |
| 1.1.0 | MIT | |
| 1.0.0 | MIT |