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 sftp = new SftpClient();
|
||||||
const remote = sftpConfig.remotePath || "/";
|
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 = {
|
const connectOptions = {
|
||||||
host: sftpConfig.host,
|
host: sftpConfig.host,
|
||||||
@ -261,28 +261,35 @@ const uploadToSFTP = async () => {
|
|||||||
throw new Error("Missing SFTP password or private key.");
|
throw new Error("Missing SFTP password or private key.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const extractBaseName = (filename) => {
|
||||||
|
return filename.replace(/[-_.](v?\d.*)?\.jar$/, "").trim();
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sftp.connect(connectOptions);
|
await sftp.connect(connectOptions);
|
||||||
const remoteFiles = await sftp.list(remote);
|
const remoteFiles = await sftp.list(remote);
|
||||||
for (const file of remoteFiles) {
|
const remoteJars = remoteFiles.filter(f => f.name.endsWith(".jar"));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
// 🚀 Upload new files
|
||||||
for (const file of files) {
|
const localPath = path.join(DOWNLOAD_PATH, localFile);
|
||||||
const local = path.join(DOWNLOAD_PATH, file);
|
const remotePath = path.posix.join(remote, localFile);
|
||||||
const remoteFile = path.posix.join(remote, file);
|
await sftp.fastPut(localPath, remotePath);
|
||||||
console.log(`🚀 Uploading ${file} → ${remoteFile}`);
|
console.log(`⬆️ Uploaded: ${localFile}`);
|
||||||
await sftp.put(local, remoteFile);
|
|
||||||
}
|
}
|
||||||
await sftp.end();
|
|
||||||
console.log("✅ SFTP upload finished.");
|
|
||||||
} catch (err) {
|
} 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