[deploy.js] 修改代码格式,以符合 standard 规范
This commit is contained in:
72
deploy.js
72
deploy.js
@@ -2,65 +2,65 @@ const fs = require('fs')
|
|||||||
const path = require('path')
|
const path = require('path')
|
||||||
const ssh = new (require('node-ssh'))()
|
const ssh = new (require('node-ssh'))()
|
||||||
|
|
||||||
/********************* 读取命令行以及配置文件里的参数 **********************/
|
/** ******************* 读取命令行以及配置文件里的参数 ******************** **/
|
||||||
const commander = require('commander')
|
const commander = require('commander')
|
||||||
const deepmerge = require('deepmerge')
|
const deepmerge = require('deepmerge')
|
||||||
|
|
||||||
var Config={}
|
var Config = {}
|
||||||
|
|
||||||
// 读取配置文件
|
// 读取配置文件
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync('./ConfigBasic.js')) {
|
if (fs.existsSync('./ConfigBasic.js')) {
|
||||||
Config=require('./ConfigBasic.js')
|
Config = require('./ConfigBasic.js')
|
||||||
console.info('ConfigBasic loaded')
|
console.info('ConfigBasic loaded')
|
||||||
}
|
}
|
||||||
if (fs.existsSync('./ConfigCustom.js')) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
if (fs.existsSync('./ConfigCustom.js')) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
||||||
Config=deepmerge(Config, require('./ConfigCustom.js')) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
Config = deepmerge(Config, require('./ConfigCustom.js')) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||||
console.info('ConfigCustom loaded')
|
console.info('ConfigCustom loaded')
|
||||||
}
|
}
|
||||||
if (fs.existsSync('./ConfigSecret.js')) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
if (fs.existsSync('./ConfigSecret.js')) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
||||||
Config=deepmerge(Config, require('./ConfigSecret.js'))
|
Config = deepmerge(Config, require('./ConfigSecret.js'))
|
||||||
console.info('ConfigSecret loaded')
|
console.info('ConfigSecret loaded')
|
||||||
}
|
}
|
||||||
}catch(err){
|
} catch (err) {
|
||||||
console.error('Loading config files failed: '+err.message)
|
console.error('Loading config files failed: ' + err.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
commander
|
commander
|
||||||
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||||
.option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${Config.deploy.host}`)
|
.option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${Config.deploy.host}`)
|
||||||
.option('-P, --port <port>', `Ssh port number of the target server. Default to ${Config.deploy.port}`)
|
.option('-P, --port <port>', `Ssh port number of the target server. Default to ${Config.deploy.port}`)
|
||||||
.option('-r, --root <root>', `Path to deploy on the target server. Default to ${Config.deploy.root}`)
|
.option('-r, --root <root>', `Path to deploy on the target server. Default to ${Config.deploy.root}`)
|
||||||
.option('-d, --dist <dist>', `Folder to deploy on the target server. Default to ${Config.deploy.dist}`)
|
.option('-d, --dist <dist>', `Folder to deploy on the target server. Default to ${Config.deploy.dist}`)
|
||||||
.option('-u, --user <user>', `User id to login the target server. Default to ${Config.deploy.user}`)
|
.option('-u, --user <user>', `User id to login the target server. Default to ${Config.deploy.user}`)
|
||||||
.option('-k, --key <key>', `User private key file to login the target server. Default to ${Config.deploy.key}`)
|
.option('-k, --key <key>', `User private key file to login the target server. Default to ${Config.deploy.key}`)
|
||||||
.option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "". Default to ${Config.deploy.password}`)
|
.option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "". Default to ${Config.deploy.password}`)
|
||||||
.parse(process.argv)
|
.parse(process.argv)
|
||||||
|
|
||||||
const root=commander.root||Config.deploy.root // 本地的项目目录。似乎该目录必须已经存在于服务器上
|
const root = commander.root || Config.deploy.root // 本地的项目目录。似乎该目录必须已经存在于服务器上
|
||||||
console.log(` root = ${root} `)
|
console.log(` root = ${root} `)
|
||||||
const dist=commander.dist||Config.deploy.dist||'dist' // 新系统将发布在这个目录里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
const dist = commander.dist || Config.deploy.dist || 'dist' // 新系统将发布在这个目录里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
||||||
console.log(` dist = ${dist} `)
|
console.log(` dist = ${dist} `)
|
||||||
const privateKeyFile=commander.key||Config.deploy.key||`${process.env.HOME}/.ssh/id_rsa`
|
const privateKeyFile = commander.key || Config.deploy.key || `${process.env.HOME}/.ssh/id_rsa`
|
||||||
console.log(` privateKeyFile = ${privateKeyFile}`)
|
console.log(` privateKeyFile = ${privateKeyFile}`)
|
||||||
|
|
||||||
const connection = {
|
const connection = {
|
||||||
host: commander.host||Config.deploy.host,
|
host: commander.host || Config.deploy.host,
|
||||||
port: commander.port||Config.deploy.port||22,
|
port: commander.port || Config.deploy.port || 22,
|
||||||
username: commander.user||Config.deploy.user,
|
username: commander.user || Config.deploy.user,
|
||||||
privateKey: fs.existsSync(privateKeyFile)?privateKeyFile:undefined,
|
privateKey: fs.existsSync(privateKeyFile) ? privateKeyFile : undefined,
|
||||||
password: commander.password||Config.deploy.password,
|
password: commander.password || Config.deploy.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')) {
|
||||||
finish([password])
|
finish([password])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
console.log(` connection = ${JSON.stringify(connection)}`)
|
console.log(` connection = ${JSON.stringify(connection)}`)
|
||||||
|
|
||||||
/************************ 连接到待部署的主机,拷贝文件到指定路径 ***************/
|
/** ********************** 连接到待部署的主机,拷贝文件到指定路径 ************* **/
|
||||||
function subDirs(path) {
|
function subDirs (path) {
|
||||||
const dirs = [path]
|
const dirs = [path]
|
||||||
if (fs.statSync(path).isFile()) {
|
if (fs.statSync(path).isFile()) {
|
||||||
return dirs
|
return dirs
|
||||||
@@ -83,13 +83,13 @@ const necessaryPath = (path) => {
|
|||||||
|
|
||||||
ssh.connect(connection).then(async () => {
|
ssh.connect(connection).then(async () => {
|
||||||
console.log(`[ mv ${dist} ${dist}-backup-${new Date().toISOString()} ... ]`)
|
console.log(`[ mv ${dist} ${dist}-backup-${new Date().toISOString()} ... ]`)
|
||||||
await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { cwd:root })
|
await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { cwd: root })
|
||||||
console.log(`[ mkdir ${dist} ... ]`)
|
console.log(`[ mkdir ${dist} ... ]`)
|
||||||
await ssh.execCommand(`mkdir ${dist}`, { cwd:root })
|
await ssh.execCommand(`mkdir ${dist}`, { cwd: root })
|
||||||
const toCreate = necessaryPath('./dist')
|
const toCreate = necessaryPath('./dist')
|
||||||
for (const name of toCreate) {
|
for (const name of toCreate) {
|
||||||
console.log(`[ mkdir ${dist}/${name.join('/')} ... ]`)
|
console.log(`[ mkdir ${dist}/${name.join('/')} ... ]`)
|
||||||
await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { cwd:root })
|
await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { cwd: root })
|
||||||
}
|
}
|
||||||
|
|
||||||
let err
|
let err
|
||||||
@@ -98,8 +98,8 @@ ssh.connect(connection).then(async () => {
|
|||||||
concurrency: 10,
|
concurrency: 10,
|
||||||
recursive: true,
|
recursive: true,
|
||||||
validate: itemPath => {
|
validate: itemPath => {
|
||||||
const baseName = path.basename(itemPath);
|
const baseName = path.basename(itemPath)
|
||||||
return !baseName.endsWith('.map');
|
return !baseName.endsWith('.map')
|
||||||
},
|
},
|
||||||
tick: (localPath, remotePath, error) => {
|
tick: (localPath, remotePath, error) => {
|
||||||
console.log(`Uploading "${localPath}" ===> "${remotePath}" ${error || 'succeeded!'}`)
|
console.log(`Uploading "${localPath}" ===> "${remotePath}" ${error || 'succeeded!'}`)
|
||||||
|
|||||||
Reference in New Issue
Block a user