66from tempfile import TemporaryDirectory
77from typing import List , Sequence , Set , Union
88
9- from idom .config import IDOM_CLIENT_BUILD_DIR
9+ from idom .config import IDOM_CLIENT_WEB_MODULE_BASE_URL
1010
11- from .utils import find_js_module_exports_in_source , get_package_name
11+ from . import _private
1212
1313logger = getLogger (__name__ )
1414
1515
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-
4116def web_module_exports (package_name : str ) -> List [str ]:
4217 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 (
4419 web_module_path (package_name ).read_text (encoding = "utf-8" )
4520 )
4621
4722
4823def web_module_url (package_name : str ) -> str :
4924 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"
5126
5227
5328def web_module_exists (package_name : str ) -> bool :
@@ -56,7 +31,7 @@ def web_module_exists(package_name: str) -> bool:
5631
5732def web_module_names () -> Set [str ]:
5833 names = []
59- web_mod_dir = _web_modules_dir ()
34+ web_mod_dir = _private . web_modules_dir ()
6035 for pth in web_mod_dir .glob ("**/*.js" ):
6136 rel_pth = pth .relative_to (web_mod_dir )
6237 if Path ("common" ) in rel_pth .parents :
@@ -79,7 +54,7 @@ def add_web_module(package_name: str, source: Union[Path, str]) -> str:
7954
8055
8156def 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 ("/" ))
8358 if must_exist and not path .exists ():
8459 raise ValueError (
8560 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:
8863
8964
9065def restore () -> None :
91- _copy_to_build_dir ( _BACKUP_BUILD_DIR )
66+ _private . restore_build_dir_from_backup ( )
9267
9368
9469def 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
10176 package_json_path = temp_app_dir / "package.json"
10277
10378 # 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 )
10580
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+ }
10784
10885 # make sure we don't delete anything we've already installed
10986 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
130107 _npm_run_build (temp_app_dir )
131108 logger .info ("Client built successfully ✅" )
132109
133- _copy_to_build_dir (temp_build_dir )
110+ _private . replace_build_dir (temp_build_dir )
134111
135112 not_discovered = package_names_to_install .difference (web_module_names ())
136113 if not_discovered :
0 commit comments