From 0e62d06af95c9b88df183ac71be3ef75b75b343a Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sat, 19 Jun 2021 22:02:36 +0800 Subject: [PATCH] u --- deploy.js | 124 +++++++++++++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/deploy.js b/deploy.js index 5e3c4f6..c0f6922 100644 --- a/deploy.js +++ b/deploy.js @@ -6,88 +6,90 @@ const commander = require('commander') const deepmerge = require('deepmerge') // 默认参数 -var Config = { - deploy: { - from: './dist', - toTarget: 'github', - - vultr: { - server: 'ssh', - host: undefined, - port: 22, - dir: undefined, - dist: 'dist', - user: undefined, - password: undefined, - key: `${process.env.HOME}/.ssh/id_rsa`, - }, - github: { - server: 'git', - repo: undefined, - branch: 'master', - gitname: undefined, - gitemail: undefined, - user: undefined, - password: undefined, - key: `${process.env.HOME}/.ssh/id_rsa`, +const wo = { global.wo = { + envi: { + deploy: { + from: './dist', + toTarget: 'github', + + vultr: { + server: 'ssh', + host: undefined, + port: 22, + dir: undefined, + dist: 'dist', + user: undefined, + password: undefined, + key: `${process.env.HOME}/.ssh/id_rsa`, + }, + github: { + server: 'git', + repo: undefined, + branch: 'master', + gitname: undefined, + gitemail: undefined, + user: undefined, + password: undefined, + key: `${process.env.HOME}/.ssh/id_rsa`, + } } } -} +}) // 读取配置文件 try { let configFile if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigDeploy.js'))) { - Config = deepmerge(Config, require(configFile)) + wo.envi = deepmerge(wo.envi, require(configFile)) console.info(`${configFile} loaded`) } } catch (err) { console.error('Loading config files failed: ' + err.message) } -Config.deploy.connection = Config.deploy[Config.deploy.toTarget] +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 ${Config.deploy.from}`) - .option('-t, --toTarget ', `connection section in config. Default to ${Config.deploy.toTarget}`) + .option('-f, --from ', `local path to copy from. Default to ${wo.envi.deploy.from}`) + .option('-t, --toTarget ', `connection section in config. Default to ${wo.envi.deploy.toTarget}`) - .option('-s, --server ', `server type, git or ssh. Default to ${Config.deploy.connection.server}`) + .option('-s, --server ', `server type, git or ssh. Default to ${wo.envi.deploy.connection.server}`) - .option('-H, --host ', `Host IP or domain name of the target server. Default to ${Config.deploy.connection.host}`) - .option('-P, --port ', `Ssh port number of the target server. Default to ${Config.deploy.connection.port}`) - .option('-D, --dir ', `Destination path to deploy on the target server. Default to ${Config.deploy.connection.dir}`) - .option('-d, --dist ', `Destination folder to deploy on the target server. Default to ${Config.deploy.connection.dist}`) + .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('-r, --repo ', `git repo address. Default to ${Config.deploy.connection.repo}`) - .option('-b, --branch ', `git repo branch. Default to ${Config.deploy.connection.branch}`) - .option('-n, --gitname ', `git user name. Default to ${Config.deploy.connection.gitname}`) - .option('-e, --gitemail ', `git user email. Default to ${Config.deploy.connection.gitemail}`) + .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}`) + .option('-n, --gitname ', `git user name. Default to ${wo.envi.deploy.connection.gitname}`) + .option('-e, --gitemail ', `git user email. Default to ${wo.envi.deploy.connection.gitemail}`) - .option('-u, --user ', `User id to login the target server. Default to ${Config.deploy.connection.user}`) - .option('-k, --key ', `User private key file to login the target server. Default to ${Config.deploy.connection.key}`) - .option('-p, --password ', `User password to login the target server. You may have to enclose it in "". Default to "${Config.deploy.connection.password}"`) + .option('-u, --user ', `User id to login the target server. Default to ${wo.envi.deploy.connection.user}`) + .option('-k, --key ', `User private key file to login the target server. Default to ${wo.envi.deploy.connection.key}`) + .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) -Config.deploy.from = commander.from || Config.deploy.from -Config.deploy.connection = Config.deploy[commander.toTarget || Config.deploy.toTarget] // 使用用户指定的连接 +wo.envi.deploy.from = commander.from || wo.envi.deploy.from +wo.envi.deploy.connection = wo.envi.deploy[commander.toTarget || wo.envi.deploy.toTarget] // 使用用户指定的连接 const connection = { - server: commander.server || Config.deploy.connection.server, + server: commander.server || wo.envi.deploy.connection.server, - host: commander.host || Config.deploy.connection.host, - port: commander.port || Config.deploy.connection.port, - dir: commander.dir || Config.deploy.connection.dir, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上 - dist: commander.dist || Config.deploy.connection.dist, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。 + 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产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。 - repo: commander.repo || Config.deploy.connection.repo, - branch: commander.branch || Config.deploy.connection.branch, - gitname: commander.gitname || Config.deploy.connection.gitname, - gitemail: commander.gitemail || Config.deploy.connection.gitemail, + repo: commander.repo || wo.envi.deploy.connection.repo, + branch: commander.branch || wo.envi.deploy.connection.branch, + gitname: commander.gitname || wo.envi.deploy.connection.gitname, + gitemail: commander.gitemail || wo.envi.deploy.connection.gitemail, - username: commander.user || Config.deploy.connection.user, - privateKey: fs.existsSync(commander.key || Config.deploy.key) ? (commander.key || Config.deploy.key) : undefined, - password: commander.password || Config.deploy.connection.password, + username: commander.user || wo.envi.deploy.connection.user, + privateKey: fs.existsSync(commander.key || wo.envi.deploy.key) ? (commander.key || wo.envi.deploy.key) : undefined, + password: commander.password || wo.envi.deploy.connection.password, tryKeyboard: true, onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => { // 不起作用 if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) { @@ -96,7 +98,7 @@ const connection = { }, } -console.log(` deploy from ${Config.deploy.from} to ${JSON.stringify(connection)}`) +console.log(` deploy from ${wo.envi.deploy.from} to ${JSON.stringify(connection)}`) if (connection.server==='ssh') { deployToSsh(connection) @@ -134,7 +136,7 @@ function deployToSsh(connection){ 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('./', Config.deploy.from)) + const toCreate = necessaryPath(path.join('./', wo.envi.deploy.from)) for (const name of toCreate) { console.log(`[ mkdir ${connection.dist}/${name.join('/')} ... ]`) await ssh.execCommand(`mkdir ${connection.dist}/${name.join('/')}`, { cwd: connection.dir }) @@ -142,7 +144,7 @@ function deployToSsh(connection){ let err console.log(`[ Upload to ${connection.dir}/${connection.dist} ... ]`) - await ssh.putDirectory(path.join('./', Config.deploy.from), `${connection.dir}/${connection.dist}`, { + await ssh.putDirectory(path.join('./', wo.envi.deploy.from), `${connection.dir}/${connection.dist}`, { concurrency: 10, recursive: true, validate: itemPath => { @@ -232,7 +234,7 @@ function deployToGit(connection){ function exec() { const baseDir = '' const deployDir = pathFn.join(baseDir, '.deploy_git') - const publicDir = Config.deploy.from + const publicDir = wo.envi.deploy.from let extendDirs = connection.extend_dirs const ignoreHidden = connection.ignore_hidden const ignorePattern = connection.ignore_pattern @@ -263,8 +265,8 @@ function deployToGit(connection){ } function setup() { - const userName = Config.deploy.gitname || '' - const userEmail = Config.deploy.gitemail || '' + const userName = wo.envi.deploy.gitname || '' + const userEmail = wo.envi.deploy.gitemail || '' // Create a placeholder for the first commit return fs.writeFile(pathFn.join(deployDir, 'placeholder'), '').then(() => {