fix remote delete
This commit is contained in:
39
index.js
39
index.js
@ -245,7 +245,7 @@ const uploadToSFTP = async () => {
|
||||
|
||||
const sftp = new SftpClient();
|
||||
const remote = sftpConfig.remotePath || "/";
|
||||
const files = fs.readdirSync(DOWNLOAD_PATH).filter((f) => f.endsWith(".jar"));
|
||||
const localFiles = fs.readdirSync(DOWNLOAD_PATH).filter(f => f.endsWith(".jar"));
|
||||
|
||||
const connectOptions = {
|
||||
host: sftpConfig.host,
|
||||
@ -261,28 +261,35 @@ const uploadToSFTP = async () => {
|
||||
throw new Error("Missing SFTP password or private key.");
|
||||
}
|
||||
|
||||
const extractBaseName = (filename) => {
|
||||
return filename.replace(/[-_.](v?\d.*)?\.jar$/, "").trim();
|
||||
};
|
||||
|
||||
try {
|
||||
await sftp.connect(connectOptions);
|
||||
const remoteFiles = await sftp.list(remote);
|
||||
for (const file of remoteFiles) {
|
||||
if (file.name.endsWith(".jar") && files.includes(file.name)) {
|
||||
const remoteFilePath = path.posix.join(remote, file.name);
|
||||
console.log(`🗑️ Deleting remote file: ${remoteFilePath}`);
|
||||
await sftp.delete(remoteFilePath);
|
||||
}
|
||||
}
|
||||
const remoteJars = remoteFiles.filter(f => f.name.endsWith(".jar"));
|
||||
|
||||
for (const localFile of localFiles) {
|
||||
const baseName = extractBaseName(localFile);
|
||||
const toDelete = remoteJars.filter(remoteFile =>
|
||||
extractBaseName(remoteFile.name) === baseName
|
||||
);
|
||||
for (const file of toDelete) {
|
||||
const fullPath = path.posix.join(remote, file.name);
|
||||
await sftp.delete(fullPath);
|
||||
console.log(`🗑️ Deleted remote: ${file.name}`);
|
||||
}
|
||||
// 🚀 Upload new files
|
||||
for (const file of files) {
|
||||
const local = path.join(DOWNLOAD_PATH, file);
|
||||
const remoteFile = path.posix.join(remote, file);
|
||||
console.log(`🚀 Uploading ${file} → ${remoteFile}`);
|
||||
await sftp.put(local, remoteFile);
|
||||
const localPath = path.join(DOWNLOAD_PATH, localFile);
|
||||
const remotePath = path.posix.join(remote, localFile);
|
||||
await sftp.fastPut(localPath, remotePath);
|
||||
console.log(`⬆️ Uploaded: ${localFile}`);
|
||||
}
|
||||
await sftp.end();
|
||||
console.log("✅ SFTP upload finished.");
|
||||
} catch (err) {
|
||||
console.error("❌ SFTP upload failed:", err.message);
|
||||
console.error("❌ SFTP Error:", err.message);
|
||||
} finally {
|
||||
sftp.end();
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user