u
This commit is contained in:
parent
35b4fa8da9
commit
0e62d06af9
76
deploy.js
76
deploy.js
@ -6,7 +6,8 @@ const commander = require('commander')
|
||||
const deepmerge = require('deepmerge')
|
||||
|
||||
// 默认参数
|
||||
var Config = {
|
||||
const wo = { global.wo = {
|
||||
envi: {
|
||||
deploy: {
|
||||
from: './dist',
|
||||
toTarget: 'github',
|
||||
@ -33,61 +34,62 @@ var Config = {
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 读取配置文件
|
||||
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 <from>', `local path to copy from. Default to ${Config.deploy.from}`)
|
||||
.option('-t, --toTarget <toTarget>', `connection section in config. Default to ${Config.deploy.toTarget}`)
|
||||
.option('-f, --from <from>', `local path to copy from. Default to ${wo.envi.deploy.from}`)
|
||||
.option('-t, --toTarget <toTarget>', `connection section in config. Default to ${wo.envi.deploy.toTarget}`)
|
||||
|
||||
.option('-s, --server <server>', `server type, git or ssh. Default to ${Config.deploy.connection.server}`)
|
||||
.option('-s, --server <server>', `server type, git or ssh. Default to ${wo.envi.deploy.connection.server}`)
|
||||
|
||||
.option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${Config.deploy.connection.host}`)
|
||||
.option('-P, --port <port>', `Ssh port number of the target server. Default to ${Config.deploy.connection.port}`)
|
||||
.option('-D, --dir <dir>', `Destination path to deploy on the target server. Default to ${Config.deploy.connection.dir}`)
|
||||
.option('-d, --dist <dist>', `Destination folder to deploy on the target server. Default to ${Config.deploy.connection.dist}`)
|
||||
.option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${wo.envi.deploy.connection.host}`)
|
||||
.option('-P, --port <port>', `Ssh port number of the target server. Default to ${wo.envi.deploy.connection.port}`)
|
||||
.option('-D, --dir <dir>', `Destination path to deploy on the target server. Default to ${wo.envi.deploy.connection.dir}`)
|
||||
.option('-d, --dist <dist>', `Destination folder to deploy on the target server. Default to ${wo.envi.deploy.connection.dist}`)
|
||||
|
||||
.option('-r, --repo <repo>', `git repo address. Default to ${Config.deploy.connection.repo}`)
|
||||
.option('-b, --branch <branch>', `git repo branch. Default to ${Config.deploy.connection.branch}`)
|
||||
.option('-n, --gitname <gitname>', `git user name. Default to ${Config.deploy.connection.gitname}`)
|
||||
.option('-e, --gitemail <gitemail>', `git user email. Default to ${Config.deploy.connection.gitemail}`)
|
||||
.option('-r, --repo <repo>', `git repo address. Default to ${wo.envi.deploy.connection.repo}`)
|
||||
.option('-b, --branch <branch>', `git repo branch. Default to ${wo.envi.deploy.connection.branch}`)
|
||||
.option('-n, --gitname <gitname>', `git user name. Default to ${wo.envi.deploy.connection.gitname}`)
|
||||
.option('-e, --gitemail <gitemail>', `git user email. Default to ${wo.envi.deploy.connection.gitemail}`)
|
||||
|
||||
.option('-u, --user <user>', `User id to login the target server. Default to ${Config.deploy.connection.user}`)
|
||||
.option('-k, --key <key>', `User private key file to login the target server. Default to ${Config.deploy.connection.key}`)
|
||||
.option('-p, --password <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>', `User id to login the target server. Default to ${wo.envi.deploy.connection.user}`)
|
||||
.option('-k, --key <key>', `User private key file to login the target server. Default to ${wo.envi.deploy.connection.key}`)
|
||||
.option('-p, --password <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(() => {
|
||||
|
Loading…
Reference in New Issue
Block a user