import fs from "fs"; import path from "path"; import { SyncConfig, SyncItem, GlobalOptions } from "./ConfigTypes"; import { getRepoLocalPath } from "./gitOperations"; import prompt from "prompt-sync"; // You'll need to install prompt-sync for this const promptSync = prompt({ sigint: true }); function isClientFileMoreRecent( clientFilePath: string, centralRepoFilePath: string ): boolean { if (!fs.existsSync(clientFilePath) || !fs.existsSync(centralRepoFilePath)) { return false; } const clientFileStats = fs.statSync(clientFilePath); const centralRepoFileStats = fs.statSync(centralRepoFilePath); return clientFileStats.mtimeMs > centralRepoFileStats.mtimeMs; } function handleOverwrite( destinationPath: string, options: GlobalOptions ): boolean { if (!fs.existsSync(destinationPath)) { return true; // If file doesn't exist, no need to handle overwrites } if (options.conflictResolution === "overwrite") { return true; // Always overwrite } else if (options.conflictResolution === "ignore") { return false; // Always ignore/skip } else if (options.conflictResolution === "prompt") { const answer = promptSync( `File ${destinationPath} exists. Overwrite? (y/n): ` ); return answer.toLowerCase() === "y"; } return false; // Default to not overwrite if not specified } function copyFileSync(source: string, target: string) { let targetFile = target; // If target is a directory, a new file with the same name will be created if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) { targetFile = path.join(target, path.basename(source)); } fs.copyFileSync(source, targetFile); console.log(`File copied from ${source} to ${targetFile}`); } function copyFolderSync(source: string, target: string) { if (!fs.lstatSync(source).isDirectory()) { console.error(`${source} is not a directory`); return; } if (!fs.existsSync(target)) { fs.mkdirSync(target, { recursive: true }); } const files = fs.readdirSync(source); files.forEach((file) => { const curSource = path.join(source, file); if (fs.lstatSync(curSource).isDirectory()) { copyFolderSync(curSource, path.join(target, file)); } else { copyFileSync(curSource, target); } }); } export function syncFiles(config: SyncConfig, configFilePath: string) { const repoLocalPath = getRepoLocalPath(config); config.syncItems.forEach((item: SyncItem) => { const sourcePath = path.join(repoLocalPath, item.sourcePath); const destinationPath = path.join( path.dirname(configFilePath), item.destinationPath ); if (isClientFileMoreRecent(destinationPath, sourcePath)) { console.log(`Client file is more recent, skipping: ${destinationPath}`); // Here, you can also add logic to handle these files differently, // such as preparing them for a pull request to the central repo return; } // If the client file is not more recent, proceed with the usual sync logic if (handleOverwrite(destinationPath, config.options)) { if (fs.lstatSync(sourcePath).isDirectory()) { copyFolderSync(sourcePath, destinationPath); } else { copyFileSync(sourcePath, destinationPath); } } else { console.log(`Skipped: ${destinationPath}`); } }); }