🚀 构建部署工具包 - 一个功能强大的 Node.js 工具包,提供文件复制、SVN 操作、系统通知确认等功能,专为自动化构建部署流程设计。
- 🔄 智能重试机制 - 自动处理网络异常和临时错误
- 📢 跨平台通知 - 支持 Windows、macOS、Linux 系统通知
- 🤖 自动化模式 - 支持 CI/CD 环境的无人值守操作
- 📁 文件操作 - 高效的文件复制、删除等操作
- 🔗 SVN 集成 - 完整的 SVN 更新、提交、删除等操作
- 📊 进度提示 - 直观的进度条和状态反馈
- 🛠️ 命令行工具 - 提供便捷的 CLI 命令
- 📝 详细日志 - 完整的操作日志和错误信息
npm install -g build-deploy-tools
npm install build-deploy-tools --save-dev
npx build-deploy-tools --help
# 交互模式(默认)
build-copy
# 自动模式
build-copy --auto
# 自动模式 + 自动提交 SVN
build-copy --auto --commit
# 自定义构建文件名和目标目录
build-copy --build=myapp --target=D:/Projects/deployment
# 测试系统通知功能
test-notification
# 自动模式测试
test-notification --auto
const { BuildDeployTools } = require('build-deploy-tools')
// 创建工具实例
const tools = new BuildDeployTools({
maxRetries: 3,
retryDelay: 2000
})
// 执行构建复制
async function deploy() {
try {
await tools.executeBuildCopy({
sourceDir: './dist',
targetParentDir: 'D:/Projects/deployment',
fileName: 'myapp',
autoCommit: true
})
console.log('部署成功!')
} catch (error) {
console.error('部署失败:', error.message)
}
}
{
"scripts": {
"build": "vite build",
"deploy": "npm run build && build-copy --auto",
"deploy-commit": "npm run build && build-copy --auto --commit",
"test-notification": "test-notification"
},
"devDependencies": {
"build-deploy-tools": "^1.1.0"
}
}
# 构建并手动确认部署
npm run deploy
# 自动构建部署并提交
npm run deploy-commit
# 测试通知功能
npm run test-notification
在 vue.config.js
或 webpack.config.js
中配置:
const { BuildDeployTools } = require('build-deploy-tools')
// 检查是否应该执行部署插件
function shouldExecuteDeployPlugin() {
// 只有包含 'build-copy' 的npm脚本才执行插件
const scriptName = process.env.npm_lifecycle_event || ''
const scriptCommand = process.env.npm_lifecycle_script || ''
console.log(`🔍 当前npm脚本: ${scriptName}`)
console.log(`🔍 脚本命令: ${scriptCommand}`)
// 检查脚本名称或命令中是否包含 build-copy
const shouldExecute = scriptName.includes('build-copy') ||
scriptCommand.includes('build-copy') ||
scriptName.includes('deploy')
console.log(`🔍 是否执行部署插件: ${shouldExecute}`)
return shouldExecute
}
module.exports = {
// ... 其他配置
configureWebpack: {
plugins: [
// 在生产环境下创建自定义插件,在构建完成后执行复制操作
process.env.NODE_ENV === 'production' && shouldExecuteDeployPlugin()
? {
apply: compiler => {
// 使用webpack的done钩子,确保在打包完成后执行
compiler.hooks.done.tapAsync('BuildDeployPlugin', async (stats, callback) => {
try {
console.log('📦 构建完成,开始执行文件复制...')
// 检查构建是否成功
if (stats.hasErrors()) {
console.error('❌ 构建有错误,跳过文件复制')
callback()
return
}
// 执行文件复制操作
const buildDeployTools = new BuildDeployTools({
maxRetries: 3,
retryDelay: 2000,
})
await buildDeployTools.executeBuildCopy({
sourceDir: './dist', // 构建输出目录
targetParentDir: 'D:/Work/Vue3/development',
autoCommit: true // 根据需要设置
})
console.log('✅ 文件复制完成')
callback()
} catch (error) {
console.error('❌ 文件复制失败:', error)
callback(error)
}
})
},
}
: null,
].filter(Boolean), // 过滤掉null值
},
}
在 vite.config.js
中配置:
import { defineConfig } from 'vite'
import { BuildDeployTools } from 'build-deploy-tools'
export default defineConfig({
// ... 其他配置
plugins: [
// ... 其他插件
{
name: 'build-deploy',
closeBundle: async () => {
if (process.env.NODE_ENV === 'production') {
console.log('📦 Vite构建完成,开始执行文件复制...')
const buildDeployTools = new BuildDeployTools()
try {
await buildDeployTools.executeBuildCopy({
sourceDir: './dist',
targetParentDir: 'D:/Work/Vue3/development',
autoCommit: false
})
console.log('✅ 文件复制完成')
} catch (error) {
console.error('❌ 文件复制失败:', error)
}
}
}
}
]
})
在 rollup.config.js
中配置:
import { BuildDeployTools } from 'build-deploy-tools'
export default {
// ... 其他配置
plugins: [
// ... 其他插件
{
name: 'build-deploy',
writeBundle: async () => {
if (process.env.NODE_ENV === 'production') {
console.log('📦 Rollup构建完成,开始执行文件复制...')
const buildDeployTools = new BuildDeployTools()
try {
await buildDeployTools.executeBuildCopy({
sourceDir: './dist',
targetParentDir: 'D:/Work/Vue3/development'
})
console.log('✅ 文件复制完成')
} catch (error) {
console.error('❌ 文件复制失败:', error)
}
}
}
}
]
}
利用 npm 的 post
钩子:
{
"scripts": {
"build": "vite build",
"postbuild": "build-copy --auto",
"build:prod": "vite build",
"postbuild:prod": "build-copy --auto --commit"
}
}
创建 scripts/build-and-deploy.js
:
const { execSync } = require('child_process')
const { BuildDeployTools } = require('build-deploy-tools')
async function buildAndDeploy() {
try {
console.log('🚀 开始构建...')
// 执行构建命令
execSync('npm run build', { stdio: 'inherit' })
console.log('📦 构建完成,开始部署...')
// 执行部署
const tools = new BuildDeployTools()
await tools.executeBuildCopy({
sourceDir: './dist',
targetParentDir: 'D:/Work/Vue3/development',
autoCommit: process.env.AUTO_COMMIT === 'true'
})
console.log('🎉 构建和部署完成!')
} catch (error) {
console.error('❌ 失败:', error.message)
process.exit(1)
}
}
buildAndDeploy()
在 package.json
中添加:
{
"scripts": {
"deploy": "node scripts/build-and-deploy.js",
"deploy:auto": "AUTO_COMMIT=true node scripts/build-and-deploy.js"
}
}
参数 | 说明 | 示例 |
---|---|---|
--auto |
启用自动模式 | build-copy --auto |
--commit |
强制自动提交到 SVN | build-copy --commit |
--no-commit |
禁止提交到 SVN | build-copy --no-commit |
--no-notification |
禁用系统通知 | build-copy --no-notification |
--build=<name> |
指定构建文件名 | build-copy --build=myapp |
--target=<path> |
指定目标目录 | build-copy --target=D:/Projects |
变量 | 说明 | 示例 |
---|---|---|
CI=true |
CI 环境自动启用自动模式 | CI=true build-copy |
npm_config_auto=true |
启用自动模式 | npm run build-copy --auto |
npm_config_commit_cli=true |
启用自动提交 | npm run build-copy --commit |
npm_config_notification=false |
禁用通知 | npm run build-copy --notification=false |
npm_config_build=filename |
指定构建文件名 | npm run build-copy --build=myapp |
本工具支持以下平台:
- ✅ Windows (Windows 10/11)
- ✅ macOS (macOS 10.14+)
- ✅ Linux (Ubuntu, CentOS, 等)
const tools = new BuildDeployTools(options)
选项 (options):
-
maxRetries
(number): 最大重试次数,默认 3 -
retryDelay
(number): 重试延迟时间(毫秒),默认 2000 -
defaultFileName
(string): 默认文件名,默认 'vam3'
执行完整的构建复制流程
参数:
-
config.sourceDir
(string): 源目录路径 -
config.targetParentDir
(string): 目标父目录路径 -
config.fileName
(string, 可选): 构建文件名 -
config.autoCommit
(boolean, 可选): 是否自动提交到 SVN
返回: Promise<boolean>
测试通知功能
返回: Promise<boolean>
解决方案:
# 使用命令行模式
build-copy --no-notification
解决方案:
- 确保目标目录是 SVN 工作副本
- 检查 SVN 权限和网络连接
解决方案:
# 先执行构建
npm run build
# 再执行部署
npm run deploy
欢迎贡献代码!请遵循以下步骤:
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/amazing-feature
) - 提交更改 (
git commit -m 'Add some amazing feature'
) - 推送到分支 (
git push origin feature/amazing-feature
) - 创建 Pull Request
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
Build Deploy Tools - 让构建部署更简单、更可靠! 🚀