5
5
from .utils import build_lockfile_path , build_pre_lockfile_path , build_ws_config_path
6
6
from .workspace import PnpmWorkspace
7
7
from ..base import BasePackageManager , PackageManagerError
8
- from ..base .constants import NODE_MODULES_WORKSPACE_BUNDLE_FILENAME
8
+ from ..base .constants import NODE_MODULES_WORKSPACE_BUNDLE_FILENAME , PACKAGE_JSON_FILENAME , PNPM_LOCKFILE_FILENAME
9
9
from ..base .node_modules_bundler import bundle_node_modules
10
+ from ..base .package_json import PackageJson
10
11
from ..base .timeit import timeit
11
12
from ..base .utils import (
12
13
b_rooted ,
@@ -46,6 +47,40 @@ def load_lockfile_from_dir(cls, dir_path):
46
47
def get_local_pnpm_store ():
47
48
return os .path .join (home_dir (), ".cache" , "pnpm-store" )
48
49
50
+ @timeit
51
+ def _create_local_node_modules (self , nm_store_path : str , store_dir : str , virtual_store_dir : str ):
52
+ """
53
+ Creates ~/.nots/nm_store/$MODDIR/node_modules folder (with installed packages and .pnpm/virtual-store)
54
+
55
+ Should be used after build for local development ($SOURCE_DIR/node_modules should be a symlink to this folder).
56
+
57
+ But now it is also a workaround to provide valid node_modules structure in the parent folder of virtual-store
58
+ It is needed for fixing custom module resolvers (like in tsc, webpack, etc...), which are trying to find modules in the parents directories
59
+ """
60
+
61
+ # provide files required for `pnpm install`
62
+ pj = PackageJson .load (os .path .join (self .build_path , PACKAGE_JSON_FILENAME ))
63
+ required_files = [
64
+ PACKAGE_JSON_FILENAME ,
65
+ PNPM_LOCKFILE_FILENAME ,
66
+ * list (pj .bins_iter ()),
67
+ * pj .get_pnpm_patched_dependencies ().values (),
68
+ ]
69
+
70
+ for f in required_files :
71
+ src = os .path .join (self .build_path , f )
72
+ if os .path .exists (src ):
73
+ dst = os .path .join (nm_store_path , f )
74
+ try :
75
+ os .remove (dst )
76
+ except FileNotFoundError :
77
+ pass
78
+
79
+ os .makedirs (os .path .dirname (dst ), exist_ok = True )
80
+ shutil .copy (src , dst )
81
+
82
+ self ._run_pnpm_install (store_dir , virtual_store_dir , nm_store_path )
83
+
49
84
@timeit
50
85
def create_node_modules (self , yatool_prebuilder_path = None , local_cli = False , bundle = True ):
51
86
"""
@@ -68,7 +103,10 @@ def create_node_modules(self, yatool_prebuilder_path=None, local_cli=False, bund
68
103
# Use single virtual-store location in ~/.nots/nm_store/$MODDIR/node_modules/.pnpm/virtual-store
69
104
virtual_store_dir = os .path .join (build_nm_path (nm_store_path ), self ._VSTORE_NM_PATH )
70
105
71
- self ._run_pnpm_install (store_dir , virtual_store_dir )
106
+ self ._create_local_node_modules (nm_store_path , store_dir , virtual_store_dir )
107
+
108
+ self ._run_pnpm_install (store_dir , virtual_store_dir , self .build_path )
109
+
72
110
self ._run_apply_addons_if_need (yatool_prebuilder_path , virtual_store_dir )
73
111
self ._replace_internal_lockfile_with_original (virtual_store_dir )
74
112
@@ -81,7 +119,7 @@ def create_node_modules(self, yatool_prebuilder_path=None, local_cli=False, bund
81
119
)
82
120
83
121
@timeit
84
- def _run_pnpm_install (self , store_dir : str , virtual_store_dir : str ):
122
+ def _run_pnpm_install (self , store_dir : str , virtual_store_dir : str , cwd : str ):
85
123
install_cmd = [
86
124
"install" ,
87
125
"--frozen-lockfile" ,
@@ -100,8 +138,9 @@ def _run_pnpm_install(self, store_dir: str, virtual_store_dir: str):
100
138
virtual_store_dir ,
101
139
]
102
140
103
- self ._exec_command (install_cmd )
141
+ self ._exec_command (install_cmd , cwd = cwd )
104
142
143
+ @timeit
105
144
def calc_prepare_deps_inouts_and_resources (
106
145
self , store_path : str , has_deps : bool
107
146
) -> tuple [list [str ], list [str ], list [str ]]:
@@ -122,6 +161,7 @@ def calc_prepare_deps_inouts_and_resources(
122
161
123
162
return ins , outs , resources
124
163
164
+ @timeit
125
165
def calc_node_modules_inouts (self , local_cli : bool , has_deps : bool ) -> tuple [list [str ], list [str ]]:
126
166
"""
127
167
Returns input and output paths for command that creates `node_modules` bundle.
@@ -139,6 +179,7 @@ def calc_node_modules_inouts(self, local_cli: bool, has_deps: bool) -> tuple[lis
139
179
140
180
return ins , outs
141
181
182
+ @timeit
142
183
def extract_packages_meta_from_lockfiles (self , lf_paths ):
143
184
"""
144
185
:type lf_paths: iterable of BaseLockfile
@@ -167,6 +208,7 @@ def _prepare_workspace(self):
167
208
168
209
return PnpmWorkspace .load (build_ws_config_path (self .build_path ))
169
210
211
+ @timeit
170
212
def build_workspace (self , tarballs_store : str ):
171
213
"""
172
214
:rtype: PnpmWorkspace
@@ -182,6 +224,7 @@ def build_workspace(self, tarballs_store: str):
182
224
183
225
return ws
184
226
227
+ @timeit
185
228
def _build_merged_pre_lockfile (self , tarballs_store , dep_paths ):
186
229
"""
187
230
:type dep_paths: list of str
@@ -199,6 +242,7 @@ def _build_merged_pre_lockfile(self, tarballs_store, dep_paths):
199
242
200
243
lf .write ()
201
244
245
+ @timeit
202
246
def _build_merged_workspace_config (self , ws , dep_paths ):
203
247
"""
204
248
NOTE: This method mutates `ws`.
@@ -223,10 +267,12 @@ def _run_apply_addons_if_need(self, yatool_prebuilder_path, virtual_store_dir):
223
267
"--virtual-store" ,
224
268
virtual_store_dir ,
225
269
],
270
+ cwd = self .build_path ,
226
271
include_defaults = False ,
227
272
script_path = os .path .join (yatool_prebuilder_path , "build" , "bin" , "prebuilder.js" ),
228
273
)
229
274
275
+ @timeit
230
276
def _replace_internal_lockfile_with_original (self , virtual_store_dir ):
231
277
original_lf_path = build_lockfile_path (self .sources_path )
232
278
vs_lf_path = os .path .join (virtual_store_dir , "lock.yaml" )
@@ -236,14 +282,15 @@ def _replace_internal_lockfile_with_original(self, virtual_store_dir):
236
282
@timeit
237
283
def _copy_pnpm_patches (self ):
238
284
pj = self .load_package_json_from_dir (self .sources_path )
239
- patchedDependencies : dict [str , str ] = pj .data .get ("pnpm" , {}).get ("patchedDependencies" , {})
285
+ patched_dependencies : dict [str , str ] = pj .data .get ("pnpm" , {}).get ("patchedDependencies" , {})
240
286
241
- for p in patchedDependencies .values ():
287
+ for p in patched_dependencies .values ():
242
288
patch_source_path = os .path .join (self .sources_path , p )
243
289
patch_build_path = os .path .join (self .build_path , p )
244
290
os .makedirs (os .path .dirname (patch_build_path ), exist_ok = True )
245
291
shutil .copyfile (patch_source_path , patch_build_path )
246
292
293
+ @timeit
247
294
def _get_default_options (self ):
248
295
return super (PnpmPackageManager , self )._get_default_options () + [
249
296
"--stream" ,
@@ -252,5 +299,6 @@ def _get_default_options(self):
252
299
"--no-color" ,
253
300
]
254
301
302
+ @timeit
255
303
def _get_debug_log_path (self ):
256
304
return self ._nm_path (".pnpm-debug.log" )
0 commit comments