u
This commit is contained in:
parent
35b4fa8da9
commit
0e62d06af9
122
deploy.js
122
deploy.js
@ -6,88 +6,90 @@ const commander = require('commander')
|
|||||||
const deepmerge = require('deepmerge')
|
const deepmerge = require('deepmerge')
|
||||||
|
|
||||||
// 默认参数
|
// 默认参数
|
||||||
var Config = {
|
const wo = { global.wo = {
|
||||||
deploy: {
|
envi: {
|
||||||
from: './dist',
|
deploy: {
|
||||||
toTarget: 'github',
|
from: './dist',
|
||||||
|
toTarget: 'github',
|
||||||
|
|
||||||
vultr: {
|
vultr: {
|
||||||
server: 'ssh',
|
server: 'ssh',
|
||||||
host: undefined,
|
host: undefined,
|
||||||
port: 22,
|
port: 22,
|
||||||
dir: undefined,
|
dir: undefined,
|
||||||
dist: 'dist',
|
dist: 'dist',
|
||||||
user: undefined,
|
user: undefined,
|
||||||
password: undefined,
|
password: undefined,
|
||||||
key: `${process.env.HOME}/.ssh/id_rsa`,
|
key: `${process.env.HOME}/.ssh/id_rsa`,
|
||||||
},
|
},
|
||||||
github: {
|
github: {
|
||||||
server: 'git',
|
server: 'git',
|
||||||
repo: undefined,
|
repo: undefined,
|
||||||
branch: 'master',
|
branch: 'master',
|
||||||
gitname: undefined,
|
gitname: undefined,
|
||||||
gitemail: undefined,
|
gitemail: undefined,
|
||||||
user: undefined,
|
user: undefined,
|
||||||
password: undefined,
|
password: undefined,
|
||||||
key: `${process.env.HOME}/.ssh/id_rsa`,
|
key: `${process.env.HOME}/.ssh/id_rsa`,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
// 读取配置文件
|
// 读取配置文件
|
||||||
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'))) {
|
||||||
Config = deepmerge(Config, require(configFile))
|
wo.envi = deepmerge(wo.envi, require(configFile))
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Loading config files failed: ' + err.message)
|
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
|
commander
|
||||||
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||||
.option('-f, --from <from>', `local path to copy from. Default to ${Config.deploy.from}`)
|
.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 ${Config.deploy.toTarget}`)
|
.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('-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 ${Config.deploy.connection.port}`)
|
.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 ${Config.deploy.connection.dir}`)
|
.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 ${Config.deploy.connection.dist}`)
|
.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('-r, --repo <repo>', `git repo address. Default to ${wo.envi.deploy.connection.repo}`)
|
||||||
.option('-b, --branch <branch>', `git repo branch. Default to ${Config.deploy.connection.branch}`)
|
.option('-b, --branch <branch>', `git repo branch. Default to ${wo.envi.deploy.connection.branch}`)
|
||||||
.option('-n, --gitname <gitname>', `git user name. Default to ${Config.deploy.connection.gitname}`)
|
.option('-n, --gitname <gitname>', `git user name. Default to ${wo.envi.deploy.connection.gitname}`)
|
||||||
.option('-e, --gitemail <gitemail>', `git user email. Default to ${Config.deploy.connection.gitemail}`)
|
.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('-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 ${Config.deploy.connection.key}`)
|
.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 "${Config.deploy.connection.password}"`)
|
.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)
|
.parse(process.argv)
|
||||||
|
|
||||||
Config.deploy.from = commander.from || Config.deploy.from
|
wo.envi.deploy.from = commander.from || wo.envi.deploy.from
|
||||||
Config.deploy.connection = Config.deploy[commander.toTarget || Config.deploy.toTarget] // 使用用户指定的连接
|
wo.envi.deploy.connection = wo.envi.deploy[commander.toTarget || wo.envi.deploy.toTarget] // 使用用户指定的连接
|
||||||
|
|
||||||
const connection = {
|
const connection = {
|
||||||
server: commander.server || Config.deploy.connection.server,
|
server: commander.server || wo.envi.deploy.connection.server,
|
||||||
|
|
||||||
host: commander.host || Config.deploy.connection.host,
|
host: commander.host || wo.envi.deploy.connection.host,
|
||||||
port: commander.port || Config.deploy.connection.port,
|
port: commander.port || wo.envi.deploy.connection.port,
|
||||||
dir: commander.dir || Config.deploy.connection.dir, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
|
dir: commander.dir || wo.envi.deploy.connection.dir, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
|
||||||
dist: commander.dist || Config.deploy.connection.dist, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
dist: commander.dist || wo.envi.deploy.connection.dist, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
||||||
|
|
||||||
repo: commander.repo || Config.deploy.connection.repo,
|
repo: commander.repo || wo.envi.deploy.connection.repo,
|
||||||
branch: commander.branch || Config.deploy.connection.branch,
|
branch: commander.branch || wo.envi.deploy.connection.branch,
|
||||||
gitname: commander.gitname || Config.deploy.connection.gitname,
|
gitname: commander.gitname || wo.envi.deploy.connection.gitname,
|
||||||
gitemail: commander.gitemail || Config.deploy.connection.gitemail,
|
gitemail: commander.gitemail || wo.envi.deploy.connection.gitemail,
|
||||||
|
|
||||||
username: commander.user || Config.deploy.connection.user,
|
username: commander.user || wo.envi.deploy.connection.user,
|
||||||
privateKey: fs.existsSync(commander.key || Config.deploy.key) ? (commander.key || Config.deploy.key) : undefined,
|
privateKey: fs.existsSync(commander.key || wo.envi.deploy.key) ? (commander.key || wo.envi.deploy.key) : undefined,
|
||||||
password: commander.password || Config.deploy.connection.password,
|
password: commander.password || wo.envi.deploy.connection.password,
|
||||||
tryKeyboard: true,
|
tryKeyboard: true,
|
||||||
onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => { // 不起作用
|
onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => { // 不起作用
|
||||||
if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
|
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') {
|
if (connection.server==='ssh') {
|
||||||
deployToSsh(connection)
|
deployToSsh(connection)
|
||||||
@ -134,7 +136,7 @@ function deployToSsh(connection){
|
|||||||
await ssh.execCommand(`mv ${connection.dist} ${connection.dist}-backup-${new Date().toISOString()}`, { cwd: connection.dir })
|
await ssh.execCommand(`mv ${connection.dist} ${connection.dist}-backup-${new Date().toISOString()}`, { cwd: connection.dir })
|
||||||
console.log(`[ mkdir ${connection.dist} ... ]`)
|
console.log(`[ mkdir ${connection.dist} ... ]`)
|
||||||
await ssh.execCommand(`mkdir ${connection.dist}`, { cwd: connection.dir })
|
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) {
|
for (const name of toCreate) {
|
||||||
console.log(`[ mkdir ${connection.dist}/${name.join('/')} ... ]`)
|
console.log(`[ mkdir ${connection.dist}/${name.join('/')} ... ]`)
|
||||||
await ssh.execCommand(`mkdir ${connection.dist}/${name.join('/')}`, { cwd: connection.dir })
|
await ssh.execCommand(`mkdir ${connection.dist}/${name.join('/')}`, { cwd: connection.dir })
|
||||||
@ -142,7 +144,7 @@ function deployToSsh(connection){
|
|||||||
|
|
||||||
let err
|
let err
|
||||||
console.log(`[ Upload to ${connection.dir}/${connection.dist} ... ]`)
|
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,
|
concurrency: 10,
|
||||||
recursive: true,
|
recursive: true,
|
||||||
validate: itemPath => {
|
validate: itemPath => {
|
||||||
@ -232,7 +234,7 @@ function deployToGit(connection){
|
|||||||
function exec() {
|
function exec() {
|
||||||
const baseDir = ''
|
const baseDir = ''
|
||||||
const deployDir = pathFn.join(baseDir, '.deploy_git')
|
const deployDir = pathFn.join(baseDir, '.deploy_git')
|
||||||
const publicDir = Config.deploy.from
|
const publicDir = wo.envi.deploy.from
|
||||||
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
|
||||||
@ -263,8 +265,8 @@ function deployToGit(connection){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
const userName = Config.deploy.gitname || ''
|
const userName = wo.envi.deploy.gitname || ''
|
||||||
const userEmail = Config.deploy.gitemail || ''
|
const userEmail = wo.envi.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(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user