Skip to content

Commit

Permalink
πŸ”§ prune unused AJ exports from build (#232)
Browse files Browse the repository at this point in the history
* πŸ”§ add resourcepack AJ pruner to build config
- we dont use these files in summit, so the summit RP doesnt need them

* πŸ”§ prune AJ display item model's unused AJ models from JSON file
- actually saves quite a bit of space

* ⚑️ minify all resourcepack JSON files in build
- significantly cuts down on build size

* πŸ”§ prune unused AJ datapack exports from build
  • Loading branch information
TheAfroOfDoom authored Oct 24, 2024
1 parent 6d8fc68 commit 95cc36e
Showing 1 changed file with 105 additions and 7 deletions.
112 changes: 105 additions & 7 deletions package-scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
const chalk = require('chalk');
const { copy, emptyDir, pathExists, readJson, writeJson } = require('fs-extra');
const { glob } = require('glob');
const parseArgs = require('minimist');
const { rimraf } = require('rimraf');

const buildDir = './build';

const prefixPaths = (prefix, paths) => paths.map((path) => `${prefix}${path}`);
const suffixPaths = (paths, suffix) => paths.map((path) => `${path}${suffix}`);

const animatedJavaExportsToPrune = prefixPaths('omegaflowey_', [
'housefly',
'petal_pipe_circle',
'petal_pipe_middle',
'soul_0_bandaid',
'soul_0_sword',
'venus_fly_trap',
]);

const getSummitDatapackPaths = () => {
const postProcessors = [];

Expand Down Expand Up @@ -129,15 +142,80 @@ const getSummitDatapackPaths = () => {
...primaryDatapackPaths,
]);

const pruneAnimatedJavaDatapackExports = async ({ compiledPath }) => {
const prunePromises = [];
for (const dir of animatedJavaExportsToPrune) {
const pruneFunctionDir = `${compiledPath}/datapacks/animated_java/data/animated_java/function/${dir}`;
prunePromises.push(rimraf(pruneFunctionDir));
prunePromises.push(
rimraf(
`${compiledPath}/datapacks/animated_java/data/animated_java/tags/function/${dir}`,
),
);
}
await Promise.all(prunePromises);
};
postProcessors.push(pruneAnimatedJavaDatapackExports);

const pruneAnimatedJavaDatapackTags = async ({ compiledPath }) => {
for (const [tagPath, suffix] of [
[
`${compiledPath}/datapacks/animated_java/data/animated_java/tags/function/global/on_load.json`,
'/on_load',
],
[
`${compiledPath}/datapacks/animated_java/data/animated_java/tags/function/global/root/on_load.json`,
'/root/on_load',
],
[
`${compiledPath}/datapacks/animated_java/data/animated_java/tags/function/global/root/on_tick.json`,
'/root/on_tick',
],
]) {
const loadTagJson = await readJson(tagPath);
loadTagJson.values = loadTagJson.values.filter((modelTag) => {
const namespace = modelTag
.replace('animated_java:', '')
.replace(suffix, '');
return !animatedJavaExportsToPrune.includes(namespace);
});
await writeJson(tagPath, loadTagJson);
}
};
postProcessors.push(pruneAnimatedJavaDatapackTags);
return { paths: datapackPaths, postProcessors };
};

const getSummitResourcepackPaths = () => {
const postProcessors = [];
const finalPostProcessors = [];

// Not `minecraft/sounds.json` since we just use that to disable ambient sounds
const minecraftPaths = prefixPaths('minecraft/', ['atlases', 'models']);

const pruneAnimatedJavaDisplayItem = async ({ compiledPath }) => {
const displayItemPath = `${compiledPath}/assets/minecraft/models/item/pink_dye.json`;
const displayItemJson = await readJson(displayItemPath);

const filteredOverrides = displayItemJson.overrides.filter((override) => {
if (!override.model.startsWith('animated_java:item/')) {
return true;
}

const modelName = override.model
.replace('animated_java:item/', '')
.split('/')[0];
return !animatedJavaExportsToPrune.includes(modelName);
});

displayItemJson.overrides = filteredOverrides;

displayItemJson.animated_java = undefined;

await writeJson(displayItemPath, displayItemJson, { spaces: 2 });
};
postProcessors.push(pruneAnimatedJavaDisplayItem);

const modelPaths = prefixPaths('models/entity/decorative/', [
'housefly.json',
'picture',
Expand Down Expand Up @@ -300,13 +378,30 @@ const getSummitResourcepackPaths = () => {
...omegaFloweyPaths,
]);

const pruneAnimatedJavaResourcepackExports = async ({ compiledPath }) => {
const prunePromises = [];
for (const dir of animatedJavaExportsToPrune) {
const compiledPruneDir = `${compiledPath}/assets/animated_java/models/item/${dir}`;
prunePromises.push(rimraf(compiledPruneDir));
}
await Promise.all(prunePromises);
};
postProcessors.push(pruneAnimatedJavaResourcepackExports);

const resourcepackPaths = prefixPaths('resourcepack/', [
'pack.mcmeta',
'pack.png',
...assetsPaths,
]);

return { paths: resourcepackPaths, postProcessors };
const minifyJsons = async ({ compiledPath }) => {
for (const path of await glob(`${compiledPath}/**/*.json`)) {
await writeJson(path, await readJson(path));
}
};
finalPostProcessors.push(minifyJsons);

return { paths: resourcepackPaths, postProcessors, finalPostProcessors };
};

const LOG_LEVEL = {
Expand Down Expand Up @@ -342,9 +437,6 @@ const logLevel = (level, ...data) => {
}
};

const prefixPaths = (prefix, paths) => paths.map((path) => `${prefix}${path}`);
const suffixPaths = (paths, suffix) => paths.map((path) => `${path}${suffix}`);

const getCompilePaths = ({ getSummitPaths }) => {
const { variant } = args;
switch (variant) {
Expand Down Expand Up @@ -396,7 +488,7 @@ const compile = async ({

await emptyDir(compiledPath);

const { paths, postProcessors } = compilePaths();
const { paths, postProcessors, finalPostProcessors = [] } = compilePaths();
if (args.verbose) {
verbose(chalk.bold(`${logColor(packType)} compile paths:`));
for (const src of paths) {
Expand All @@ -421,11 +513,17 @@ const compile = async ({
const checkmark = '\u{2705}';
info(`Finished copying ${paths.length} paths ${checkmark}`);

if (postProcessors.length > 0) {
info(`Running ${postProcessors.length} post-processors`);
const totalProcessors = postProcessors.length + finalPostProcessors.length;
if (totalProcessors > 0) {
info(`Running ${totalProcessors} post-processors`);
await Promise.all(
postProcessors.map((postProcessor) => postProcessor({ compiledPath })),
);
await Promise.all(
finalPostProcessors.map((postProcessor) =>
postProcessor({ compiledPath }),
),
);
info(`Finished post-processing ${checkmark}`);
}
};
Expand Down

0 comments on commit 95cc36e

Please sign in to comment.