修正了不在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 path = require('path');
const NodeSsh = require('node-ssh');
const ssh = new NodeSsh();
const fs = require('fs')
const path = require('path')
const ssh = new (require('node-ssh'))()
const commander = require('commander')
commander
@@ -17,6 +16,8 @@ commander
const root=commander.root // 本地的项目目录。似乎该目录必须已经存在于服务器上
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 connection = {
@@ -25,38 +26,49 @@ const connection = {
username: commander.user||'tic',
privateKey: fs.existsSync(privateKeyFile)?privateKeyFile:undefined,
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) {
const dirs = [path];
const dirs = [path]
if (fs.statSync(path).isFile()) {
return dirs;
return dirs
}
fs.readdirSync(path).forEach(item => {
const stat = fs.statSync(`${path}/${item}`);
const stat = fs.statSync(`${path}/${item}`)
if (stat.isDirectory()) {
dirs.push(...subDirs(`${path}/${item}`))
}
});
return dirs;
})
return dirs
}
const necessaryPath = (path) => {
return subDirs(path)
.map(it => it.replace(path, ''))
.filter(it => it)
.map(it => it.split('/').filter(it => it));
};
ssh.connect(connection).then(async () => {
await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { root });
await ssh.execCommand(`mkdir ${dist}`, { root });
const toCreate = necessaryPath('./dist');
for (const name of toCreate) {
await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { root });
.map(it => it.split('/').filter(it => it))
}
let err;
ssh.connect(connection).then(async () => {
console.log(`[ mv ${dist} ${dist}-backup-${new Date().toISOString()} ... ]`)
await ssh.execCommand(`mv ${dist} ${dist}-backup-${new Date().toISOString()}`, { cwd:root })
console.log(`[ mkdir ${dist} ... ]`)
await ssh.execCommand(`mkdir ${dist}`, { cwd:root })
const toCreate = necessaryPath('./dist')
for (const name of toCreate) {
console.log(`[ mkdir ${dist}/${name.join('/')} ... ]`)
await ssh.execCommand(`mkdir ${dist}/${name.join('/')}`, { cwd:root })
}
let err
console.log(`[ Upload to ${root}/${dist} ... ]`)
await ssh.putDirectory('./dist', `${root}/${dist}`, {
concurrency: 10,
recursive: true,
@@ -65,19 +77,19 @@ ssh.connect(connection).then(async () => {
return !baseName.endsWith('.map');
},
tick: (localPath, remotePath, error) => {
console.log(`file ${localPath} to ${remotePath} ${error || 'uploaded'}`);
err = error;
console.log(`Uploading "${localPath}" ===> "${remotePath}" ${error || 'succeeded!'}`)
err = error
},
});
ssh.dispose();
})
ssh.dispose()
if (err) {
console.log('uploaded with error!');
process.exit(1);
console.log('[ Uploaded with error! ]')
process.exit(1)
} else {
console.log('build published!');
console.log('[ Uploaded successfully! ]')
}
}).catch(err => {
console.error(err);
ssh.dispose();
process.exit(1);
});
console.error(err)
ssh.dispose()
process.exit(1)
})