Fix bug with copy app (#371)

Fixes #361
This commit is contained in:
Will Chen
2025-06-09 14:58:55 -07:00
committed by GitHub
parent c2696cf1f9
commit 34ea593928

View File

@@ -40,6 +40,25 @@ import { Worker } from "worker_threads";
import { createFromTemplate } from "./createFromTemplate"; import { createFromTemplate } from "./createFromTemplate";
import { gitCommit } from "../utils/git_utils"; import { gitCommit } from "../utils/git_utils";
async function copyDir(
source: string,
destination: string,
filter?: (source: string) => boolean,
) {
await fsPromises.cp(source, destination, {
recursive: true,
filter: (src: string) => {
if (path.basename(src) === "node_modules") {
return false;
}
if (filter) {
return filter(src);
}
return true;
},
});
}
const logger = log.scope("app_handlers"); const logger = log.scope("app_handlers");
const handle = createLoggedHandler(logger); const handle = createLoggedHandler(logger);
@@ -258,14 +277,11 @@ export function registerAppHandlers() {
// 3. Copy the app folder // 3. Copy the app folder
try { try {
await fsPromises.cp(originalAppPath, newAppPath, { await copyDir(originalAppPath, newAppPath, (source: string) => {
recursive: true, if (!withHistory && path.basename(source) === ".git") {
filter: (source: string) => { return false;
if (!withHistory && path.basename(source) === ".git") { }
return false; return true;
}
return true;
},
}); });
} catch (error) { } catch (error) {
logger.error("Failed to copy app directory:", error); logger.error("Failed to copy app directory:", error);
@@ -744,10 +760,7 @@ export function registerAppHandlers() {
}); });
// Copy the directory without node_modules // Copy the directory without node_modules
await fsPromises.cp(oldAppPath, newAppPath, { await copyDir(oldAppPath, newAppPath);
recursive: true,
filter: (source) => !source.includes("node_modules"),
});
} catch (error: any) { } catch (error: any) {
logger.error( logger.error(
`Error moving app files from ${oldAppPath} to ${newAppPath}:`, `Error moving app files from ${oldAppPath} to ${newAppPath}:`,
@@ -789,10 +802,7 @@ export function registerAppHandlers() {
if (newAppPath !== oldAppPath) { if (newAppPath !== oldAppPath) {
try { try {
// Copy back from new to old // Copy back from new to old
await fsPromises.cp(newAppPath, oldAppPath, { await copyDir(newAppPath, oldAppPath);
recursive: true,
filter: (source) => !source.includes("node_modules"),
});
// Delete the new directory // Delete the new directory
await fsPromises.rm(newAppPath, { recursive: true, force: true }); await fsPromises.rm(newAppPath, { recursive: true, force: true });
} catch (rollbackError) { } catch (rollbackError) {