77 lines
2.1 KiB
JavaScript
77 lines
2.1 KiB
JavaScript
// cli.js,创建于根目录,与命令配置的./cli.js保持一致
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const { NodeSSH } = require('node-ssh');
|
|
|
|
sshPublish();
|
|
async function sshPublish() {
|
|
let privateKeyPath = '';
|
|
|
|
const publishDir = '/www/wwwroot/ldhb-dist/dist';
|
|
let remoteConfig = undefined;
|
|
|
|
privateKeyPath = '';
|
|
remoteConfig = {
|
|
host: '101.46.52.67',
|
|
username: 'root',
|
|
password: 'Jsld.0813',
|
|
port: 22,
|
|
readyTimeout: 5000000,
|
|
};
|
|
// 初始化ssh
|
|
const ssh = new NodeSSH();
|
|
// 连接 服务器
|
|
|
|
await connect(ssh, remoteConfig, privateKeyPath);
|
|
console.log('连接成功');
|
|
console.log('开始文件上传!');
|
|
// 上传目录到服务器
|
|
await uploadDir(ssh, '/dist', publishDir);
|
|
console.log('文件上传成功!');
|
|
// 执行exit命令
|
|
|
|
const { stdout, stderr, code, signal } = await ssh.execCommand('exit');
|
|
ssh.dispose();
|
|
}
|
|
|
|
function connect(ssh, remoteConfig, privateKeyPath) {
|
|
return ssh.connect({
|
|
host: remoteConfig.host,
|
|
username: remoteConfig.username,
|
|
password: remoteConfig.password,
|
|
port: remoteConfig.port,
|
|
tryKeyboard: true,
|
|
// privateKey: fs.readFileSync(path.join(__dirname, privateKeyPath)).toString(),
|
|
// privateKey: path.join(__dirname, privateKeyPath),
|
|
onKeyboardInteractive: (
|
|
name,
|
|
instructions,
|
|
instructionsLang,
|
|
prompts,
|
|
finish
|
|
) => {},
|
|
});
|
|
}
|
|
function uploadDir(ssh, localDir, publishDir) {
|
|
return ssh.putDirectory(path.join(__dirname, localDir), publishDir, {
|
|
recursive: true,
|
|
concurrency: 10,
|
|
// ^ WARNING: Not all servers support high concurrency
|
|
// try a bunch of values and see what works on your server
|
|
validate: function (itemPath) {
|
|
const baseName = path.basename(itemPath);
|
|
return (
|
|
baseName.substr(0, 1) !== '.' && // do not allow dot files
|
|
baseName !== 'node_modules'
|
|
); // do not allow node_modules
|
|
},
|
|
tick: function (localPath, remotePath, error) {
|
|
if (error) {
|
|
console.log(localPath, remotePath, error || '');
|
|
} else {
|
|
console.log(localPath + ':成功上传');
|
|
}
|
|
},
|
|
});
|
|
}
|