|
| 1 | +import json |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +from loguru import logger |
1 | 5 | from pathlib import Path |
| 6 | +from tempfile import TemporaryDirectory |
| 7 | +from typing import Optional, List, Union |
2 | 8 |
|
3 | 9 |
|
4 | 10 | CLIENT_DIR = Path(__file__).parent |
| 11 | + |
| 12 | + |
| 13 | +def import_path(name: str) -> Optional[str]: |
| 14 | + path = CLIENT_DIR / "web_modules" |
| 15 | + for name_part in name.split("/"): |
| 16 | + if not path.is_dir(): |
| 17 | + return None |
| 18 | + path /= name_part |
| 19 | + full_path = path.with_suffix(".js") |
| 20 | + if not full_path.is_file(): |
| 21 | + return None |
| 22 | + return _web_module(name) |
| 23 | + |
| 24 | + |
| 25 | +def define_module(name: str, source: str) -> str: |
| 26 | + path = CLIENT_DIR / "web_modules" |
| 27 | + for n in name.split("/"): |
| 28 | + if not path.exists(): |
| 29 | + path.mkdir() |
| 30 | + path /= n |
| 31 | + module = path.with_suffix(".js") |
| 32 | + with module.open("w+") as f: |
| 33 | + f.write(source) |
| 34 | + return _web_module(name) |
| 35 | + |
| 36 | + |
| 37 | +def install(*dependencies: str) -> None: |
| 38 | + pkg = _package_json() |
| 39 | + |
| 40 | + npm_install = [] |
| 41 | + for dep in dependencies: |
| 42 | + install_spec, *import_paths = dep.split(" ") |
| 43 | + if not import_paths: |
| 44 | + raise ValueError( |
| 45 | + "Expected a space seperated string where an installation " |
| 46 | + f"spec is followed by at least on import path, not '{dep}'" |
| 47 | + ) |
| 48 | + pkg["snowpack"]["webDependencies"].extend(import_paths) |
| 49 | + npm_install.append(install_spec) |
| 50 | + |
| 51 | + with TemporaryDirectory() as tempdir: |
| 52 | + with (Path(tempdir) / "package.json").open("w+") as f: |
| 53 | + json.dump(pkg, f) |
| 54 | + |
| 55 | + _run_subprocess(["npm", "install"], tempdir) |
| 56 | + if npm_install: |
| 57 | + _run_subprocess(["npm", "install"] + npm_install, tempdir) |
| 58 | + _run_subprocess(["npm", "run", "snowpack"], tempdir) |
| 59 | + |
| 60 | + |
| 61 | +def restore() -> None: |
| 62 | + for path in ["web_modules", "node_modules"]: |
| 63 | + full_path = CLIENT_DIR.joinpath(*path.split("/")) |
| 64 | + if full_path.is_file(): |
| 65 | + full_path.unlink() |
| 66 | + elif full_path.is_dir(): |
| 67 | + shutil.rmtree(full_path) |
| 68 | + install() |
| 69 | + |
| 70 | + |
| 71 | +def _package_json(): |
| 72 | + with (CLIENT_DIR / "package.json").open("r") as f: |
| 73 | + dependencies = json.load(f)["dependencies"] |
| 74 | + |
| 75 | + return { |
| 76 | + "dependencies": dependencies, |
| 77 | + "scripts": {"snowpack": "./node_modules/.bin/snowpack"}, |
| 78 | + "devDependencies": {"snowpack": "^1.6.0"}, |
| 79 | + "snowpack": { |
| 80 | + "installOptions": { |
| 81 | + "dest": str(CLIENT_DIR / "web_modules"), |
| 82 | + "include": str(CLIENT_DIR / "modules" / "**" / "*.js"), |
| 83 | + }, |
| 84 | + "webDependencies": [], |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +def _run_subprocess(args: List[str], cwd: Union[str, Path]): |
| 90 | + try: |
| 91 | + subprocess.run( |
| 92 | + args, cwd=cwd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE |
| 93 | + ) |
| 94 | + except subprocess.CalledProcessError as error: |
| 95 | + logger.error(error.stderr.decode()) |
| 96 | + raise |
| 97 | + |
| 98 | + |
| 99 | +def _web_module(name: str) -> str: |
| 100 | + return f"../web_modules/{name}.js" |
0 commit comments