Skip to content

Commit 7caf910

Browse files
committed
ensure already installed modules are not lost
1 parent 1950728 commit 7caf910

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

idom/client/manage.py

+37-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from loguru import logger
55
from pathlib import Path
66
from tempfile import TemporaryDirectory
7-
from typing import Optional, List, Union, Dict, Sequence
7+
from typing import Optional, List, Union, Dict, Sequence, Any, cast
88

99
from .utils import Spinner
1010

@@ -110,9 +110,6 @@ def install(
110110
delete_web_modules(exp, skip_missing=True)
111111

112112
with TemporaryDirectory() as tempdir:
113-
if BUILD_DIR.exists():
114-
shutil.rmtree(BUILD_DIR)
115-
116113
tempdir_path = Path(tempdir)
117114
temp_static_dir = tempdir_path / "static"
118115

@@ -122,7 +119,15 @@ def install(
122119
with open(temp_static_dir / "package.json") as f:
123120
package_json = json.load(f)
124121

125-
package_json["snowpack"].setdefault("install", []).extend(export_list)
122+
temp_build_dir = temp_static_dir / "build"
123+
124+
cache = _read_idom_build_cache(temp_build_dir)
125+
126+
export_list = list(set(cache["export_list"] + export_list))
127+
package_list = list(set(cache["package_list"] + package_list))
128+
129+
pkg_snowpack = package_json.setdefault("snowpack", {})
130+
pkg_snowpack.setdefault("install", []).extend(export_list)
126131

127132
with (temp_static_dir / "package.json").open("w+") as f:
128133
json.dump(package_json, f)
@@ -132,7 +137,15 @@ def install(
132137
_run_subprocess(["npm", "install"] + package_list, temp_static_dir)
133138
_run_subprocess(["npm", "run", "build"], temp_static_dir)
134139

135-
shutil.copytree(temp_static_dir / "build", BUILD_DIR, symlinks=True)
140+
cache["export_list"] = export_list
141+
cache["package_list"] = package_list
142+
143+
_write_idom_build_cache(temp_build_dir, cache)
144+
145+
if BUILD_DIR.exists():
146+
shutil.rmtree(BUILD_DIR)
147+
148+
shutil.copytree(temp_build_dir, BUILD_DIR, symlinks=True)
136149

137150

138151
def restore() -> None:
@@ -163,5 +176,23 @@ def _delete_os_paths(*paths: Path) -> None:
163176
shutil.rmtree(p)
164177

165178

179+
def _read_idom_build_cache(path: Path) -> Dict[str, Any]:
180+
cache_file = path / ".idom-cache.json"
181+
if not cache_file.exists():
182+
return {
183+
"package_list": [],
184+
"export_list": [],
185+
}
186+
else:
187+
with cache_file.open() as f:
188+
return cast(Dict[str, Any], json.load(f))
189+
190+
191+
def _write_idom_build_cache(path: Path, cache: Dict[str, Any]) -> None:
192+
cache_file = path / ".idom-cache.json"
193+
with cache_file.open("w+") as f:
194+
json.dump(cache, f)
195+
196+
166197
def _to_list_of_str(value: Sequence[str]) -> List[str]:
167198
return [value] if isinstance(value, str) else list(value)

idom/client/static/package.json

-10
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,5 @@
2323
"htm": "^3.0.3",
2424
"react": "^16.13.1",
2525
"react-dom": "^16.13.1"
26-
},
27-
"snowpack": {
28-
"installOptions": {
29-
"namedExports": [
30-
"react"
31-
]
32-
},
33-
"buildOptions": {
34-
"clean": true
35-
}
3626
}
3727
}

idom/server/sanic.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ async def client_files(
8888
) -> response.HTTPResponse:
8989
file_extensions = [".html", ".js", ".json"]
9090
abs_path = find_path(path)
91-
return (
92-
(await response.file_stream(str(abs_path)))
93-
if abs_path is not None and abs_path.suffix in file_extensions
94-
else response.text(f"Could not find: {path!r}", status=404)
95-
)
91+
if (
92+
abs_path is not None
93+
and abs_path.suffix in file_extensions
94+
and not abs_path.stem.startswith(".")
95+
):
96+
return await response.file_stream(str(abs_path))
97+
else:
98+
response.text(f"Could not find: {path!r}", status=404)
9699

97100
if config["redirect_root_to_index"]:
98101

0 commit comments

Comments
 (0)