|
| 1 | +import { isFunctionTagPath } from '../util/fileUtil' |
| 2 | +import { IFunctionTag, parseDataPackPath } from '../util/minecraftUtil' |
| 3 | +import { DataPackAJMeta } from './datapackCompiler' |
| 4 | +import { getExportPaths } from './exporter' |
| 5 | +import { ResourcePackAJMeta } from './resourcepackCompiler/global' |
| 6 | +import { replacePathPart } from './util' |
| 7 | + |
| 8 | +export async function cleanupExportedFiles() { |
| 9 | + const aj = Project!.animated_java |
| 10 | + const { |
| 11 | + resourcePackFolder, |
| 12 | + dataPackFolder, |
| 13 | + // textureExportFolder, |
| 14 | + // modelExportFolder, |
| 15 | + // displayItemPath, |
| 16 | + } = getExportPaths() |
| 17 | + |
| 18 | + if (aj.resource_pack_export_mode === 'raw') { |
| 19 | + const assetsMetaPath = PathModule.join(resourcePackFolder, 'assets.ajmeta') |
| 20 | + const assetsMeta = new ResourcePackAJMeta( |
| 21 | + assetsMetaPath, |
| 22 | + aj.export_namespace, |
| 23 | + Project!.last_used_export_namespace, |
| 24 | + resourcePackFolder |
| 25 | + ) |
| 26 | + assetsMeta.read() |
| 27 | + |
| 28 | + // PROGRESS_DESCRIPTION.set('Removing Old Data Pack Files...') |
| 29 | + // PROGRESS.set(0) |
| 30 | + // MAX_PROGRESS.set(assetsMeta.oldFiles.size) |
| 31 | + const removedFolders = new Set<string>() |
| 32 | + for (const file of assetsMeta.oldFiles) { |
| 33 | + if (!isFunctionTagPath(file)) { |
| 34 | + if (fs.existsSync(file)) await fs.promises.unlink(file) |
| 35 | + } else if (aj.export_namespace !== Project!.last_used_export_namespace) { |
| 36 | + const resourceLocation = parseDataPackPath(file)!.resourceLocation |
| 37 | + if ( |
| 38 | + resourceLocation.startsWith( |
| 39 | + `animated_java:${Project!.last_used_export_namespace}/` |
| 40 | + ) && |
| 41 | + fs.existsSync(file) |
| 42 | + ) { |
| 43 | + const newPath = replacePathPart( |
| 44 | + file, |
| 45 | + Project!.last_used_export_namespace, |
| 46 | + aj.export_namespace |
| 47 | + ) |
| 48 | + await fs.promises.mkdir(PathModule.dirname(newPath), { recursive: true }) |
| 49 | + await fs.promises.copyFile(file, newPath) |
| 50 | + await fs.promises.unlink(file) |
| 51 | + } |
| 52 | + } |
| 53 | + let folder = PathModule.dirname(file) |
| 54 | + while ( |
| 55 | + !removedFolders.has(folder) && |
| 56 | + fs.existsSync(folder) && |
| 57 | + (await fs.promises.readdir(folder)).length === 0 |
| 58 | + ) { |
| 59 | + await fs.promises.rm(folder, { recursive: true }) |
| 60 | + removedFolders.add(folder) |
| 61 | + folder = PathModule.dirname(folder) |
| 62 | + if (PathModule.basename(folder) === 'assets') break |
| 63 | + } |
| 64 | + // PROGRESS.set(PROGRESS.get() + 1) |
| 65 | + } |
| 66 | + |
| 67 | + assetsMeta.write() |
| 68 | + } |
| 69 | + |
| 70 | + if (aj.data_pack_export_mode === 'raw') { |
| 71 | + const dataMetaPath = PathModule.join(dataPackFolder, 'data.ajmeta') |
| 72 | + const dataMeta = new DataPackAJMeta( |
| 73 | + dataMetaPath, |
| 74 | + aj.export_namespace, |
| 75 | + Project!.last_used_export_namespace, |
| 76 | + dataPackFolder |
| 77 | + ) |
| 78 | + dataMeta.read() |
| 79 | + |
| 80 | + // PROGRESS_DESCRIPTION.set('Removing Old Data Pack Files...') |
| 81 | + // PROGRESS.set(0) |
| 82 | + // MAX_PROGRESS.set(dataMeta.oldFiles.size) |
| 83 | + const removedFolders = new Set<string>() |
| 84 | + for (const file of dataMeta.oldFiles) { |
| 85 | + if (isFunctionTagPath(file) && fs.existsSync(file)) { |
| 86 | + if (aj.export_namespace !== Project!.last_used_export_namespace) { |
| 87 | + const resourceLocation = parseDataPackPath(file)!.resourceLocation |
| 88 | + if ( |
| 89 | + resourceLocation.startsWith( |
| 90 | + `animated_java:${Project!.last_used_export_namespace}/` |
| 91 | + ) |
| 92 | + ) { |
| 93 | + const newPath = replacePathPart( |
| 94 | + file, |
| 95 | + Project!.last_used_export_namespace, |
| 96 | + aj.export_namespace |
| 97 | + ) |
| 98 | + await fs.promises.mkdir(PathModule.dirname(newPath), { recursive: true }) |
| 99 | + await fs.promises.copyFile(file, newPath) |
| 100 | + await fs.promises.unlink(file) |
| 101 | + } |
| 102 | + } |
| 103 | + // Remove mentions of the export namespace from the file |
| 104 | + const content: IFunctionTag = JSON.parse( |
| 105 | + (await fs.promises.readFile(file)).toString() |
| 106 | + ) |
| 107 | + content.values = content.values.filter( |
| 108 | + v => |
| 109 | + typeof v === 'string' && |
| 110 | + (!v.startsWith(`animated_java:${aj.export_namespace}/`) || |
| 111 | + !v.startsWith(`animated_java:${Project!.last_used_export_namespace}/`)) |
| 112 | + ) |
| 113 | + await fs.promises.writeFile(file, autoStringify(content)) |
| 114 | + } else { |
| 115 | + // Delete the file |
| 116 | + if (fs.existsSync(file)) await fs.promises.unlink(file) |
| 117 | + } |
| 118 | + let folder = PathModule.dirname(file) |
| 119 | + while ( |
| 120 | + !removedFolders.has(folder) && |
| 121 | + fs.existsSync(folder) && |
| 122 | + (await fs.promises.readdir(folder)).length === 0 |
| 123 | + ) { |
| 124 | + await fs.promises.rm(folder, { recursive: true }) |
| 125 | + removedFolders.add(folder) |
| 126 | + folder = PathModule.dirname(folder) |
| 127 | + if (PathModule.basename(folder) === 'data') break |
| 128 | + } |
| 129 | + // PROGRESS.set(PROGRESS.get() + 1) |
| 130 | + } |
| 131 | + |
| 132 | + dataMeta.write() |
| 133 | + } |
| 134 | + |
| 135 | + Blockbench.showQuickMessage('Exported files extracted successfully!', 2000) |
| 136 | +} |
0 commit comments