From c50d29c072267a0ca682a3b9b64e0d64194f2176 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sun, 18 Jul 2021 12:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20ConfigSecret.js=20?= =?UTF-8?q?=E5=AD=98=E6=94=BE=E5=AF=86=E7=A0=81=E7=AD=89=E6=9C=BA=E5=AF=86?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.js | 57 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/deploy.js b/deploy.js index 7482d7b..c9048bb 100644 --- a/deploy.js +++ b/deploy.js @@ -9,15 +9,15 @@ const deepmerge = require('deepmerge') const wo = (global.wo = { envi: { deploy: { - fromDist: './dist', + fromPath: './dist', gotoTarget: 'github', vultr: { targetType: 'ssh', host: undefined, port: 22, - basePath: undefined, - distDir: 'dist', + targetPath: undefined, + targetDir: 'dist', user: undefined, password: undefined, key: `${process.env.HOME}/.ssh/id_rsa`, @@ -43,6 +43,10 @@ try { wo.envi = deepmerge(wo.envi, require(configFile)) console.info(`${configFile} loaded`) } + if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigSecret.js'))) { + wo.envi = deepmerge(wo.envi, require(configFile)) + console.info(`${configFile} loaded`) + } } catch (err) { console.error('Loading config files failed: ' + err.message) } @@ -51,15 +55,15 @@ wo.envi.deploy.connection = wo.envi.deploy[wo.envi.deploy.gotoTarget] // 读取命令行参数 commander .version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version' - .option('-f, --fromDist ', `local distribution path to copy from. Default to ${wo.envi.deploy.fromDist}`) + .option('-f, --fromPath ', `local distribution path to copy from. Default to ${wo.envi.deploy.fromPath}`) .option('-g, --gotoTarget ', `connection section in config. Default to ${wo.envi.deploy.gotoTarget}`) .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('-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('-b, --targetPath ', `Destination path to deploy on the target. Default to ${wo.envi.deploy.connection.targetPath}`) + .option('-d, --targetDir ', `Destination folder to deploy on the target. Default to ${wo.envi.deploy.connection.targetDir}`) .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,35 +75,36 @@ 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.fromDist = commander.fromDist || wo.envi.deploy.fromDist +wo.envi.deploy.fromPath = commander.fromPath || wo.envi.deploy.fromPath wo.envi.deploy.connection = wo.envi.deploy[commander.gotoTarget || wo.envi.deploy.gotoTarget] // 使用用户指定的连接 // 可以用命令行参数覆盖掉配置文件 const connection = { targetType: commander.targetType || wo.envi.deploy.connection.targetType, + // for ssh host: commander.host || wo.envi.deploy.connection.host, port: commander.port || wo.envi.deploy.connection.port, - basePath: commander.basePath || wo.envi.deploy.connection.basePath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上 - distDir: commander.distDir || wo.envi.deploy.connection.distDir, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。 - + targetPath: commander.targetPath || wo.envi.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上 + targetDir: commander.targetDir || wo.envi.deploy.connection.targetDir, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。 + // for git 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, - + // common 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) => { // 不起作用 + onKeyboardInteractive: (name, instructions, lang, prompts, finish) => { // 不起作用 if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) { finish([password]) } }, } -console.log(` deploy from ${wo.envi.deploy.fromDist} to ${JSON.stringify(connection)}`) +console.log(` deploy from ${wo.envi.deploy.fromPath} to ${JSON.stringify(connection)}`) if (connection.targetType==='ssh') { deployToSsh(connection) @@ -133,19 +138,19 @@ function deployToSsh(connection){ } ssh.connect(connection).then(async () => { - 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)) + console.log(`[ mv ${connection.targetDir} ${connection.targetDir}-backup-${new Date().toISOString()} ... ]`) + await ssh.execCommand(`mv ${connection.targetDir} ${connection.targetDir}-backup-${new Date().toISOString()}`, { cwd: connection.targetPath }) + console.log(`[ mkdir ${connection.targetDir} ... ]`) + await ssh.execCommand(`mkdir ${connection.targetDir}`, { cwd: connection.targetPath }) + const toCreate = necessaryPath(path.join('./', wo.envi.deploy.fromPath)) for (const name of toCreate) { - console.log(`[ mkdir ${connection.distDir}/${name.join('/')} ... ]`) - await ssh.execCommand(`mkdir ${connection.distDir}/${name.join('/')}`, { cwd: connection.basePath }) + console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`) + await ssh.execCommand(`mkdir ${connection.targetDir}/${name.join('/')}`, { cwd: connection.targetPath }) } let err - console.log(`[ Upload to ${connection.basePath}/${connection.distDir} ... ]`) - await ssh.putDirectory(path.join('./', wo.envi.deploy.fromDist), `${connection.basePath}/${connection.distDir}`, { + console.log(`[ Upload to ${connection.targetPath}/${connection.targetDir} ... ]`) + await ssh.putDirectory(path.join('./', wo.envi.deploy.fromPath), `${connection.targetPath}/${connection.targetDir}`, { concurrency: 10, recursive: true, validate: itemPath => { @@ -233,9 +238,9 @@ function deployToGit(connection){ } function exec() { - const baseDir = '' - const deployDir = pathFn.join(baseDir, '.deploy_git') - const fromDir = wo.envi.deploy.fromDist + const targetDir = '' + const deployDir = pathFn.join(targetDir, '.deploy_git') + const fromDir = wo.envi.deploy.fromPath let extendDirs = connection.extend_dirs const ignoreHidden = connection.ignore_hidden const ignorePattern = connection.ignore_pattern @@ -330,7 +335,7 @@ function deployToGit(connection){ const mapFn = function(dir) { const opts = {} - const extendPath = pathFn.join(baseDir, dir) + const extendPath = pathFn.join(targetDir, dir) const extendDist = pathFn.join(deployDir, dir) if (typeof ignoreHidden === 'object') {