4
4
from loguru import logger
5
5
from pathlib import Path
6
6
from tempfile import TemporaryDirectory
7
- from typing import Optional , List , Union , Dict , Sequence
7
+ from typing import Optional , List , Union , Dict , Sequence , Any , cast
8
8
9
9
from .utils import Spinner
10
10
@@ -110,9 +110,6 @@ def install(
110
110
delete_web_modules (exp , skip_missing = True )
111
111
112
112
with TemporaryDirectory () as tempdir :
113
- if BUILD_DIR .exists ():
114
- shutil .rmtree (BUILD_DIR )
115
-
116
113
tempdir_path = Path (tempdir )
117
114
temp_static_dir = tempdir_path / "static"
118
115
@@ -122,7 +119,15 @@ def install(
122
119
with open (temp_static_dir / "package.json" ) as f :
123
120
package_json = json .load (f )
124
121
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 )
126
131
127
132
with (temp_static_dir / "package.json" ).open ("w+" ) as f :
128
133
json .dump (package_json , f )
@@ -132,7 +137,15 @@ def install(
132
137
_run_subprocess (["npm" , "install" ] + package_list , temp_static_dir )
133
138
_run_subprocess (["npm" , "run" , "build" ], temp_static_dir )
134
139
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 )
136
149
137
150
138
151
def restore () -> None :
@@ -163,5 +176,23 @@ def _delete_os_paths(*paths: Path) -> None:
163
176
shutil .rmtree (p )
164
177
165
178
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
+
166
197
def _to_list_of_str (value : Sequence [str ]) -> List [str ]:
167
198
return [value ] if isinstance (value , str ) else list (value )
0 commit comments