MCPcopy Create free account
hub / github.com/Shazbot/WH3-Mod-Manager / renamePackedFilesWithOptions

Function renamePackedFilesWithOptions

src/packFileSerializer.ts:3308–3409  ·  view source on GitHub ↗
(
  packPath: string,
  searchPattern: string,
  replaceTarget: string,
  useRegex: boolean,
  isDev?: boolean,
  pathFilter?: string,
)

Source from the content-addressed store, hash-verified

3306 }
3307}
3308export const renamePackedFilesWithOptions = async (
3309 packPath: string,
3310 searchPattern: string,
3311 replaceTarget: string,
3312 useRegex: boolean,
3313 isDev?: boolean,
3314 pathFilter?: string,
3315): Promise<void> => {
3316 const backupPath = packPath + ".backup." + Date.now();
3317 // Create temporary pack path we use to create the new pack
3318 const tempPath = packPath + ".temp." + Date.now();
3319 try {
3320 // Create backup
3321 await fsExtra.copy(packPath, backupPath);
3322 console.log(`Created backup at: ${backupPath}`);
3323 // Read the original pack
3324 const originalPack = await readPack(packPath, { skipParsingTables: true, skipSorting: true });
3325 // Find files that match the pattern and create renamed copies
3326 const renamedFiles: PackedFile[] = [];
3327 let hasChanges = false;
3328 // Create a map from original names to renamed files for quick lookup
3329 const renameMap = new Map<string, PackedFile>();
3330 for (const packedFile of originalPack.packedFiles) {
3331 // Check path filter first (if provided)
3332 if (pathFilter?.trim()) {
3333 const filePath = packedFile.name.substring(0, packedFile.name.lastIndexOf("\\"));
3334 if (!filePath.includes(pathFilter.trim())) {
3335 continue; // Skip files that don't match path filter
3336 }
3337 }
3338 const baseFilename = getBaseFilename(packedFile.name);
3339 let newBaseFilename: string | null = null;
3340 if (useRegex) {
3341 const regex = new RegExp(searchPattern, "g");
3342 if (regex.test(baseFilename)) {
3343 regex.lastIndex = 0; // Reset for replacement
3344 newBaseFilename = baseFilename.replace(regex, replaceTarget);
3345 }
3346 } else {
3347 // Simple string replacement
3348 if (baseFilename.includes(searchPattern)) {
3349 newBaseFilename = baseFilename.replace(new RegExp(escapeRegExp(searchPattern), "g"), replaceTarget);
3350 }
3351 }
3352 if (newBaseFilename && newBaseFilename !== baseFilename) {
3353 const newFileName = replaceBaseFilename(packedFile.name, newBaseFilename);
3354 console.log(`Renaming with options: ${packedFile.name} -> ${newFileName}`);
3355 // Create a copy of the packed file with new name
3356 const renamedFile: PackedFile = {
3357 ...packedFile,
3358 name: newFileName,
3359 };
3360 renameMap.set(packedFile.name, renamedFile);
3361 renamedFiles.push(renamedFile);
3362 hasChanges = true;
3363 }
3364 }
3365 if (!hasChanges) {

Callers 1

registerIpcMainListenersFunction · 0.85

Calls 5

readPackFunction · 0.70
getBaseFilenameFunction · 0.70
escapeRegExpFunction · 0.70
replaceBaseFilenameFunction · 0.70

Tested by

no test coverage detected