|
| 1 | +import hashlib |
| 2 | +import json |
1 | 3 | import os
|
2 |
| -import re |
3 | 4 | import shutil
|
4 | 5 | import time
|
5 | 6 | from subprocess import call
|
6 |
| -from subprocess import check_output |
7 | 7 |
|
8 | 8 | from django.apps import apps as django_apps
|
9 | 9 | from django.core.management import call_command
|
|
16 | 16 | from npm_mjs.tools import set_last_run
|
17 | 17 |
|
18 | 18 |
|
| 19 | +def get_package_hash(): |
| 20 | + """Generate a hash of all package.json files""" |
| 21 | + hash_md5 = hashlib.md5() |
| 22 | + for config in django_apps.get_app_configs(): |
| 23 | + for filename in ["package.json", "package.json5"]: |
| 24 | + filepath = os.path.join(config.path, filename) |
| 25 | + if os.path.exists(filepath): |
| 26 | + with open(filepath, "rb") as f: |
| 27 | + hash_md5.update(f.read()) |
| 28 | + return hash_md5.hexdigest() |
| 29 | + |
| 30 | + |
19 | 31 | def install_npm(force, stdout, post_npm_signal=True):
|
20 | 32 | change_times = [0]
|
21 | 33 | for path in SETTINGS_PATHS:
|
22 | 34 | change_times.append(os.path.getmtime(path))
|
23 | 35 | settings_change = max(change_times)
|
24 |
| - package_path = os.path.join(TRANSPILE_CACHE_PATH, "package.json") |
25 |
| - rspack_bin_path = os.path.join(TRANSPILE_CACHE_PATH, "node_modules/.bin/rspack") |
26 |
| - if os.path.exists(package_path) and os.path.exists(rspack_bin_path): |
27 |
| - package_change = os.path.getmtime(package_path) |
| 36 | + package_hash = get_package_hash() |
| 37 | + cache_file = os.path.join(TRANSPILE_CACHE_PATH, "package_hash.json") |
| 38 | + |
| 39 | + if os.path.exists(cache_file): |
| 40 | + with open(cache_file) as f: |
| 41 | + cached_hash = json.load(f).get("hash") |
28 | 42 | else:
|
29 |
| - package_change = -1 |
30 |
| - app_package_change = 0 |
31 |
| - configs = django_apps.get_app_configs() |
32 |
| - for config in configs: |
33 |
| - app_package_path = os.path.join(config.path, "package.json") |
34 |
| - if os.path.exists(app_package_path): |
35 |
| - app_package_change = max( |
36 |
| - os.path.getmtime(app_package_path), |
37 |
| - app_package_change, |
38 |
| - ) |
39 |
| - else: |
40 |
| - app_package_path = os.path.join(config.path, "package.json5") |
41 |
| - if os.path.exists(app_package_path): |
42 |
| - app_package_change = max( |
43 |
| - os.path.getmtime(app_package_path), |
44 |
| - app_package_change, |
45 |
| - ) |
| 43 | + cached_hash = None |
| 44 | + |
46 | 45 | npm_install = False
|
47 | 46 | if (
|
48 | 47 | settings_change > get_last_run("npm_install")
|
49 |
| - or app_package_change > package_change |
| 48 | + or package_hash != cached_hash |
50 | 49 | or force
|
51 | 50 | ):
|
52 |
| - stdout.write("Installing npm dependencies...") |
| 51 | + stdout.write("Installing pnpm dependencies...") |
53 | 52 | if not os.path.exists(TRANSPILE_CACHE_PATH):
|
54 | 53 | os.makedirs(TRANSPILE_CACHE_PATH)
|
55 | 54 | set_last_run("npm_install", int(round(time.time())))
|
56 |
| - node_modules_path = os.path.join(TRANSPILE_CACHE_PATH, "node_modules") |
57 |
| - if os.path.exists(node_modules_path): |
58 |
| - shutil.rmtree(node_modules_path, ignore_errors=True) |
59 | 55 | call_command("create_package_json")
|
60 |
| - if "SUDO_UID" in os.environ: |
61 |
| - del os.environ["SUDO_UID"] |
62 |
| - os.environ["npm_config_unsafe_perm"] = "true" |
63 |
| - node_version = int( |
64 |
| - re.search(r"\d+", str(check_output(["node", "--version"]))).group(), |
65 |
| - ) |
66 |
| - if node_version > 16: |
67 |
| - os.environ["NODE_OPTIONS"] = "--openssl-legacy-provider" |
68 |
| - call(["npm", "install"], cwd=TRANSPILE_CACHE_PATH) |
| 56 | + |
| 57 | + # Use pnpm instead of npm |
| 58 | + if shutil.which("pnpm") is None: |
| 59 | + stdout.write("Installing pnpm...") |
| 60 | + call(["npm", "install", "-g", "pnpm"], cwd=TRANSPILE_CACHE_PATH) |
| 61 | + |
| 62 | + # Check if pnpm-lock.yaml exists |
| 63 | + lockfile_path = os.path.join(TRANSPILE_CACHE_PATH, "pnpm-lock.yaml") |
| 64 | + if os.path.exists(lockfile_path): |
| 65 | + stdout.write("Installing dependencies with frozen lockfile...") |
| 66 | + call(["pnpm", "install", "--frozen-lockfile"], cwd=TRANSPILE_CACHE_PATH) |
| 67 | + else: |
| 68 | + stdout.write("Installing dependencies and generating lockfile...") |
| 69 | + call(["pnpm", "install"], cwd=TRANSPILE_CACHE_PATH) |
| 70 | + |
| 71 | + # Update cache |
| 72 | + with open(cache_file, "w") as f: |
| 73 | + json.dump({"hash": package_hash}, f) |
| 74 | + |
69 | 75 | if post_npm_signal:
|
70 | 76 | signals.post_npm_install.send(sender=None)
|
71 | 77 | npm_install = True
|
| 78 | + else: |
| 79 | + stdout.write("No changes detected, skipping pnpm install.") |
| 80 | + |
72 | 81 | return npm_install
|
73 | 82 |
|
74 | 83 |
|
|
0 commit comments