Skip to content

Commit 2b2cc1c

Browse files
authored
Minimize size of bundle manifests by default (#156)
`--pretty`/`-p` flag will enable the previous pretty-printed output
1 parent f92a7ce commit 2b2cc1c

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

generate_bundles.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,23 @@ def generate_entry(
6161

6262

6363
def generate_manifest_file(
64-
json_files: list[Path], metadata_file: Path, path_prefix: str, outfile: Path
64+
json_files: list[Path],
65+
metadata_file: Path,
66+
path_prefix: str,
67+
outfile: Path,
68+
pretty=False,
6569
):
6670
"""Generates a manifest for all vendordep json files in json_files."""
6771
metadata_database = load_metadata(metadata_file)
6872
entries = []
6973
for file in json_files:
7074
entries.append(generate_entry(file, path_prefix, metadata_database))
71-
outfile.write_text(json.dumps(entries, indent=2), newline="\n")
7275

76+
format_args = {"indent": 2} if pretty else {"separators": (",", ":")}
77+
outfile.write_text(json.dumps(entries, **format_args), newline="\n")
7378

74-
def generate_bundle(year: str, root: Path, outdir: Path):
79+
80+
def generate_bundle(year: str, root: Path, outdir: Path, pretty=False):
7581
"""Generates a 'bundle' consisting of a YEAR.json manifest and a directory named YEAR containing all of the vendordep files
7682
7783
Requires a metadata file YEAR_metadata.json, and a directory named YEAR containing the input vendordeps.
@@ -84,7 +90,7 @@ def generate_bundle(year: str, root: Path, outdir: Path):
8490
manifest_file = Path(outdir) / f"{year}.json"
8591
vendordeps = [file for file in json_dir.glob("*.json")]
8692

87-
generate_manifest_file(vendordeps, metadata, path_prefix, manifest_file)
93+
generate_manifest_file(vendordeps, metadata, path_prefix, manifest_file, pretty)
8894

8995
# Copy all vendordeps to outdir/YEAR
9096
depsdir = outdir / year
@@ -110,10 +116,16 @@ def main():
110116
parser.add_argument(
111117
"year", nargs="+", type=str, help="Years to generate bundles for"
112118
)
113-
args = parser.parse_args()
114119

120+
parser.add_argument(
121+
"--pretty",
122+
"-p",
123+
action="store_true",
124+
help="Pretty-print the output. Without this option, output is minified.",
125+
)
126+
args = parser.parse_args()
115127
for year in args.year:
116-
generate_bundle(year, args.root, args.output)
128+
generate_bundle(year, args.root, args.output, args.pretty)
117129

118130

119131
if __name__ == "__main__":

0 commit comments

Comments
 (0)