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 , Any , Sequence
7
+ from typing import Optional , List , Union , Dict , Sequence
8
8
9
9
from .utils import Spinner
10
10
11
11
12
12
STATIC_DIR = Path (__file__ ).parent / "static"
13
-
14
- CORE_MODULES = STATIC_DIR / "core_modules"
15
13
NODE_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
+
17
18
18
19
STATIC_SHIMS : Dict [str , Path ] = {}
19
20
20
21
21
22
def find_path (url_path : str ) -> Optional [Path ]:
22
23
url_path = url_path .strip ("/" )
23
24
24
- builtin_path = STATIC_DIR .joinpath (* url_path .split ("/" ))
25
+ builtin_path = BUILD_DIR .joinpath (* url_path .split ("/" ))
25
26
if builtin_path .exists ():
26
27
return builtin_path
27
28
@@ -83,9 +84,9 @@ def delete_web_modules(names: Sequence[str], skip_missing: bool = False) -> None
83
84
84
85
def installed () -> List [str ]:
85
86
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" :
89
90
names .append (str (rel_path .with_suffix ("" )))
90
91
return list (sorted (names ))
91
92
@@ -108,51 +109,40 @@ def install(
108
109
for exp in export_list :
109
110
delete_web_modules (exp , skip_missing = True )
110
111
111
- package_json = _package_json ()
112
- package_json ["snowpack" ]["webDependencies" ].extend (export_list )
113
-
114
112
with TemporaryDirectory () as tempdir :
113
+ if BUILD_DIR .exists ():
114
+ shutil .rmtree (BUILD_DIR )
115
+
115
116
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 ()
116
121
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 )
121
124
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 :
123
128
json .dump (package_json , f )
124
129
125
130
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 )
129
136
130
137
131
138
def restore () -> None :
132
139
with Spinner ("Restoring" ):
133
140
_delete_os_paths (WEB_MODULES , NODE_MODULES )
134
141
_run_subprocess (["npm" , "install" ], STATIC_DIR )
135
- _run_subprocess (["npm" , "run" , "snowpack " ], STATIC_DIR )
142
+ _run_subprocess (["npm" , "run" , "build " ], STATIC_DIR )
136
143
STATIC_SHIMS .clear ()
137
144
138
145
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
-
156
146
def _run_subprocess (args : List [str ], cwd : Union [str , Path ]) -> None :
157
147
try :
158
148
subprocess .run (
0 commit comments