22import subprocess
33from pathlib import Path
44from tempfile import TemporaryDirectory
5- from typing import Optional , Iterable , Sequence , List
5+ from typing import Optional , Iterable , Sequence , List , Tuple
66
77from .build_config import (
88 BuildConfig ,
99 BuildConfigItem ,
1010 find_python_packages_build_config_items ,
1111)
12- from .utils import open_modifiable_json , find_js_module_exports
12+ from .utils import open_modifiable_json , find_js_module_exports_in_source
1313
1414from idom .cli import console
1515
1919_BUILD_CONFIG : Optional [BuildConfig ] = None
2020
2121
22+ class WebModuleError (Exception ):
23+ """Related to the use of javascript web modules"""
24+
25+
2226def build_config () -> BuildConfig :
2327 global _BUILD_CONFIG
2428 if _BUILD_CONFIG is None :
@@ -27,23 +31,24 @@ def build_config() -> BuildConfig:
2731
2832
2933def web_module_exports (source_name : str , package_name : str ) -> List [str ]:
30- dep_alias = build_config ().get_js_dependency_alias (source_name , package_name )
31- if dep_alias is None :
32- return []
33- module_file = find_client_build_path (f"web_modules/{ dep_alias } .js" )
34- if module_file is None :
35- return []
36- return find_js_module_exports (module_file )
34+ _ , module_file = _web_module_alias_and_file_path (source_name , package_name )
35+ with module_file .open () as f :
36+ return find_js_module_exports_in_source (f .read ())
3737
3838
3939def web_module_url (source_name : str , package_name : str ) -> Optional [str ]:
40- dep_alias = build_config ().get_js_dependency_alias (source_name , package_name )
41- if dep_alias is None :
42- return None
43- if find_client_build_path (f"web_modules/{ dep_alias } .js" ) is None :
44- return None
45- # need to go back a level since the JS that import this is in `core_components`
46- return f"../web_modules/{ dep_alias } .js"
40+ alias , _ = _web_module_alias_and_file_path (source_name , package_name )
41+ # need to go back a level since the JS that imports this is in `core_components`
42+ return f"../web_modules/{ alias } .js"
43+
44+
45+ def web_module_exists (source_name : str , package_name : str ) -> bool :
46+ try :
47+ _web_module_alias_and_file_path (source_name , package_name )
48+ except WebModuleError :
49+ return False
50+ else :
51+ return True
4752
4853
4954def find_client_build_path (rel_path : str ) -> Optional [Path ]:
@@ -108,6 +113,22 @@ def restore() -> None:
108113 _run_subprocess (["npm" , "run" , "build" ], APP_DIR )
109114
110115
116+ def _web_module_alias_and_file_path (
117+ source_name : str , package_name : str
118+ ) -> Tuple [str , Path ]:
119+ alias = build_config ().get_js_dependency_alias (source_name , package_name )
120+ if alias is None :
121+ raise WebModuleError (
122+ f"Package { package_name !r} is not declared as a dependency of { source_name !r} "
123+ )
124+ module_file = find_client_build_path (f"web_modules/{ alias } .js" )
125+ if module_file is None :
126+ raise WebModuleError (
127+ f"Dependency { package_name !r} of { source_name !r} was not installed"
128+ )
129+ return alias , module_file
130+
131+
111132def _npm_install (packages : Sequence [str ], cwd : Path ) -> None :
112133 _run_subprocess (["npm" , "install" ] + packages , cwd )
113134
0 commit comments