修正了不在root下建立备份的错误
This commit is contained in:
74
deploy.js
74
deploy.js
@@ -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 () => {
|
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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}`, {
|
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)
|
||||||
});
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user