159 lines
4.2 KiB
JavaScript
159 lines
4.2 KiB
JavaScript
const fs = require('fs');
|
|
const { execSync } = require('child_process');
|
|
const { Client } = require('ssh2');
|
|
|
|
// 配置SSH连接信息 - 实际使用中应该从环境变量或配置文件中获取
|
|
const SSH_CONFIG = {
|
|
host: 'baishe.fullstack.club',
|
|
port: 12322,
|
|
username: 'root',
|
|
password: '88888888',
|
|
remotePath: '/app/test/test-log4j/jars' // 远程服务器目标目录
|
|
};
|
|
|
|
function executeCommand(command, options = {}) {
|
|
try {
|
|
const output = execSync(command, { stdio: 'inherit', ...options });
|
|
return output;
|
|
} catch (error) {
|
|
console.error(`执行命令失败: ${command}`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function findJarFile(targetDir) {
|
|
const files = fs.readdirSync(targetDir);
|
|
const jarFile = files.find(file => file.endsWith('.jar') && !file.includes('original'));
|
|
|
|
if (!jarFile) {
|
|
console.error('未找到生成的jar文件');
|
|
process.exit(1);
|
|
}
|
|
|
|
return `${targetDir}/${jarFile}`;
|
|
}
|
|
|
|
function copyFile(source, destination) {
|
|
try {
|
|
fs.copyFileSync(source, destination);
|
|
console.log(`已复制: ${source} -> ${destination}`);
|
|
} catch (error) {
|
|
console.error(`复制文件失败: ${source} -> ${destination}`, error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function uploadToServer(localDir, remoteDir) {
|
|
const conn = new Client();
|
|
|
|
console.log(`正在连接到SSH服务器: ${SSH_CONFIG.host}`);
|
|
|
|
conn.on('ready', () => {
|
|
console.log('SSH连接已建立');
|
|
|
|
const sftp = conn.sftp((err, sftp) => {
|
|
if (err) throw err;
|
|
|
|
// 确保远程目录存在
|
|
sftp.mkdir(remoteDir, { recursive: true }, (err) => {
|
|
if (err && err.code !== 4) { // 忽略目录已存在的错误
|
|
console.error('创建远程目录失败:', err);
|
|
conn.end();
|
|
process.exit(1);
|
|
}
|
|
|
|
// 读取本地目录中的所有文件
|
|
const files = fs.readdirSync(localDir);
|
|
let uploadedCount = 0;
|
|
|
|
if (files.length === 0) {
|
|
console.log('没有文件需要上传');
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
files.forEach(file => {
|
|
const localPath = `${localDir}/${file}`;
|
|
const remotePath = `${remoteDir}/${file}`;
|
|
|
|
sftp.fastPut(localPath, remotePath, {}, (err) => {
|
|
if (err) {
|
|
console.error(`上传文件失败: ${file}`, err);
|
|
conn.end();
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`已上传: ${file}`);
|
|
uploadedCount++;
|
|
|
|
if (uploadedCount === files.length) {
|
|
console.log(`所有文件上传完成,共 ${uploadedCount} 个文件`);
|
|
conn.end();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err);
|
|
process.exit(1);
|
|
});
|
|
|
|
conn.on('end', () => {
|
|
console.log('SSH连接已关闭');
|
|
});
|
|
|
|
conn.connect(SSH_CONFIG);
|
|
}
|
|
|
|
function main() {
|
|
const args = process.argv.slice(2);
|
|
const command = args[0];
|
|
|
|
if (!command) {
|
|
console.log('请提供命令 (sync-test 或 release)');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (command === 'sync-test') {
|
|
const includeDependencies = args.includes('-f');
|
|
const jarsDir = 'test-log4j/jars';
|
|
|
|
// 1. 执行mvn编译打包
|
|
console.log('正在执行mvn编译打包...');
|
|
executeCommand('mvn clean package');
|
|
|
|
// 2. 找到并复制jar文件
|
|
const targetDir = 'target';
|
|
const jarFile = findJarFile(targetDir);
|
|
|
|
// 确保目标目录存在
|
|
if (!fs.existsSync(jarsDir)) {
|
|
fs.mkdirSync(jarsDir, { recursive: true });
|
|
}
|
|
|
|
const destJar = `${jarsDir}/${jarFile.split('/').pop()}`;
|
|
copyFile(jarFile, destJar);
|
|
|
|
// 3. 如果需要,导出依赖
|
|
if (includeDependencies) {
|
|
console.log('正在导出测试依赖...');
|
|
executeCommand('mvn dependency:copy-dependencies -DoutputDirectory=test-log4j/jars -DincludeScope=test');
|
|
}
|
|
|
|
// 4. 上传到服务器
|
|
console.log('准备上传文件到服务器...');
|
|
uploadToServer(jarsDir, SSH_CONFIG.remotePath);
|
|
|
|
} else if (command === 'release') {
|
|
console.log('release命令尚未实现');
|
|
// 在这里实现release命令的逻辑
|
|
} else {
|
|
console.log(`未知命令: ${command}`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main(); |