const testWebDir = 'E:/Wi/web/test'; // 忽略同步的目录 const ignoreProj = ['prod']; // 同步时需要保留的文件路径(相对路径) const ignoreSyncPath = ['static/config/globalConfig.js']; // 部署后同步文件到同层其他项目目录,且忽略项目配置文件 const remoteRunAfter = `cd /d ${testWebDir} && powershell -Command "$currentDir = Split-Path -Leaf (Get-Location); $ignoreDirs = @('${ignoreProj.join( "','" )}', $currentDir); $ignoreSyncPaths = @('${ignoreSyncPath.join( "','" )}'); $parentDir = Split-Path (Get-Location) -Parent; Get-ChildItem $parentDir -Directory | Where-Object { -not $ignoreDirs.Contains($_.Name) } | ForEach-Object { $targetDir = $_.FullName; $backupFiles = @{}; foreach ($ignorePath in $ignoreSyncPaths) { $fullIgnorePath = Join-Path $targetDir $ignorePath; if (Test-Path $fullIgnorePath) { $tempPath = Join-Path $env:TEMP ('backup_' + [guid]::NewGuid().ToString()); Copy-Item -Path $fullIgnorePath -Destination $tempPath -Force; $backupFiles[$ignorePath] = $tempPath; } }; Get-ChildItem $targetDir -Recurse | Where-Object { $_.FullName -ne $targetDir } | Remove-Item -Force -Recurse; Get-ChildItem -Path '.' -Recurse -File | Where-Object { $relativePath = $_.FullName.Substring((Get-Location).Path.Length + 1); $shouldCopy = $true; foreach ($ignorePath in $ignoreSyncPaths) { if ($relativePath -like $ignorePath) { $shouldCopy = $false; break; } }; $shouldCopy } | ForEach-Object { $relativePath = $_.FullName.Substring((Get-Location).Path.Length + 1); $targetPath = Join-Path $targetDir $relativePath; $targetParent = Split-Path $targetPath -Parent; if (-not (Test-Path $targetParent)) { New-Item -Path $targetParent -ItemType Directory -Force | Out-Null }; Copy-Item -Path $_.FullName -Destination $targetPath -Force }; foreach ($ignorePath in $ignoreSyncPaths) { if ($backupFiles.ContainsKey($ignorePath)) { $fullIgnorePath = Join-Path $targetDir $ignorePath; $parentFolder = Split-Path $fullIgnorePath -Parent; if (-not (Test-Path $parentFolder)) { New-Item -Path $parentFolder -ItemType Directory -Force | Out-Null }; Move-Item -Path $backupFiles[$ignorePath] -Destination $fullIgnorePath -Force } } }"`; module.exports = { privateKeyPath: 'C:/ssh/id_rsa', // 本地私钥地址,位置一般在C:/Users/xxx/.ssh/id_rsa,非必填,有私钥则配置 passphrase: '', // 本地私钥密码,非必填,有私钥则配置 projectName: '', // 项目名称 // 根据需要进行配置,如只需部署prod线上环境,请删除dev测试环境配置,反之亦然,支持多环境部署 dev: { // 测试环境 name: '测试环境', preRun: '', // 测试环境打包脚本 host: '47.100.245.85', // 测试服务器地址 port: 22, // ssh port,一般默认22 username: 'Administrator', // 登录服务器用户名 password: '', // 登录服务器密码 distPath: 'dist', // 本地打包dist目录 webDir: testWebDir, // // 测试环境服务器地址 remoteIgnorePath: 'WI水务智能助手.apk', // 远程服务器忽略删除文件或文件夹,可填数组或字符串 // 备份 bakOld: { // 备份个数,不填默认 3 个 num: 3, // 是否备份,默认启用备份 enable: true, }, // 远程执行命令 remoteRun: { // 部署前执行命令 before: '', // 部署后执行命令 after: remoteRunAfter, }, // 部署预览 preview: { url: 'https://wi.beng35.com/web/test', // 部署后检查部署效果,查看的网页地址 openInBrowser: false, // 部署完成后是否自动打开部署的网页地址 }, }, prod: { // 线上环境 name: '线上环境', preRun: '', // 线上环境打包脚本 host: '47.100.245.85', // 线上服务器地址 port: 22, // ssh port,一般默认22 username: 'administrator', // 登录服务器用户名 password: '', // 登录服务器密码 distPath: 'dist', // 本地打包dist目录 webDir: 'E:/Wi/web/prod', // 线上环境服务器地址 remoteIgnorePath: 'WI水务智能助手.apk', // 远程服务器忽略删除文件或文件夹,可填数组或字符串 // 备份 bakOld: { // 备份个数,不填默认 3 个 num: 3, // 是否备份,默认启用备份 enable: true, }, preview: { url: 'https://wi.beng35.com/', openInBrowser: false, }, }, // 再还有多余的环境按照这个格式写即可 };