Skip to content

Instantly share code, notes, and snippets.

@geoffreygarrett
Created June 22, 2024 10:25
Show Gist options
  • Save geoffreygarrett/79972124de4893d0ce5968356afd2a51 to your computer and use it in GitHub Desktop.
Save geoffreygarrett/79972124de4893d0ce5968356afd2a51 to your computer and use it in GitHub Desktop.
This Gawk script parses the output from 'deno info', extracts dependencies along with their sizes, converts all sizes to megabytes (MB), aggregates sizes by unique paths, and lists the top 10 largest dependencies sorted by size in descending order.
#!/usr/bin/env gawk -f
# deno_dependency_analyzer.awk
#
# Usage:
# deno info --import-map ./import_map.json <your_deno_script>.ts | gawk -f deno_dependency_analyzer.awk
#
# Description:
# This Gawk script parses the output from 'deno info', extracts dependencies along with their sizes,
# converts all sizes to megabytes (MB), aggregates sizes by unique paths, and lists the top 10 largest
# dependencies sorted by size in descending order.
{
# Remove ANSI escape sequences for cleaner parsing
gsub(/\x1B\[[0-9;]*[mK]/, "");
# Match dependency entries with their sizes, formatted as '(X.XXKB)' or '(X.XXMB)'
if (match($0, /(\S+)\s+\(([0-9]+\.[0-9]+)(KB|MB)\)/, arr)) {
# Extract the path, size, and unit from the regex match
path = arr[1];
size = arr[2];
unit = arr[3];
# Convert size to MB if it's initially in KB
if (unit == "KB") {
size /= 1024;
}
# Accumulate sizes by path in an associative array
sizes[path] += size;
}
}
END {
# Sort the associative array by size in descending order and print the top 10 entries
asorti(sizes, sorted, "@val_num_desc");
for (i = 1; i <= 10 && i <= length(sorted); i++) {
printf("%.2f MB\t%s\n", sizes[sorted[i]], sorted[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment