rename: from>fromDist, dir>basePath, dist>distDir, server>targetType

This commit is contained in:
陆柯 2021-07-01 08:22:13 +08:00
parent 0e62d06af9
commit 23c98bcd00

View File

@ -9,21 +9,21 @@ const deepmerge = require('deepmerge')
const wo = { global.wo = { const wo = { global.wo = {
envi: { envi: {
deploy: { deploy: {
from: './dist', fromDist: './dist',
toTarget: 'github', toTarget: 'github',
vultr: { vultr: {
server: 'ssh', targetType: 'ssh',
host: undefined, host: undefined,
port: 22, port: 22,
dir: undefined, basePath: undefined,
dist: 'dist', distDir: 'dist',
user: undefined, user: undefined,
password: undefined, password: undefined,
key: `${process.env.HOME}/.ssh/id_rsa`, key: `${process.env.HOME}/.ssh/id_rsa`,
}, },
github: { github: {
server: 'git', targetType: 'git',
repo: undefined, repo: undefined,
branch: 'master', branch: 'master',
gitname: undefined, gitname: undefined,
@ -34,7 +34,7 @@ const wo = { global.wo = {
} }
} }
} }
}) }}
// 读取配置文件 // 读取配置文件
try { try {
@ -51,15 +51,15 @@ wo.envi.deploy.connection = wo.envi.deploy[wo.envi.deploy.toTarget]
// 读取命令行参数 // 读取命令行参数
commander commander
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v就要加 '-v --version' .version('1.0', '-v, --version') // 默认是 -V。如果要 -v就要加 '-v --version'
.option('-f, --from <from>', `local path to copy from. Default to ${wo.envi.deploy.from}`) .option('-f, --fromDist <fromDist>', `local distribution path to copy from. Default to ${wo.envi.deploy.fromDist}`)
.option('-t, --toTarget <toTarget>', `connection section in config. Default to ${wo.envi.deploy.toTarget}`) .option('-t, --toTarget <toTarget>', `connection section in config. Default to ${wo.envi.deploy.toTarget}`)
.option('-s, --server <server>', `server type, git or ssh. Default to ${wo.envi.deploy.connection.server}`) .option('-T, --targetType <targetType>', `target type, git or ssh. Default to ${wo.envi.deploy.connection.targetType}`)
.option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${wo.envi.deploy.connection.host}`) .option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${wo.envi.deploy.connection.host}`)
.option('-P, --port <port>', `Ssh port number of the target server. Default to ${wo.envi.deploy.connection.port}`) .option('-P, --port <port>', `Ssh port number of the target server. Default to ${wo.envi.deploy.connection.port}`)
.option('-D, --dir <dir>', `Destination path to deploy on the target server. Default to ${wo.envi.deploy.connection.dir}`) .option('-b, --basePath <basePath>', `Destination path to deploy on the target server. Default to ${wo.envi.deploy.connection.basePath}`)
.option('-d, --dist <dist>', `Destination folder to deploy on the target server. Default to ${wo.envi.deploy.connection.dist}`) .option('-d, --distDir <distDir>', `Destination folder to deploy on the target server. Default to ${wo.envi.deploy.connection.distDir}`)
.option('-r, --repo <repo>', `git repo address. Default to ${wo.envi.deploy.connection.repo}`) .option('-r, --repo <repo>', `git repo address. Default to ${wo.envi.deploy.connection.repo}`)
.option('-b, --branch <branch>', `git repo branch. Default to ${wo.envi.deploy.connection.branch}`) .option('-b, --branch <branch>', `git repo branch. Default to ${wo.envi.deploy.connection.branch}`)
@ -71,16 +71,17 @@ commander
.option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "". Default to "${wo.envi.deploy.connection.password}"`) .option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "". Default to "${wo.envi.deploy.connection.password}"`)
.parse(process.argv) .parse(process.argv)
wo.envi.deploy.from = commander.from || wo.envi.deploy.from wo.envi.deploy.fromDist = commander.fromDist || wo.envi.deploy.fromDist
wo.envi.deploy.connection = wo.envi.deploy[commander.toTarget || wo.envi.deploy.toTarget] // 使用用户指定的连接
wo.envi.deploy.connection = wo.envi.deploy[commander.toTarget || wo.envi.deploy.toTarget] // 使用用户指定的连接
// 可以用命令行参数覆盖掉配置文件
const connection = { const connection = {
server: commander.server || wo.envi.deploy.connection.server, targetType: commander.targetType || wo.envi.deploy.connection.targetType,
host: commander.host || wo.envi.deploy.connection.host, host: commander.host || wo.envi.deploy.connection.host,
port: commander.port || wo.envi.deploy.connection.port, port: commander.port || wo.envi.deploy.connection.port,
dir: commander.dir || wo.envi.deploy.connection.dir, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上 basePath: commander.basePath || wo.envi.deploy.connection.basePath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
dist: commander.dist || wo.envi.deploy.connection.dist, // 新系统将发布在这个文件夹里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。 distDir: commander.distDir || wo.envi.deploy.connection.distDir, // 新系统将发布在这个文件夹里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。
repo: commander.repo || wo.envi.deploy.connection.repo, repo: commander.repo || wo.envi.deploy.connection.repo,
branch: commander.branch || wo.envi.deploy.connection.branch, branch: commander.branch || wo.envi.deploy.connection.branch,
@ -98,11 +99,11 @@ const connection = {
}, },
} }
console.log(` deploy from ${wo.envi.deploy.from} to ${JSON.stringify(connection)}`) console.log(` deploy from ${wo.envi.deploy.fromDist} to ${JSON.stringify(connection)}`)
if (connection.server==='ssh') { if (connection.targetType==='ssh') {
deployToSsh(connection) deployToSsh(connection)
}else if (connection.server==='git'){ }else if (connection.targetType==='git'){
deployToGit(connection) deployToGit(connection)
} }
@ -132,19 +133,19 @@ function deployToSsh(connection){
} }
ssh.connect(connection).then(async () => { ssh.connect(connection).then(async () => {
console.log(`[ mv ${connection.dist} ${connection.dist}-backup-${new Date().toISOString()} ... ]`) console.log(`[ mv ${connection.distDir} ${connection.distDir}-backup-${new Date().toISOString()} ... ]`)
await ssh.execCommand(`mv ${connection.dist} ${connection.dist}-backup-${new Date().toISOString()}`, { cwd: connection.dir }) await ssh.execCommand(`mv ${connection.distDir} ${connection.distDir}-backup-${new Date().toISOString()}`, { cwd: connection.basePath })
console.log(`[ mkdir ${connection.dist} ... ]`) console.log(`[ mkdir ${connection.distDir} ... ]`)
await ssh.execCommand(`mkdir ${connection.dist}`, { cwd: connection.dir }) await ssh.execCommand(`mkdir ${connection.distDir}`, { cwd: connection.basePath })
const toCreate = necessaryPath(path.join('./', wo.envi.deploy.from)) const toCreate = necessaryPath(path.join('./', wo.envi.deploy.fromDist))
for (const name of toCreate) { for (const name of toCreate) {
console.log(`[ mkdir ${connection.dist}/${name.join('/')} ... ]`) console.log(`[ mkdir ${connection.distDir}/${name.join('/')} ... ]`)
await ssh.execCommand(`mkdir ${connection.dist}/${name.join('/')}`, { cwd: connection.dir }) await ssh.execCommand(`mkdir ${connection.distDir}/${name.join('/')}`, { cwd: connection.basePath })
} }
let err let err
console.log(`[ Upload to ${connection.dir}/${connection.dist} ... ]`) console.log(`[ Upload to ${connection.basePath}/${connection.distDir} ... ]`)
await ssh.putDirectory(path.join('./', wo.envi.deploy.from), `${connection.dir}/${connection.dist}`, { await ssh.putDirectory(path.join('./', wo.envi.deploy.fromDist), `${connection.basePath}/${connection.distDir}`, {
concurrency: 10, concurrency: 10,
recursive: true, recursive: true,
validate: itemPath => { validate: itemPath => {
@ -234,7 +235,7 @@ function deployToGit(connection){
function exec() { function exec() {
const baseDir = '' const baseDir = ''
const deployDir = pathFn.join(baseDir, '.deploy_git') const deployDir = pathFn.join(baseDir, '.deploy_git')
const publicDir = wo.envi.deploy.from const fromDir = wo.envi.deploy.fromDist
let extendDirs = connection.extend_dirs let extendDirs = connection.extend_dirs
const ignoreHidden = connection.ignore_hidden const ignoreHidden = connection.ignore_hidden
const ignorePattern = connection.ignore_pattern const ignorePattern = connection.ignore_pattern
@ -315,7 +316,7 @@ function deployToGit(connection){
opts.ignorePattern = new RegExp(ignorePattern.public) opts.ignorePattern = new RegExp(ignorePattern.public)
} }
return fs.copyDir(publicDir, deployDir, opts) return fs.copyDir(fromDir, deployDir, opts)
}).then(() => { }).then(() => {
// log.info('Copying files from extend dirs...') // log.info('Copying files from extend dirs...')