water-ldht/built.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-09-26 14:04:18 +08:00
// cli.js,创建于根目录,与命令配置的./cli.js保持一致
2024-11-01 16:58:10 +08:00
const path = require('path');
2024-09-26 14:04:18 +08:00
const fs = require('fs');
const { NodeSSH } = require('node-ssh');
sshPublish();
async function sshPublish() {
let privateKeyPath = '';
2024-11-01 16:58:10 +08:00
const publishDir = '/www/wwwroot/ldhb-dist/dist';
2024-09-26 14:04:18 +08:00
let remoteConfig = undefined;
privateKeyPath = '';
remoteConfig = {
2024-11-01 16:58:10 +08:00
host: '101.46.52.67',
2024-09-26 14:04:18 +08:00
username: 'root',
2024-11-01 16:58:10 +08:00
password: 'Jsld.0813',
2024-09-26 14:04:18 +08:00
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),
2024-11-01 16:58:10 +08:00
onKeyboardInteractive: (
name,
instructions,
instructionsLang,
prompts,
finish
) => {},
2024-09-26 14:04:18 +08:00
});
}
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) {
2024-11-01 16:58:10 +08:00
const baseName = path.basename(itemPath);
return (
baseName.substr(0, 1) !== '.' && // do not allow dot files
baseName !== 'node_modules'
); // do not allow node_modules
2024-09-26 14:04:18 +08:00
},
tick: function (localPath, remotePath, error) {
2024-11-01 16:58:10 +08:00
if (error) {
2024-09-26 14:04:18 +08:00
console.log(localPath, remotePath, error || '');
2024-11-01 16:58:10 +08:00
} else {
2024-09-26 14:04:18 +08:00
console.log(localPath + ':成功上传');
}
2024-11-01 16:58:10 +08:00
},
2024-09-26 14:04:18 +08:00
});
}