Skip to content

Commit

Permalink
修复上传文件夹卡死问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fuchengwei committed Sep 21, 2020
1 parent f8b6643 commit 3372d78
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
62 changes: 51 additions & 11 deletions lib/commands/deploy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const ora = require('ora')
const inquirer = require('inquirer')
const archiver = require('archiver')
const { NodeSSH } = require('node-ssh')
const childProcess = require('child_process')
const { deployConfigPath } = require('../config')
Expand Down Expand Up @@ -103,6 +104,35 @@ const execBuild = async (config, index) => {
}
}

// 打包Zip
const buildZip = async (config, index) => {
await new Promise((resolve, reject) => {
log(`(${index}) 打包 ${underline(config.distPath)} Zip`)
const archive = archiver('zip', {
zlib: { level: 9 }
}).on('error', (e) => {
error(e)
})

const output = fs
.createWriteStream(`${process.cwd()}/${config.distPath}.zip`)
.on('close', (e) => {
if (e) {
error(`打包zip出错: ${e}`)
reject(e)
process.exit(1)
} else {
succeed(`${underline(`${config.distPath}.zip`)} 打包成功`)
resolve()
}
})

archive.pipe(output)
archive.directory(config.distPath, false)
archive.finalize()
})
}

// 连接ssh
const connectSSH = async (config, index) => {
try {
Expand All @@ -118,24 +148,24 @@ const connectSSH = async (config, index) => {
// 上传本地文件
const uploadLocalFile = async (config, index) => {
try {
const backPath = `${config.webDir}.back`
const localPath = `${process.cwd()}/${config.distPath}`
const localFileName = `${config.distPath}.zip`
const remoteFileName = `${config.webDir}.zip`
const localPath = `${process.cwd()}/${localFileName}`

log(`(${index}) 上传打包文件至目录 ${underline(config.webDir)}`)
log(`(${index}) 上传打包zip至目录 ${underline(remoteFileName)}`)

const spinner = ora('正在上传中\n')

spinner.start()

await ssh.putDirectory(localPath, backPath, {
recursive: true,
concurrency: 10
await ssh.putFile(localPath, remoteFileName, null, {
concurrency: 1
})

spinner.stop()
succeed('上传成功')
} catch (e) {
error(e)
error(`上传失败: ${e}`)
process.exit(1)
}
}
Expand All @@ -148,19 +178,27 @@ const removeRemoteFile = async (config, index) => {
log(`(${index}) 删除远程文件 ${underline(webDir)}`)

await ssh.execCommand(`rm -rf ${webDir}`)

succeed('删除成功')
} catch (e) {
error(e)
process.exit(1)
}
}

// 移动远程文件
const moveRemoteFile = async (config) => {
// 解压远程文件
const unzipRemoteFile = async (config, index) => {
try {
const { webDir } = config
const remoteFileName = `${webDir}.zip`

log(`(${index}) 解压远程文件 ${underline(remoteFileName)}`)

await ssh.execCommand(
`unzip -o ${remoteFileName} -d ${webDir} && rm -rf ${remoteFileName}`
)

await ssh.execCommand(`mv ${webDir}.back ${webDir}`)
succeed('解压成功')
} catch (e) {
error(e)
process.exit(1)
Expand Down Expand Up @@ -188,6 +226,7 @@ const removeLocalFile = (config, index) => {
}

remove(localPath)
fs.unlinkSync(`${localPath}.zip`)
succeed('删除本地打包目录成功')
}

Expand All @@ -202,11 +241,12 @@ const createTaskList = (config) => {

taskList = []
taskList.push(execBuild)
taskList.push(buildZip)
taskList.push(connectSSH)
taskList.push(uploadLocalFile)
isRemoveRemoteFile && taskList.push(removeRemoteFile)
taskList.push(unzipRemoteFile)
taskList.push(removeLocalFile)
taskList.push(moveRemoteFile)
taskList.push(disconnectSSH)
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deploy-cli-service",
"version": "1.1.0",
"version": "1.1.2",
"description": "前端一键自动化部署脚手架服务",
"main": "lib/service.js",
"homepage": "https://github.com/fuchengwei/deploy-cli-service",
Expand All @@ -20,6 +20,7 @@
"url": "git+https://github.com/fuchengwei/deploy-cli-service"
},
"dependencies": {
"archiver": "^5.0.2",
"chalk": "^4.1.0",
"commander": "^6.1.0",
"inquirer": "^7.3.3",
Expand Down

0 comments on commit 3372d78

Please sign in to comment.