|
| 1 | +import hashlib |
| 2 | +import json |
1 | 3 | import os
|
2 | 4 | import shutil
|
3 | 5 |
|
| 6 | +from .constants import PNPM_PRE_LOCKFILE_FILENAME |
4 | 7 | from .lockfile import PnpmLockfile
|
5 | 8 | from .utils import build_lockfile_path, build_pre_lockfile_path, build_ws_config_path
|
6 | 9 | from .workspace import PnpmWorkspace
|
7 | 10 | from ..base import BasePackageManager, PackageManagerError
|
8 |
| -from ..base.constants import NODE_MODULES_WORKSPACE_BUNDLE_FILENAME, PACKAGE_JSON_FILENAME, PNPM_LOCKFILE_FILENAME |
| 11 | +from ..base.constants import ( |
| 12 | + NODE_MODULES_WORKSPACE_BUNDLE_FILENAME, |
| 13 | + PACKAGE_JSON_FILENAME, |
| 14 | + PNPM_LOCKFILE_FILENAME, |
| 15 | +) |
9 | 16 | from ..base.node_modules_bundler import bundle_node_modules
|
10 | 17 | from ..base.package_json import PackageJson
|
11 | 18 | from ..base.timeit import timeit
|
@@ -51,6 +58,17 @@ def get_local_pnpm_store():
|
51 | 58 | def get_local_old_pnpm_store():
|
52 | 59 | return os.path.join(home_dir(), ".cache", "pnpm-store")
|
53 | 60 |
|
| 61 | + @timeit |
| 62 | + def _get_file_hash(self, path: str): |
| 63 | + sha256 = hashlib.sha256() |
| 64 | + |
| 65 | + with open(path, "rb") as f: |
| 66 | + # Read the file in chunks |
| 67 | + for chunk in iter(lambda: f.read(4096), b""): |
| 68 | + sha256.update(chunk) |
| 69 | + |
| 70 | + return sha256.hexdigest() |
| 71 | + |
54 | 72 | @timeit
|
55 | 73 | def _create_local_node_modules(self, nm_store_path: str, store_dir: str, virtual_store_dir: str):
|
56 | 74 | """
|
@@ -85,6 +103,11 @@ def _create_local_node_modules(self, nm_store_path: str, store_dir: str, virtual
|
85 | 103 |
|
86 | 104 | self._run_pnpm_install(store_dir, virtual_store_dir, nm_store_path)
|
87 | 105 |
|
| 106 | + # Write node_modules.json to prevent extra `pnpm install` running 1 |
| 107 | + with open(os.path.join(nm_store_path, "node_modules.json"), "w") as f: |
| 108 | + pre_pnpm_lockfile_hash = self._get_file_hash(build_pre_lockfile_path(self.build_path)) |
| 109 | + json.dump({PNPM_PRE_LOCKFILE_FILENAME: {"hash": pre_pnpm_lockfile_hash}}, f) |
| 110 | + |
88 | 111 | @timeit
|
89 | 112 | def create_node_modules(self, yatool_prebuilder_path=None, local_cli=False, bundle=True):
|
90 | 113 | """
|
|
0 commit comments