rename envi to envar
This commit is contained in:
parent
efc97a3aad
commit
85e6a67b04
58
deploy.js
58
deploy.js
@ -7,7 +7,7 @@ const deepmerge = require('deepmerge')
|
||||
|
||||
// 默认参数
|
||||
const wo = (global.wo = {
|
||||
envi: {
|
||||
envar: {
|
||||
deploy: {
|
||||
fromPath: './webroot',
|
||||
gotoTarget: 'github',
|
||||
@ -40,11 +40,11 @@ const wo = (global.wo = {
|
||||
try {
|
||||
let configFile
|
||||
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`)
|
||||
}
|
||||
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`)
|
||||
}
|
||||
} 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 "".`)
|
||||
.parse(process.argv)
|
||||
|
||||
wo.envi.deploy.fromPath = commander.fromPath || wo.envi.deploy.fromPath
|
||||
wo.envi.deploy.connection = wo.envi.deploy[commander.gotoTarget || wo.envi.deploy.gotoTarget] // 使用用户指定的连接
|
||||
wo.envar.deploy.fromPath = commander.fromPath || wo.envar.deploy.fromPath
|
||||
wo.envar.deploy.connection = wo.envar.deploy[commander.gotoTarget || wo.envar.deploy.gotoTarget] // 使用用户指定的连接
|
||||
|
||||
// 可以用命令行参数覆盖掉配置文件
|
||||
const connection = {
|
||||
targetType: commander.targetType || wo.envi.deploy.connection.targetType,
|
||||
targetType: commander.targetType || wo.envar.deploy.connection.targetType,
|
||||
|
||||
// for ssh
|
||||
host: commander.host || wo.envi.deploy.connection.host,
|
||||
port: commander.port || wo.envi.deploy.connection.port,
|
||||
targetPath: commander.targetPath || wo.envi.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
|
||||
targetDir: commander.targetDir || wo.envi.deploy.connection.targetDir, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
||||
host: commander.host || wo.envar.deploy.connection.host,
|
||||
port: commander.port || wo.envar.deploy.connection.port,
|
||||
targetPath: commander.targetPath || wo.envar.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
|
||||
targetDir: commander.targetDir || wo.envar.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,
|
||||
repo: commander.repo || wo.envar.deploy.connection.repo,
|
||||
branch: commander.branch || wo.envar.deploy.connection.branch,
|
||||
gitname: commander.gitname || wo.envar.deploy.connection.gitname,
|
||||
gitemail: commander.gitemail || wo.envar.deploy.connection.gitemail,
|
||||
// common
|
||||
username: commander.user || wo.envi.deploy.connection.user,
|
||||
privateKey: fs.existsSync(commander.key || wo.envi.deploy.connection.key) ? (commander.key || wo.envi.deploy.connection.key) : undefined,
|
||||
password: commander.password || wo.envi.deploy.connection.password,
|
||||
username: commander.user || wo.envar.deploy.connection.user,
|
||||
privateKey: fs.existsSync(commander.key || wo.envar.deploy.connection.key) ? (commander.key || wo.envar.deploy.connection.key) : undefined,
|
||||
password: commander.password || wo.envar.deploy.connection.password,
|
||||
tryKeyboard: true,
|
||||
onKeyboardInteractive: (name, instructions, lang, prompts, finish) => { // 不起作用
|
||||
if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('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') {
|
||||
deployToSsh(connection)
|
||||
@ -142,7 +142,7 @@ function deployToSsh(connection){
|
||||
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))
|
||||
const toCreate = necessaryPath(path.join('./', wo.envar.deploy.fromPath))
|
||||
for (const name of toCreate) {
|
||||
console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`)
|
||||
await ssh.execCommand(`mkdir ${connection.targetDir}/${name.join('/')}`, { cwd: connection.targetPath })
|
||||
@ -150,7 +150,7 @@ function deployToSsh(connection){
|
||||
|
||||
let err
|
||||
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,
|
||||
recursive: true,
|
||||
validate: itemPath => {
|
||||
@ -164,10 +164,10 @@ function deployToSsh(connection){
|
||||
})
|
||||
ssh.dispose()
|
||||
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)
|
||||
} 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){
|
||||
console.info(`😊😊😊 ${connection.url} 😊😊😊`)
|
||||
}
|
||||
@ -176,7 +176,7 @@ function deployToSsh(connection){
|
||||
}).catch(err => {
|
||||
console.error(err)
|
||||
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)
|
||||
})
|
||||
}
|
||||
@ -244,7 +244,7 @@ function deployToGit(connection){
|
||||
function exec() {
|
||||
const targetDir = ''
|
||||
const deployDir = pathFn.join(targetDir, '.deploy_git')
|
||||
const fromDir = wo.envi.deploy.fromPath
|
||||
const fromDir = wo.envar.deploy.fromPath
|
||||
let extendDirs = connection.extend_dirs
|
||||
const ignoreHidden = connection.ignore_hidden
|
||||
const ignorePattern = connection.ignore_pattern
|
||||
@ -275,8 +275,8 @@ function deployToGit(connection){
|
||||
}
|
||||
|
||||
function setup() {
|
||||
const userName = wo.envi.deploy.gitname || ''
|
||||
const userEmail = wo.envi.deploy.gitemail || ''
|
||||
const userName = wo.envar.deploy.gitname || ''
|
||||
const userEmail = wo.envar.deploy.gitemail || ''
|
||||
|
||||
// Create a placeholder for the first commit
|
||||
return fs.writeFile(pathFn.join(deployDir, 'placeholder'), '').then(() => {
|
||||
@ -300,12 +300,12 @@ function deployToGit(connection){
|
||||
}).then(() => {
|
||||
return git('push', '-u', repo.url, 'HEAD:' + repo.branch, '--force')
|
||||
}).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){
|
||||
console.info(`😊😊😊 ${connection.url} 😊😊😊`)
|
||||
}
|
||||
}).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)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user