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