server-deploy-cli
Version:
connect server with ssh2-sftp-client, and publish local files to remote server or delete some files from the remote server or other operations you wanna do
100 lines (96 loc) • 3.41 kB
JavaScript
/*
* @Author: famous
* @Date: 2022-10-13 14:06:40
* @Last Modified by: famous
* @Last Modified time: 2023-07-11 11:33:56
*
* deploy-cli [options] <args>:
*
* 可以直接执行 deploy-cli ,将根据默认配置文件(项目根目录的 deploy.config.js)的配置部署项目
*
* Options:
* -b, --backup: 将服务器的当前项目备份至本地,deploy-cli -b -c <deployConfigRelativePath>
* -c, --config: 指定配置文件相对项目根目录的路径,deploy-cli -c <deployConfigRelativePath>
* -d, --delete: 删除服务器某个文件,deploy-cli -d <remoteFilePath> <serverIndex>
* -g, --get: 下载服务器某个文件,deploy-cli -g <remoteFilePath> <localFileRelativePath> <serverIndex>,The local path should include the filename to use for saving the file.
* -gd, --get-dir: 下载服务器某个文件夹,deploy-cli -gd <remoteDirPath> <localDirRelativePath> <serverIndex>
* -h, --help: 打印命令帮助信息,deploy-cli -h
* -rd, --rm-dir: 删除服务器某个目录(包含本身),deploy-cli -rd <remoteDirPath> <serverIndex>
* -sh, --shell: 执行配置中的 .sh 文件命令(可指定配置文件),deploy-cli -sh <deployConfigRelativePath>
* -v, --version: server-deploy-cli 工具当前版本,deploy-cli -v
*
* 注:1、单次命令只解析第一个 option ,即: process.argv[2] ,其它的 option(-c 除外) 不做处理
* 2、配置文件中存在多个服务器时, serverIndex 默认取第0个,也可通过命令传入下标 index 值,即 serverIndex 参数
* 3、-b, -d, -g, -gd 及 -rd 等 option 后面可跟上 -c <deployConfigRelativePath>, 默认不跟则取默认配置文件的相关配置
*/
// const logHelp = require('./help')
import logHelp from './help.js'
const validOptions = [
{
key: '-b',
alias: '--backup',
operator: 'backup',
},
{
key: '-c',
alias: '--config',
operator: 'deploy',
},
{
key: '-d',
alias: '--delete',
operator: 'delete',
},
{
key: '-g',
alias: '--get',
operator: 'get',
},
{
key: '-gd',
alias: '--get-dir',
operator: 'getDir',
},
{
key: '-h',
alias: '--help',
operator: 'help',
},
{
key: '-rd',
alias: '--rm-dir',
operator: 'rmDir',
},
{
key: '-sh',
alias: '--shell',
operator: 'shell',
},
{
key: '-v',
alias: '--version',
operator: 'version',
}
]
export default () => {
return new Promise( (resolve, reject) => {
const argv = process.argv
const optionKey = argv[2]
if (!optionKey) { //no option, go to deploy with default deploy config
return resolve({
operator: 'deploy',
args: []
})
}
const option = validOptions.find( option => option.key === optionKey || option.alias === optionKey ),
args = argv.slice(2)
if (!option) { //unknown option
logHelp()
return reject(`unknown option: ${ args.join(' ') }.`)
}
resolve({
operator: option.operator,
args: args.slice(1)
})
})
}