From a0100375afb418c170de8775e9de8c61b2d98c75 Mon Sep 17 00:00:00 2001 From: Louis Li Date: Sat, 11 Jan 2025 16:24:33 -0800 Subject: [PATCH] Refactor file deletion logic in sync script to use item names for exclusion --- scripts/sync.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/scripts/sync.js b/scripts/sync.js index 470842e..4689df4 100644 --- a/scripts/sync.js +++ b/scripts/sync.js @@ -31,7 +31,7 @@ function httpsRequest(options, data = null) { }); } -async function deleteServerFiles(remoteDir, exclude = [".env", "dist/runtime", ".apollo", ".trash"]) { +async function deleteServerFiles(remoteDir, exclude) { try { const headers = { Authorization: `Bearer ${API_KEY}`, @@ -51,8 +51,8 @@ async function deleteServerFiles(remoteDir, exclude = [".env", "dist/runtime", " const allItems = listResponse.data.map((item) => `${remoteDir}/${item.attributes.name}`); const itemsToDelete = allItems.filter((item) => { - const relativePath = path.relative(REMOTE_DIR, item); // Get the relative path - return !exclude.some((ignore) => relativePath.startsWith(ignore)); + const itemName = path.basename(item); + return !exclude.includes(itemName); }); if (itemsToDelete.length === 0) { @@ -78,7 +78,6 @@ async function deleteServerFiles(remoteDir, exclude = [".env", "dist/runtime", " } } - async function uploadFile(localFilePath, remotePath) { const fileContent = fs.readFileSync(localFilePath, "utf8"); @@ -107,11 +106,10 @@ async function uploadDirectory(localDir, remoteDir, ignoreList = []) { for (const item of items) { const localPath = path.join(localDir, item); const remotePath = `${remoteDir}/${item}`; - const relativePath = path.relative(LOCAL_DIR, localPath); + const itemName = path.basename(localPath); - // Check if the relative path matches any ignore rule - if (ignoreList.some((ignore) => relativePath.startsWith(ignore))) { - console.log(`Skipping ignored item: ${relativePath}`); + if (ignoreList.includes(itemName)) { + console.log(`Skipping ignored item: ${localPath}`); continue; } @@ -125,7 +123,6 @@ async function uploadDirectory(localDir, remoteDir, ignoreList = []) { } } - async function restartServer() { console.log("Restarting the server..."); const options = { @@ -150,7 +147,8 @@ async function restartServer() { try { console.log(`Starting deployment process...`); - await deleteServerFiles(REMOTE_DIR); + await deleteServerFiles("dist", ["runtime"]); + await deleteServerFiles("dist", [".env", ".apollo", ".trash", "dist"]) console.log(`Uploading files from ${LOCAL_DIR} to ${REMOTE_DIR}...`); await uploadDirectory(LOCAL_DIR, REMOTE_DIR, IGNORE_LIST);