44from loguru import logger
55from pathlib import Path
66from tempfile import TemporaryDirectory
7- from typing import Optional , List , Union , Dict , Any , Sequence
7+ from typing import Optional , List , Union , Dict , Sequence
88
99from .utils import Spinner
1010
1111
1212STATIC_DIR = Path (__file__ ).parent / "static"
13-
14- CORE_MODULES = STATIC_DIR / "core_modules"
1513NODE_MODULES = STATIC_DIR / "node_modules"
16- WEB_MODULES = STATIC_DIR / "web_modules"
14+ BUILD_DIR = STATIC_DIR / "build"
15+ CORE_MODULES = BUILD_DIR / "core_modules"
16+ WEB_MODULES = BUILD_DIR / "web_modules"
17+
1718
1819STATIC_SHIMS : Dict [str , Path ] = {}
1920
2021
2122def find_path (url_path : str ) -> Optional [Path ]:
2223 url_path = url_path .strip ("/" )
2324
24- builtin_path = STATIC_DIR .joinpath (* url_path .split ("/" ))
25+ builtin_path = BUILD_DIR .joinpath (* url_path .split ("/" ))
2526 if builtin_path .exists ():
2627 return builtin_path
2728
@@ -83,9 +84,9 @@ def delete_web_modules(names: Sequence[str], skip_missing: bool = False) -> None
8384
8485def installed () -> List [str ]:
8586 names : List [str ] = []
86- for path in WEB_MODULES .rglob ("*" ):
87- if path .is_file () and path . suffix == ".js" :
88- rel_path = path . relative_to ( WEB_MODULES )
87+ for path in WEB_MODULES .rglob ("*.js " ):
88+ rel_path = path .relative_to ( WEB_MODULES )
89+ if rel_path . parent . name != "common" :
8990 names .append (str (rel_path .with_suffix ("" )))
9091 return list (sorted (names ))
9192
@@ -108,51 +109,40 @@ def install(
108109 for exp in export_list :
109110 delete_web_modules (exp , skip_missing = True )
110111
111- package_json = _package_json ()
112- package_json ["snowpack" ]["webDependencies" ].extend (export_list )
113-
114112 with TemporaryDirectory () as tempdir :
113+ if BUILD_DIR .exists ():
114+ shutil .rmtree (BUILD_DIR )
115+
115116 tempdir_path = Path (tempdir )
117+ temp_static_dir = tempdir_path / "static"
118+
119+ shutil .copytree (STATIC_DIR , temp_static_dir , symlinks = True )
120+ assert (temp_static_dir / "package.json" ).exists ()
116121
117- if NODE_MODULES .exists ():
118- shutil .copytree (
119- NODE_MODULES , tempdir_path / NODE_MODULES .name , symlinks = True
120- )
122+ with open (temp_static_dir / "package.json" ) as f :
123+ package_json = json .load (f )
121124
122- with (tempdir_path / "package.json" ).open ("w+" ) as f :
125+ package_json ["snowpack" ].setdefault ("install" , []).extend (export_list )
126+
127+ with (temp_static_dir / "package.json" ).open ("w+" ) as f :
123128 json .dump (package_json , f )
124129
125130 with Spinner (f"Installing: { ', ' .join (package_list )} " ):
126- _run_subprocess (["npm" , "install" ], tempdir )
127- _run_subprocess (["npm" , "install" ] + package_list , tempdir )
128- _run_subprocess (["npm" , "run" , "snowpack" ], tempdir )
131+ _run_subprocess (["npm" , "install" ], temp_static_dir )
132+ _run_subprocess (["npm" , "install" ] + package_list , temp_static_dir )
133+ _run_subprocess (["npm" , "run" , "build" ], temp_static_dir )
134+
135+ shutil .copytree (temp_static_dir / "build" , BUILD_DIR , symlinks = True )
129136
130137
131138def restore () -> None :
132139 with Spinner ("Restoring" ):
133140 _delete_os_paths (WEB_MODULES , NODE_MODULES )
134141 _run_subprocess (["npm" , "install" ], STATIC_DIR )
135- _run_subprocess (["npm" , "run" , "snowpack " ], STATIC_DIR )
142+ _run_subprocess (["npm" , "run" , "build " ], STATIC_DIR )
136143 STATIC_SHIMS .clear ()
137144
138145
139- def _package_json () -> Dict [str , Any ]:
140- with (STATIC_DIR / "package.json" ).open ("r" ) as f :
141- dependencies = json .load (f )["dependencies" ]
142-
143- return {
144- "dependencies" : dependencies ,
145- "scripts" : {"snowpack" : "./node_modules/.bin/snowpack" },
146- "devDependencies" : {"snowpack" : "^1.6.0" },
147- "snowpack" : {
148- "installOptions" : {
149- "dest" : str (WEB_MODULES ),
150- },
151- "webDependencies" : [],
152- },
153- }
154-
155-
156146def _run_subprocess (args : List [str ], cwd : Union [str , Path ]) -> None :
157147 try :
158148 subprocess .run (
0 commit comments