|
15 | 15 | NODE_MODULES = STATIC_DIR / "node_modules"
|
16 | 16 | WEB_MODULES = STATIC_DIR / "web_modules"
|
17 | 17 |
|
| 18 | +STATIC_SHIMS: Dict[str, Path] = {} |
18 | 19 |
|
19 |
| -def web_module(name: str) -> str: |
| 20 | + |
| 21 | +def find_path(url_path: str) -> Optional[Path]: |
| 22 | + url_path = url_path.strip("/") |
| 23 | + |
| 24 | + builtin_path = STATIC_DIR.joinpath(*url_path.split("/")) |
| 25 | + if builtin_path.exists(): |
| 26 | + return builtin_path |
| 27 | + |
| 28 | + return STATIC_SHIMS.get(url_path) |
| 29 | + |
| 30 | + |
| 31 | +def web_module_path(name: str) -> Optional[Path]: |
| 32 | + return find_path(f"web_modules/{name}.js") |
| 33 | + |
| 34 | + |
| 35 | +def web_module_url(name: str) -> str: |
20 | 36 | path = f"../{WEB_MODULES.name}/{name}.js"
|
21 | 37 | if not web_module_exists(name):
|
22 | 38 | raise ValueError(f"Module '{path}' does not exist.")
|
23 | 39 | return path
|
24 | 40 |
|
25 | 41 |
|
26 |
| -def web_module_path(name: str) -> Optional[Path]: |
27 |
| - path = _create_web_module_os_path(name).with_suffix(".js") |
28 |
| - return path if path.is_file() else None |
29 |
| - |
30 |
| - |
31 | 42 | def web_module_exists(name: str) -> bool:
|
32 |
| - return web_module_path(name) is not None |
| 43 | + return find_path(f"web_modules/{name}.js") is not None |
33 | 44 |
|
34 | 45 |
|
35 |
| -def define_web_module(name: str, source: str) -> str: |
36 |
| - path = _create_web_module_os_path(name).with_suffix(".js") |
37 |
| - with path.open("w+") as f: |
38 |
| - f.write(source) |
39 |
| - return web_module(name) |
| 46 | +def register_web_module(name: str, source: Union[str, Path]) -> str: |
| 47 | + source_path = source if isinstance(source, Path) else Path(source) |
| 48 | + if web_module_exists(name): |
| 49 | + raise ValueError(f"Web module {name} already exists") |
| 50 | + if not source_path.is_file(): |
| 51 | + raise ValueError(f"Web modules source {source} does not exist or is not a file") |
| 52 | + STATIC_SHIMS[f"web_modules/{name}.js"] = source |
| 53 | + return web_module_url(name) |
40 | 54 |
|
41 | 55 |
|
42 | 56 | def delete_web_modules(names: Sequence[str], skip_missing: bool = False) -> None:
|
43 | 57 | paths = []
|
44 |
| - for n in _to_list_of_str(names): |
| 58 | + for name in _to_list_of_str(names): |
45 | 59 | exists = False
|
46 |
| - path = _create_web_module_os_path(n) |
47 |
| - js_path = path.with_suffix(".js") |
48 |
| - if path.is_dir(): |
| 60 | + |
| 61 | + dir_name = f"web_modules/{name}" |
| 62 | + js_name = f"web_modules/{name}.js" |
| 63 | + path = find_path(dir_name) |
| 64 | + js_path = find_path(js_name) |
| 65 | + |
| 66 | + if path is not None: |
49 | 67 | paths.append(path)
|
50 | 68 | exists = True
|
51 |
| - if js_path.is_file(): |
| 69 | + |
| 70 | + if js_name in STATIC_SHIMS: |
| 71 | + del STATIC_SHIMS[js_name] |
| 72 | + exists = True |
| 73 | + elif js_path is not None: |
52 | 74 | paths.append(js_path)
|
53 | 75 | exists = True
|
| 76 | + |
54 | 77 | if not exists and not skip_missing:
|
55 |
| - raise ValueError(f"Module '{n}' does not exist.") |
| 78 | + raise ValueError(f"Module '{name}' does not exist.") |
| 79 | + |
56 | 80 | for p in paths:
|
57 | 81 | _delete_os_paths(p)
|
58 | 82 |
|
@@ -109,6 +133,7 @@ def restore() -> None:
|
109 | 133 | _delete_os_paths(WEB_MODULES, NODE_MODULES)
|
110 | 134 | _run_subprocess(["npm", "install"], STATIC_DIR)
|
111 | 135 | _run_subprocess(["npm", "run", "snowpack"], STATIC_DIR)
|
| 136 | + STATIC_SHIMS.clear() |
112 | 137 |
|
113 | 138 |
|
114 | 139 | def _package_json() -> Dict[str, Any]:
|
@@ -141,15 +166,6 @@ def _run_subprocess(args: List[str], cwd: Union[str, Path]) -> None:
|
141 | 166 | return None
|
142 | 167 |
|
143 | 168 |
|
144 |
| -def _create_web_module_os_path(name: str) -> Path: |
145 |
| - path = WEB_MODULES |
146 |
| - for n in name.split("/"): |
147 |
| - if not path.exists(): |
148 |
| - path.mkdir() |
149 |
| - path /= n |
150 |
| - return path |
151 |
| - |
152 |
| - |
153 | 169 | def _delete_os_paths(*paths: Path) -> None:
|
154 | 170 | for p in paths:
|
155 | 171 | if p.is_file():
|
|
0 commit comments