修正了不在root下建立备份的错误

This commit is contained in:
luk.lu
2018-11-26 14:40:29 +08:00
parent b0fd6092ee
commit d495ab27d5

View File

@@ -1,7 +1,6 @@
const fs = require('fs'); const fs = require('fs')
const path = require('path'); const path = require('path')
const NodeSsh = require('node-ssh'); const ssh = new (require('node-ssh'))()
const ssh = new NodeSsh();
const commander = require('commander') const commander = require('commander')
commander commander
@@ -17,6 +16,8 @@ commander
const root=commander.root // 本地的项目目录。似乎该目录必须已经存在于服务器上 const root=commander.root // 本地的项目目录。似乎该目录必须已经存在于服务器上
const dist=commander.dist||'dist' // 新系统将发布在这个目录里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。 const dist=commander.dist||'dist' // 新系统将发布在这个目录里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。
console.log(` root = ${root} `)
console.log(` dist = ${dist} `)
const privateKeyFile=commander.key||`${process.env.HOME}/.ssh/id_rsa` const privateKeyFile=commander.key||`${process.env.HOME}/.ssh/id_rsa`
const connection = { const connection = {
@@ -25,38 +26,49 @@ const connection = {
username: commander.user||'tic', username: commander.user||'tic',
privateKey: fs.existsSync(privateKeyFile)?privateKeyFile:undefined, privateKey: fs.existsSync(privateKeyFile)?privateKeyFile:undefined,
password: commander.password, password: commander.password,
}; tryKeyboard: true,
onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => { // 不起作用
if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
finish([password])
}
},
}
console.log(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
} }
fs.readdirSync(path).forEach(item => { fs.readdirSync(path).forEach(item => {
const stat = fs.statSync(`${path}/${item}`); const stat = fs.statSync(`${path}/${item}`)
if (stat.isDirectory()) { if (stat.isDirectory()) {
dirs.push(...subDirs(`${path}/${item}`)) dirs.push(...subDirs(`${path}/${item}`))
} }
}); })
return dirs; return dirs
} }
const necessaryPath = (path) => { const necessaryPath = (path) => {
return subDirs(path) return subDirs(path)
.map(it => it.replace(path, '')) .map(it => it.replace(path, ''))
.filter(it => it) .filter(it => it)
.map(it => it.split('/').filter(it => it)); .map(it => it.split('/').filter(it => it))
}; }
ssh.connect(connection).then(async () => { ssh.connect(connection).then(async () => {
await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { root }); console.log(`[ mv ${dist} ${dist}-backup-${new Date().toISOString()} ... ]`)
await ssh.execCommand(`mkdir ${dist}`, { root }); await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { cwd:root })
const toCreate = necessaryPath('./dist'); console.log(`[ mkdir ${dist} ... ]`)
await ssh.execCommand(`mkdir ${dist}`, { cwd:root })
const toCreate = necessaryPath('./dist')
for (const name of toCreate) { for (const name of toCreate) {
await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { root }); console.log(`[ mkdir ${dist}/${name.join('/')} ... ]`)
await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { cwd:root })
} }
let err; let err
console.log(`[ Upload to ${root}/${dist} ... ]`)
await ssh.putDirectory('./dist', `${root}/${dist}`, { await ssh.putDirectory('./dist', `${root}/${dist}`, {
concurrency: 10, concurrency: 10,
recursive: true, recursive: true,
@@ -65,19 +77,19 @@ ssh.connect(connection).then(async () => {
return !baseName.endsWith('.map'); return !baseName.endsWith('.map');
}, },
tick: (localPath, remotePath, error) => { tick: (localPath, remotePath, error) => {
console.log(`file ${localPath} to ${remotePath} ${error || 'uploaded'}`); console.log(`Uploading "${localPath}" ===> "${remotePath}" ${error || 'succeeded!'}`)
err = error; err = error
}, },
}); })
ssh.dispose(); ssh.dispose()
if (err) { if (err) {
console.log('uploaded with error!'); console.log('[ Uploaded with error! ]')
process.exit(1); process.exit(1)
} else { } else {
console.log('build published!'); console.log('[ Uploaded successfully! ]')
} }
}).catch(err => { }).catch(err => {
console.error(err); console.error(err)
ssh.dispose(); ssh.dispose()
process.exit(1); process.exit(1)
}); })