99
1010CLIENT_DIR = Path (__file__ ).parent
1111
12+ CORE_MODULES = CLIENT_DIR / "core_modules"
13+ NODE_MODULES = CLIENT_DIR / "node_modules"
14+ WEB_MODULES = CLIENT_DIR / "web_modules"
15+ ETC_MODULES = CLIENT_DIR / "etc_modules"
1216
13- def import_path (name : str ) -> Optional [str ]:
14- path = CLIENT_DIR / "web_modules"
15- for name_part in name .split ("/" ):
16- if not path .is_dir ():
17- return None
18- path /= name_part
19- full_path = path .with_suffix (".js" )
20- if not full_path .is_file ():
21- return None
22- return _web_module (name )
17+
18+ def import_path (prefix : str , name : str ) -> Optional [str ]:
19+ if not module_exists (prefix , name ):
20+ raise ValueError (f"Module '{ _module_js_path (prefix , name )} ' does not exist." )
21+ return _module_js_path (prefix , name )
2322
2423
2524def define_module (name : str , source : str ) -> str :
26- path = CLIENT_DIR
27- for n in ["etc_modules" ] + name .split ("/" ):
28- if not path .exists ():
29- path .mkdir ()
30- path /= n
31- module = path .with_suffix (".js" )
32- with module .open ("w+" ) as f :
25+ path = _create_module_os_path (ETC_MODULES , name )
26+ with path .open ("w+" ) as f :
3327 f .write (source )
34- return _etc_module (name )
28+ return _module_js_path ("etc_modules" , name )
29+
30+
31+ def delete_module (prefix : str , name : str ) -> None :
32+ if not module_exists (prefix , name ):
33+ raise ValueError (f"Module '{ _module_js_path (prefix , name )} ' does not exist." )
34+ return None
35+
36+
37+ def module_exists (prefix : Union [str , Path ], name : str ) -> bool :
38+ return _find_module_os_path (prefix , name ) is not None
3539
3640
3741def install (* dependencies : str ) -> None :
@@ -59,12 +63,7 @@ def install(*dependencies: str) -> None:
5963
6064
6165def restore () -> None :
62- for path in ["web_modules" , "node_modules" ]:
63- full_path = CLIENT_DIR .joinpath (* path .split ("/" ))
64- if full_path .is_file ():
65- full_path .unlink ()
66- elif full_path .is_dir ():
67- shutil .rmtree (full_path )
66+ _delete_os_paths (WEB_MODULES , NODE_MODULES , ETC_MODULES )
6867 install ()
6968
7069
@@ -78,8 +77,8 @@ def _package_json():
7877 "devDependencies" : {"snowpack" : "^1.6.0" },
7978 "snowpack" : {
8079 "installOptions" : {
81- "dest" : str (CLIENT_DIR / "web_modules" ),
82- "include" : str (CLIENT_DIR / "modules" / "**" / "*.js" ),
80+ "dest" : str (WEB_MODULES ),
81+ "include" : str (CORE_MODULES / "**" / "*.js" ),
8382 },
8483 "webDependencies" : [],
8584 },
@@ -96,9 +95,40 @@ def _run_subprocess(args: List[str], cwd: Union[str, Path]):
9695 raise
9796
9897
99- def _web_module (name : str ) -> str :
100- return f"../web_modules/{ name } .js"
98+ def _module_js_path (prefix : str , name : str ) -> str :
99+ return f"../{ prefix } /{ name } .js"
100+
101+
102+ def _find_module_os_path (prefix : Union [str , Path ], name : str ) -> Optional [Path ]:
103+ if isinstance (prefix , str ):
104+ path = CLIENT_DIR / prefix
105+ else :
106+ path = prefix
107+ for name_part in name .split ("/" ):
108+ if not path .is_dir ():
109+ return None
110+ path /= name_part
111+ full_path = path .with_suffix (".js" )
112+ if not full_path .is_file ():
113+ return None
114+ return full_path
115+
116+
117+ def _create_module_os_path (prefix : Union [str , Path ], name : str ) -> Path :
118+ if isinstance (prefix , str ):
119+ path = CLIENT_DIR / prefix
120+ else :
121+ path = prefix
122+ for n in name .split ("/" ):
123+ if not path .exists ():
124+ path .mkdir ()
125+ path /= n
126+ return path .with_suffix (".js" )
101127
102128
103- def _etc_module (name : str ) -> str :
104- return f"../etc_modules/{ name } .js"
129+ def _delete_os_paths (* paths : Path ) -> None :
130+ for p in paths :
131+ if p .is_file ():
132+ p .unlink ()
133+ elif p .is_dir ():
134+ shutil .rmtree (p )
0 commit comments