rename envi to envar

This commit is contained in:
陆柯 2022-04-03 16:01:25 +08:00
parent efc97a3aad
commit 85e6a67b04

View File

@ -7,7 +7,7 @@ const deepmerge = require('deepmerge')
// 默认参数 // 默认参数
const wo = (global.wo = { const wo = (global.wo = {
envi: { envar: {
deploy: { deploy: {
fromPath: './webroot', fromPath: './webroot',
gotoTarget: 'github', gotoTarget: 'github',
@ -40,11 +40,11 @@ const wo = (global.wo = {
try { try {
let configFile let configFile
if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigDeploy.js'))) { if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigDeploy.js'))) {
wo.envi = deepmerge(wo.envi, require(configFile)) wo.envar = deepmerge(wo.envar, require(configFile))
console.info(`${configFile} loaded`) console.info(`${configFile} loaded`)
} }
if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigSecret.js'))) { if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigSecret.js'))) {
wo.envi = deepmerge(wo.envi, require(configFile)) wo.envar = deepmerge(wo.envar, require(configFile))
console.info(`${configFile} loaded`) console.info(`${configFile} loaded`)
} }
} catch (err) { } catch (err) {
@ -74,37 +74,37 @@ commander
.option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "".`) .option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "".`)
.parse(process.argv) .parse(process.argv)
wo.envi.deploy.fromPath = commander.fromPath || wo.envi.deploy.fromPath wo.envar.deploy.fromPath = commander.fromPath || wo.envar.deploy.fromPath
wo.envi.deploy.connection = wo.envi.deploy[commander.gotoTarget || wo.envi.deploy.gotoTarget] // 使用用户指定的连接 wo.envar.deploy.connection = wo.envar.deploy[commander.gotoTarget || wo.envar.deploy.gotoTarget] // 使用用户指定的连接
// 可以用命令行参数覆盖掉配置文件 // 可以用命令行参数覆盖掉配置文件
const connection = { const connection = {
targetType: commander.targetType || wo.envi.deploy.connection.targetType, targetType: commander.targetType || wo.envar.deploy.connection.targetType,
// for ssh // for ssh
host: commander.host || wo.envi.deploy.connection.host, host: commander.host || wo.envar.deploy.connection.host,
port: commander.port || wo.envi.deploy.connection.port, port: commander.port || wo.envar.deploy.connection.port,
targetPath: commander.targetPath || wo.envi.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上 targetPath: commander.targetPath || wo.envar.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
targetDir: commander.targetDir || wo.envi.deploy.connection.targetDir, // 新系统将发布在这个文件夹里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。 targetDir: commander.targetDir || wo.envar.deploy.connection.targetDir, // 新系统将发布在这个文件夹里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。
// for git // for git
repo: commander.repo || wo.envi.deploy.connection.repo, repo: commander.repo || wo.envar.deploy.connection.repo,
branch: commander.branch || wo.envi.deploy.connection.branch, branch: commander.branch || wo.envar.deploy.connection.branch,
gitname: commander.gitname || wo.envi.deploy.connection.gitname, gitname: commander.gitname || wo.envar.deploy.connection.gitname,
gitemail: commander.gitemail || wo.envi.deploy.connection.gitemail, gitemail: commander.gitemail || wo.envar.deploy.connection.gitemail,
// common // common
username: commander.user || wo.envi.deploy.connection.user, username: commander.user || wo.envar.deploy.connection.user,
privateKey: fs.existsSync(commander.key || wo.envi.deploy.connection.key) ? (commander.key || wo.envi.deploy.connection.key) : undefined, privateKey: fs.existsSync(commander.key || wo.envar.deploy.connection.key) ? (commander.key || wo.envar.deploy.connection.key) : undefined,
password: commander.password || wo.envi.deploy.connection.password, password: commander.password || wo.envar.deploy.connection.password,
tryKeyboard: true, tryKeyboard: true,
onKeyboardInteractive: (name, instructions, lang, prompts, finish) => { // 不起作用 onKeyboardInteractive: (name, instructions, lang, prompts, finish) => { // 不起作用
if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) { if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
finish([password]) finish([password])
} }
}, },
url: wo.envi.deploy.connection.url url: wo.envar.deploy.connection.url
} }
console.log(` deploy from ${wo.envi.deploy.fromPath} to ${JSON.stringify(connection)}`) console.log(` deploy from ${wo.envar.deploy.fromPath} to ${JSON.stringify(connection)}`)
if (connection.targetType==='ssh') { if (connection.targetType==='ssh') {
deployToSsh(connection) deployToSsh(connection)
@ -142,7 +142,7 @@ function deployToSsh(connection){
await ssh.execCommand(`mv ${connection.targetDir} ${connection.targetDir}-backup-${new Date().toISOString()}`, { cwd: connection.targetPath }) await ssh.execCommand(`mv ${connection.targetDir} ${connection.targetDir}-backup-${new Date().toISOString()}`, { cwd: connection.targetPath })
console.log(`[ mkdir ${connection.targetDir} ... ]`) console.log(`[ mkdir ${connection.targetDir} ... ]`)
await ssh.execCommand(`mkdir ${connection.targetDir}`, { cwd: connection.targetPath }) await ssh.execCommand(`mkdir ${connection.targetDir}`, { cwd: connection.targetPath })
const toCreate = necessaryPath(path.join('./', wo.envi.deploy.fromPath)) const toCreate = necessaryPath(path.join('./', wo.envar.deploy.fromPath))
for (const name of toCreate) { for (const name of toCreate) {
console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`) console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`)
await ssh.execCommand(`mkdir ${connection.targetDir}/${name.join('/')}`, { cwd: connection.targetPath }) await ssh.execCommand(`mkdir ${connection.targetDir}/${name.join('/')}`, { cwd: connection.targetPath })
@ -150,7 +150,7 @@ function deployToSsh(connection){
let err let err
console.log(`[ Upload to ${connection.targetPath}/${connection.targetDir} ... ]`) console.log(`[ Upload to ${connection.targetPath}/${connection.targetDir} ... ]`)
await ssh.putDirectory(path.join('./', wo.envi.deploy.fromPath), `${connection.targetPath}/${connection.targetDir}`, { await ssh.putDirectory(path.join('./', wo.envar.deploy.fromPath), `${connection.targetPath}/${connection.targetDir}`, {
concurrency: 10, concurrency: 10,
recursive: true, recursive: true,
validate: itemPath => { validate: itemPath => {
@ -164,10 +164,10 @@ function deployToSsh(connection){
}) })
ssh.dispose() ssh.dispose()
if (err) { if (err) {
console.error(`🤷‍♀️🤷‍♀️🤷‍♀️ Failed deploy ${wo.envi.deploy.fromPath} to ${connection.targetPath}/${connection.targetDir} 🤷‍♀️🤷‍♀️🤷‍♀️`) console.error(`🤷‍♀️🤷‍♀️🤷‍♀️ Failed deploy ${wo.envar.deploy.fromPath} to ${connection.targetPath}/${connection.targetDir} 🤷‍♀️🤷‍♀️🤷‍♀️`)
process.exit(1) process.exit(1)
} else { } else {
console.info(`😊😊😊 Successfully deployed [${wo.envi.deploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 😊😊😊`) console.info(`😊😊😊 Successfully deployed [${wo.envar.deploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 😊😊😊`)
if (connection.url){ if (connection.url){
console.info(`😊😊😊 ${connection.url} 😊😊😊`) console.info(`😊😊😊 ${connection.url} 😊😊😊`)
} }
@ -176,7 +176,7 @@ function deployToSsh(connection){
}).catch(err => { }).catch(err => {
console.error(err) console.error(err)
ssh.dispose() ssh.dispose()
console.error(`🤷‍♀️🤷‍♀️🤷‍♀️ Failed deploy [${wo.envi.deploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 🤷‍♀️🤷‍♀️🤷‍♀️`) console.error(`🤷‍♀️🤷‍♀️🤷‍♀️ Failed deploy [${wo.envar.deploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 🤷‍♀️🤷‍♀️🤷‍♀️`)
process.exit(1) process.exit(1)
}) })
} }
@ -244,7 +244,7 @@ function deployToGit(connection){
function exec() { function exec() {
const targetDir = '' const targetDir = ''
const deployDir = pathFn.join(targetDir, '.deploy_git') const deployDir = pathFn.join(targetDir, '.deploy_git')
const fromDir = wo.envi.deploy.fromPath const fromDir = wo.envar.deploy.fromPath
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
@ -275,8 +275,8 @@ function deployToGit(connection){
} }
function setup() { function setup() {
const userName = wo.envi.deploy.gitname || '' const userName = wo.envar.deploy.gitname || ''
const userEmail = wo.envi.deploy.gitemail || '' const userEmail = wo.envar.deploy.gitemail || ''
// Create a placeholder for the first commit // Create a placeholder for the first commit
return fs.writeFile(pathFn.join(deployDir, 'placeholder'), '').then(() => { return fs.writeFile(pathFn.join(deployDir, 'placeholder'), '').then(() => {
@ -300,12 +300,12 @@ function deployToGit(connection){
}).then(() => { }).then(() => {
return git('push', '-u', repo.url, 'HEAD:' + repo.branch, '--force') return git('push', '-u', repo.url, 'HEAD:' + repo.branch, '--force')
}).then(()=>{ }).then(()=>{
console.info(`😊😊😊 Successfully deployed [${wo.envi.deploy.fromPath}] to [${connection.repo}#${connection.branch}] 😊😊😊`) console.info(`😊😊😊 Successfully deployed [${wo.envar.deploy.fromPath}] to [${connection.repo}#${connection.branch}] 😊😊😊`)
if (connection.url){ if (connection.url){
console.info(`😊😊😊 ${connection.url} 😊😊😊`) console.info(`😊😊😊 ${connection.url} 😊😊😊`)
} }
}).catch((err)=>{ }).catch((err)=>{
console.error(`🤷‍♀️🤷‍♀️🤷‍♀️ Failed deploy [${wo.envi.deploy.fromPath}] to [${connection.repo}#${connection.branch}] 🤷‍♀️🤷‍♀️🤷‍♀️`) console.error(`🤷‍♀️🤷‍♀️🤷‍♀️ Failed deploy [${wo.envar.deploy.fromPath}] to [${connection.repo}#${connection.branch}] 🤷‍♀️🤷‍♀️🤷‍♀️`)
process.exit(1) process.exit(1)
}) })
} }