Skip to content

Commit 2ce569b

Browse files
committed
Minimize size of bundle manifests by default
`--pretty`/`-p` flag will enable the previous pretty-printed output
1 parent 9927e39 commit 2ce569b

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
@@ -42,17 +42,23 @@ def generate_entry(
4242

4343

4444
def generate_manifest_file(
45-
json_files: list[Path], metadata_file: Path, path_prefix: str, outfile: Path
45+
json_files: list[Path],
46+
metadata_file: Path,
47+
path_prefix: str,
48+
outfile: Path,
49+
pretty=False,
4650
):
4751
"""Generates a manifest for all vendordep json files in json_files."""
4852
metadata_database = load_metadata(metadata_file)
4953
entries = []
5054
for file in json_files:
5155
entries.append(generate_entry(file, path_prefix, metadata_database))
52-
outfile.write_text(json.dumps(entries, indent=2), newline="\n")
5356

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

55-
def generate_bundle(year: str, root: Path, outdir: Path):
60+
61+
def generate_bundle(year: str, root: Path, outdir: Path, pretty=False):
5662
"""Generates a 'bundle' consisting of a YEAR.json manifest and a directory named YEAR containing all of the vendordep files
5763
5864
Requires a metadata file YEAR_metadata.json, and a directory named YEAR containing the input vendordeps.
@@ -65,7 +71,7 @@ def generate_bundle(year: str, root: Path, outdir: Path):
6571
manifest_file = Path(outdir) / f"{year}.json"
6672
vendordeps = [file for file in json_dir.glob("*.json")]
6773

68-
generate_manifest_file(vendordeps, metadata, path_prefix, manifest_file)
74+
generate_manifest_file(vendordeps, metadata, path_prefix, manifest_file, pretty)
6975

7076
# Copy all vendordeps to outdir/YEAR
7177
depsdir = outdir / year
@@ -91,10 +97,16 @@ def main():
9197
parser.add_argument(
9298
"year", nargs="+", type=str, help="Years to generate bundles for"
9399
)
94-
args = parser.parse_args()
95100

101+
parser.add_argument(
102+
"--pretty",
103+
"-p",
104+
action="store_true",
105+
help="Pretty-print the output. Without this option, output is minified.",
106+
)
107+
args = parser.parse_args()
96108
for year in args.year:
97-
generate_bundle(year, args.root, args.output)
109+
generate_bundle(year, args.root, args.output, args.pretty)
98110

99111

100112
if __name__ == "__main__":

0 commit comments

Comments
 (0)