Skip to content

Commit

Permalink
添加秘钥登录&&是否删除远程文件配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
fuchengwei committed Sep 19, 2020
1 parent b6a6805 commit a26ccca
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 79 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"tabWidth": 2,
"singleQuote": true,
"semi": false,
"jsxSingleQuote": true
"jsxSingleQuote": true,
"trailingComma": "none"
}
147 changes: 104 additions & 43 deletions lib/commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ const {
log,
succeed,
error,
underline,
underline
} = require('../utils')

const ssh = new NodeSSH()
const maxBuffer = 5000 * 1024

// 任务列表
let taskList

// 是否确认部署
const confirmDeploy = (message) => {
return inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message,
},
message
}
])
}

Expand All @@ -34,30 +37,46 @@ const checkEnvCorrect = (config, env) => {
'host',
'port',
'username',
'password',
'distPath',
'webDir',
'webDir'
]

if (config) {
if (
config &&
(function () {
const { privateKey, password } = config
if (!privateKey && !password) {
error(
`配置错误: 请配置 ${underline('privateKey')}${underline(
'passwrod'
)}`
)
process.exit(1)
}
return true
})()
) {
keys.forEach((key) => {
if (!config[key] || config[key] === '/') {
error(
`${underline(`${env}环境`)} ${underline(`${key}属性`)} 配置不正确`
`配置错误: ${underline(`${env}环境`)} ${underline(
`${key}属性`
)} 配置不正确`
)
process.exit(1)
}
})
} else {
error(`未指定部署环境或指定部署环境不存在`)
error('配置错误: 未指定部署环境或指定部署环境不存在')
process.exit(1)
}
}

// 执行打包脚本
const execBuild = async (script) => {
const execBuild = async (config, index) => {
try {
log(`(1) ${script}`)
const { script } = config
log(`(${index}) ${script}`)
const spinner = ora('正在打包中\n')

spinner.start()
Expand Down Expand Up @@ -85,9 +104,9 @@ const execBuild = async (script) => {
}

// 连接ssh
const connectSSH = async (config) => {
const connectSSH = async (config, index) => {
try {
log(`(2) ssh连接 ${underline(config.host)}`)
log(`(${index}) ssh连接 ${underline(config.host)}`)
await ssh.connect(config)
succeed('ssh连接成功')
} catch (e) {
Expand All @@ -96,31 +115,21 @@ const connectSSH = async (config) => {
}
}

// 删除远程文件
const removeRemoteFile = async (webDir) => {
try {
log(`(3) 删除远程文件 ${underline(webDir)}`)
await ssh.execCommand(`rm -rf ${webDir}`)
succeed('删除成功')
} catch (e) {
error(e)
process.exit(1)
}
}

// 上传本地文件
const uploadLocalFile = async (config) => {
const uploadLocalFile = async (config, index) => {
try {
log(`(4) 上传打包文件至目录 ${underline(config.webDir)}`)

const backPath = `${config.webDir}.back`
const localPath = `${process.cwd()}/${config.distPath}`

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

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

spinner.start()

await ssh.putDirectory(localPath, config.webDir, {
await ssh.putDirectory(localPath, backPath, {
recursive: true,
concurrency: 10,
concurrency: 10
})

spinner.stop()
Expand All @@ -131,16 +140,38 @@ const uploadLocalFile = async (config) => {
}
}

// 断开ssh
const disconnectSSH = () => {
ssh.dispose()
// 删除远程文件
const removeRemoteFile = async (config, index) => {
try {
const { webDir } = config

log(`(${index}) 删除远程文件 ${underline(webDir)}`)

await ssh.execCommand(`rm -rf ${webDir}`)
succeed('删除成功')
} catch (e) {
error(e)
process.exit(1)
}
}

// 移动远程文件
const moveRemoteFile = async (config) => {
try {
const { webDir } = config

await ssh.execCommand(`mv ${webDir}.back ${webDir}`)
} catch (e) {
error(e)
process.exit(1)
}
}

// 删除本地打包文件
const removeLocalFile = (distPath) => {
const localPath = `${process.cwd()}/${distPath}`
const removeLocalFile = (config, index) => {
const localPath = `${process.cwd()}/${config.distPath}`

log(`(5) 删除本地打包目录 ${underline(localPath)}`)
log(`(${index}) 删除本地打包目录 ${underline(localPath)}`)

const remove = (path) => {
if (fs.existsSync(path)) {
Expand All @@ -160,26 +191,56 @@ const removeLocalFile = (distPath) => {
succeed('删除本地打包目录成功')
}

// 断开ssh
const disconnectSSH = () => {
ssh.dispose()
}

// 创建任务列表
const createTaskList = (config) => {
const { isRemoveRemoteFile = true } = config

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

// 执行任务列表
const executeTaskList = async (config) => {
for (const [index, execute] of new Map(
taskList.map((execute, index) => [index, execute])
)) {
await execute(config, index + 1)
}
}

module.exports = {
description: '部署项目',
apply: async (env) => {
if (checkDeployConfigExists()) {
const config = require(deployConfigPath)
const projectName = config.projectName
const envConfig = config[env]
const envConfig = Object.assign(config[env], {
privateKey: config.privateKey,
passphrase: config.passphrase
})

checkEnvCorrect(envConfig, env)

const answers = await confirmDeploy(
`${underline(projectName)} 项目是否部署到 ${underline(envConfig.name)}?`
)

if (answers.confirm) {
await execBuild(envConfig.script)
await connectSSH(envConfig)
await removeRemoteFile(envConfig.webDir)
await uploadLocalFile(envConfig)
disconnectSSH()
removeLocalFile(envConfig.distPath)
createTaskList(envConfig)

await executeTaskList(envConfig)

succeed(
`恭喜您,${underline(projectName)}项目已在${underline(
envConfig.name
Expand All @@ -195,5 +256,5 @@ module.exports = {
)
process.exit(1)
}
},
}
}
7 changes: 5 additions & 2 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
checkDeployConfigExists,
succeed,
error,
underline,
underline
} = require('../utils')
const { inquirerConfig, deployConfigPath } = require('../config')

Expand All @@ -18,6 +18,8 @@ const getUserInputInfo = () => {
const createJsonObj = (userInputInfo) => {
const jsonObj = {
projectName: userInputInfo.projectName,
privateKey: userInputInfo.privateKey,
passphrase: userInputInfo.passphrase
}
const { deployEnvList } = userInputInfo

Expand All @@ -31,6 +33,7 @@ const createJsonObj = (userInputInfo) => {
password: userInputInfo[`${env}Password`],
distPath: userInputInfo[`${env}DistPath`],
webDir: userInputInfo[`${env}WebDir`],
isRemoveRemoteFile: userInputInf[`${env}IsRemoveRemoteFile`]
}
}

Expand Down Expand Up @@ -68,5 +71,5 @@ module.exports = {
process.exit(0)
})
}
},
}
}
Loading

0 comments on commit a26ccca

Please sign in to comment.