diff --git a/scripts/stats.js b/scripts/stats.js index b75ef7f01..c493f120a 100644 --- a/scripts/stats.js +++ b/scripts/stats.js @@ -1,6 +1,7 @@ import * as Style from "../src/js/style.js"; import config from "../src/config.js"; import { Command, Option } from "commander"; +import fs from "node:fs"; const program = new Command(); program @@ -18,9 +19,31 @@ program .addOption( new Option("-s, --layer-size", "size of all layers").conflicts("allJson") ) + .addOption( + new Option( + "-ss1, --spritesheet-1x-size", + "size of 1x sprite sheet" + ).conflicts("allJson") + ) + .addOption( + new Option( + "-ss2, --spritesheet-2x-size", + "size of 2x sprite sheet" + ).conflicts("allJson") + ) + .addOption( + new Option("-sh, --shield-json-size", "size of ShieldJSON").conflicts( + "allJson" + ) + ) .option("-loc, --locales ", "language codes", ["mul"]) .option("-j, --all-json", "output all stats in JSON") - .option("-pp, --pretty", "pretty-print JSON output"); + .option("-pp, --pretty", "pretty-print JSON output") + .option( + "-d, --directory ", + "specify location of Americana distribution", + ["dist"] + ); program.parse(process.argv); @@ -29,6 +52,7 @@ const opts = program.opts(); if (Object.keys(opts).length === 1) program.help(); const locales = opts.locales[0].split(","); +const distDir = opts.directory; const style = Style.build( config.OPENMAPTILES_URL, @@ -45,8 +69,37 @@ if (opts.layerCount) { process.exit(); } -const styleSize = JSON.stringify(layers).length; +function spriteSheetSize(distDir, single) { + let size = single ? "" : "@2x"; + return ( + fs.statSync(`${distDir}/sprites/sprite${size}.png`).size + + fs.statSync(`${distDir}/sprites/sprite${size}.json`).size + ); +} + +function distFileSize(distDir, path) { + return fs.statSync(`${distDir}/${path}`).size; +} +const spriteSheet1xSize = spriteSheetSize(distDir, true); +if (opts.spritesheet1xSize) { + console.log(spriteSheet1xSize); + process.exit(); +} + +const spriteSheet2xSize = spriteSheetSize(distDir, false); +if (opts.spritesheet2xSize) { + console.log(spriteSheet2xSize); + process.exit(); +} + +const shieldJSONSize = distFileSize(distDir, "shields.json"); +if (opts.shieldJsonSize) { + console.log(shieldJSONSize); + process.exit(); +} + +const styleSize = JSON.stringify(layers).length; if (opts.layerSize) { console.log(styleSize); process.exit(); @@ -58,6 +111,9 @@ const stats = { layerCount, styleSize, layerGroup: {}, + spriteSheet1xSize, + spriteSheet2xSize, + shieldJSONSize, }; for (let i = 0; i < layerCount; i++) { diff --git a/scripts/stats_compare.js b/scripts/stats_compare.js index 45f70dad7..279171a60 100644 --- a/scripts/stats_compare.js +++ b/scripts/stats_compare.js @@ -22,13 +22,34 @@ const layersRow = mdCompareRow( ); const sizeRow = mdCompareRow( - "Size (b)", + "StyleJSON Size (b)", stats1.styleSize, stats2.styleSize, difference.styleSize ); -printTable("Style size statistics", [layersRow, sizeRow]); +const shieldRow = mdCompareRow( + "ShieldJSON Size (b)", + stats1.shieldJSONSize, + stats2.shieldJSONSize, + difference.shieldJSONSize +); + +const ss1xRow = mdCompareRow( + "1x Sprite Sheet Size (b)", + stats1.spriteSheet1xSize, + stats2.spriteSheet1xSize, + difference.spriteSheet1xSize +); + +const ss2xRow = mdCompareRow( + "2x Sprite Sheet Size (b)", + stats1.spriteSheet2xSize, + stats2.spriteSheet2xSize, + difference.spriteSheet2xSize +); + +printTable("Style size statistics", [layersRow, sizeRow, ss1xRow, ss2xRow, shieldRow]); /** * Show comparison of the number of layers in each group before and after