6
6
from tempfile import TemporaryDirectory
7
7
from typing import List , Sequence , Set , Union
8
8
9
- from idom .config import IDOM_CLIENT_BUILD_DIR
9
+ from idom .config import IDOM_CLIENT_WEB_MODULE_BASE_URL
10
10
11
- from .utils import find_js_module_exports_in_source , get_package_name
11
+ from . import _private
12
12
13
13
logger = getLogger (__name__ )
14
14
15
15
16
- _THIS_DIR = Path (__file__ ).parent
17
- _APP_DIR = _THIS_DIR / "app"
18
- _BACKUP_BUILD_DIR = _APP_DIR / "build"
19
- # these directory are modified when users install packages
20
-
21
-
22
- def _build_dir () -> Path :
23
- return IDOM_CLIENT_BUILD_DIR .get ()
24
-
25
-
26
- def _web_modules_dir () -> Path :
27
- return _build_dir () / "_snowpack" / "pkg"
28
-
29
-
30
- def _copy_to_build_dir (source : Path ) -> None :
31
- target = _build_dir ()
32
- if target .exists ():
33
- shutil .rmtree (target )
34
- shutil .copytree (source , target , symlinks = True )
35
-
36
-
37
- if not _build_dir ().exists (): # coverage: skip
38
- _copy_to_build_dir (_BACKUP_BUILD_DIR )
39
-
40
-
41
16
def web_module_exports (package_name : str ) -> List [str ]:
42
17
web_module_path (package_name , must_exist = True )
43
- return find_js_module_exports_in_source (
18
+ return _private . find_js_module_exports_in_source (
44
19
web_module_path (package_name ).read_text (encoding = "utf-8" )
45
20
)
46
21
47
22
48
23
def web_module_url (package_name : str ) -> str :
49
24
web_module_path (package_name , must_exist = True )
50
- return f"./_snowpack/pkg /{ package_name } .js"
25
+ return IDOM_CLIENT_WEB_MODULE_BASE_URL . get () + f" /{ package_name } .js"
51
26
52
27
53
28
def web_module_exists (package_name : str ) -> bool :
@@ -56,7 +31,7 @@ def web_module_exists(package_name: str) -> bool:
56
31
57
32
def web_module_names () -> Set [str ]:
58
33
names = []
59
- web_mod_dir = _web_modules_dir ()
34
+ web_mod_dir = _private . web_modules_dir ()
60
35
for pth in web_mod_dir .glob ("**/*.js" ):
61
36
rel_pth = pth .relative_to (web_mod_dir )
62
37
if Path ("common" ) in rel_pth .parents :
@@ -79,7 +54,7 @@ def add_web_module(package_name: str, source: Union[Path, str]) -> str:
79
54
80
55
81
56
def web_module_path (package_name : str , must_exist : bool = False ) -> Path :
82
- path = _web_modules_dir ().joinpath (* (package_name + ".js" ).split ("/" ))
57
+ path = _private . web_modules_dir ().joinpath (* (package_name + ".js" ).split ("/" ))
83
58
if must_exist and not path .exists ():
84
59
raise ValueError (
85
60
f"Web module { package_name !r} does not exist at path { str (path )!r} "
@@ -88,7 +63,7 @@ def web_module_path(package_name: str, must_exist: bool = False) -> Path:
88
63
89
64
90
65
def restore () -> None :
91
- _copy_to_build_dir ( _BACKUP_BUILD_DIR )
66
+ _private . restore_build_dir_from_backup ( )
92
67
93
68
94
69
def build (packages_to_install : Sequence [str ], clean_build : bool = False ) -> None :
@@ -101,9 +76,11 @@ def build(packages_to_install: Sequence[str], clean_build: bool = False) -> None
101
76
package_json_path = temp_app_dir / "package.json"
102
77
103
78
# copy over the whole APP_DIR directory into the temp one
104
- shutil .copytree (_APP_DIR , temp_app_dir , symlinks = True )
79
+ shutil .copytree (_private . APP_DIR , temp_app_dir , symlinks = True )
105
80
106
- package_names_to_install = {get_package_name (p ) for p in packages_to_install }
81
+ package_names_to_install = {
82
+ _private .get_package_name (p ) for p in packages_to_install
83
+ }
107
84
108
85
# make sure we don't delete anything we've already installed
109
86
built_package_json_path = temp_build_dir / "package.json"
@@ -130,7 +107,7 @@ def build(packages_to_install: Sequence[str], clean_build: bool = False) -> None
130
107
_npm_run_build (temp_app_dir )
131
108
logger .info ("Client built successfully ✅" )
132
109
133
- _copy_to_build_dir (temp_build_dir )
110
+ _private . replace_build_dir (temp_build_dir )
134
111
135
112
not_discovered = package_names_to_install .difference (web_module_names ())
136
113
if not_discovered :
0 commit comments