"""install_size.py - how much disk did "npx paperclipai" fill, and how much is the data?

    python install_size.py C:/pclab/data

npx keeps what it downloads in npm's cache folder, under _npx. This check finds
the folder that holds paperclipai and adds up its files, then does the same for
the data folder named on the command line.
"""
import subprocess
import sys
from pathlib import Path


def megabytes(folder):
    return sum(file.stat().st_size for file in folder.rglob("*") if file.is_file()) / 1e6


def main():
    cache = subprocess.run("npm config get cache", shell=True, capture_output=True, text=True)
    for folder in sorted((Path(cache.stdout.strip()) / "_npx").iterdir()):
        if (folder / "node_modules" / "paperclipai").is_dir():
            packages = sum(1 for item in (folder / "node_modules").iterdir())
            print(f"The program: {megabytes(folder):,.0f} MB in {folder.name}, "
                  f"{packages} folders in node_modules")
    if len(sys.argv) > 1:
        print(f"The data folder: {megabytes(Path(sys.argv[1])):,.0f} MB")


if __name__ == "__main__":
    main()
