Skip to content

Commit

Permalink
Refactor file deletion logic in sync script to use item names for exc…
Browse files Browse the repository at this point in the history
…lusion
  • Loading branch information
nullpointerexceptionkek committed Jan 12, 2025
1 parent f2128cc commit a010037
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions scripts/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand All @@ -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) {
Expand All @@ -78,7 +78,6 @@ async function deleteServerFiles(remoteDir, exclude = [".env", "dist/runtime", "
}
}


async function uploadFile(localFilePath, remotePath) {
const fileContent = fs.readFileSync(localFilePath, "utf8");

Expand Down Expand Up @@ -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;
}

Expand All @@ -125,7 +123,6 @@ async function uploadDirectory(localDir, remoteDir, ignoreList = []) {
}
}


async function restartServer() {
console.log("Restarting the server...");
const options = {
Expand All @@ -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);
Expand Down

0 comments on commit a010037

Please sign in to comment.