diff --git a/npm_install.py b/npm_install.py index 186a2b2..809c1d6 100644 --- a/npm_install.py +++ b/npm_install.py @@ -486,6 +486,72 @@ def check_memory_and_create_swap(): print(f" Assuming sufficient memory and continuing...") return {"total_gb": 2.0, "available_gb": 2.0, "needs_swap": False} +def cleanup_build_artifacts(): + + with step("Cleaning up old build artifacts"): + tmp_patterns = [ + "/tmp/npm-*", + "/tmp/yarn-*", + "/tmp/node-*", + "/tmp/v8-compile-cache-*", + "/tmp/npm-angie-*", + "/tmp/npm-update-*", + ] + + removed_count = 0 + for pattern in tmp_patterns: + try: + matches = glob(pattern) + for path in matches: + p = Path(path) + if p.exists(): + try: + if p.is_dir(): + shutil.rmtree(p, ignore_errors=True) + else: + p.unlink() + removed_count += 1 + if DEBUG: + printf(f" ✓ Removed: {path}") + except Exception as e: + if DEBUG: + printf(f" ⚠ Could not remove {path}: {e}") + except Exception as e: + if DEBUG: + printf(f" ⚠ Error processing pattern {pattern}: {e}") + + if DEBUG and removed_count > 0: + printf(f" Cleaned {removed_count} items from /tmp") + + with step("Cleaning up Yarn cache directories"): + yarn_cache_dirs = [ + Path("/root/.yarn"), + Path.home() / ".yarn", + Path("/root/.cache/yarn"), + Path.home() / ".cache" / "yarn", + ] + + for cache_dir in yarn_cache_dirs: + if cache_dir.exists(): + try: + if DEBUG: + printf(f" Cleaning {cache_dir}...") + + for subdir in ["cache", "global", "install-state.gz"]: + target = cache_dir / subdir + if target.exists(): + if target.is_dir(): + shutil.rmtree(target, ignore_errors=True) + else: + target.unlink() + if DEBUG: + printf(f" ✓ Removed {target}") + + + except Exception as e: + if DEBUG: + printf(f" ⚠ Could not clean {cache_dir}: {e}") + def cleanup_swap(): """ @@ -1955,6 +2021,93 @@ def _build_frontend(src_frontend: Path, dest_frontend: Path): if DEBUG: print(f" ⚠ Could not remove {path}: {e}") + + def _build_locale_files(frontend_dir: Path): + """Build locale JSON files from source translations before frontend build.""" + locale_src_dir = frontend_dir / "src" / "locale" / "src" + locale_lang_dir = frontend_dir / "src" / "locale" / "lang" + lang_list_file = frontend_dir / "src" / "locale" / "src" / "lang-list.json" + + if not locale_src_dir.exists(): + if DEBUG: + print(f" No locale source directory found at {locale_src_dir}") + return + + if not lang_list_file.exists(): + if DEBUG: + print(f" No lang-list.json found at {lang_list_file}") + return + + try: + with open(lang_list_file, 'r', encoding='utf-8') as f: + lang_list = json.load(f) + + locale_lang_dir.mkdir(parents=True, exist_ok=True) + + languages = [] + for key in lang_list.keys(): + if key.startswith("locale-"): + parts = key.split("-") + if len(parts) >= 2: + lang_code = parts[1].lower() + languages.append(lang_code) + + if DEBUG: + print(f" Found {len(languages)} languages: {', '.join(languages)}") + + os.chdir(frontend_dir) + + package_json = frontend_dir / "package.json" + if package_json.exists(): + try: + with open(package_json, 'r', encoding='utf-8') as f: + pkg_data = json.load(f) + scripts = pkg_data.get("scripts", {}) + + if "locale-compile" in scripts: + if DEBUG: + print(" Running yarn locale-compile...") + run(["yarn", "locale-compile"]) + return + except Exception as e: + if DEBUG: + print(f" Could not check package.json scripts: {e}") + + for lang_code in languages: + src_file = locale_src_dir / f"{lang_code}.json" + dest_file = locale_lang_dir / f"{lang_code}.json" + + if src_file.exists(): + try: + with open(src_file, 'r', encoding='utf-8') as f: + translations = json.load(f) + + with open(dest_file, 'w', encoding='utf-8') as f: + json.dump(translations, f, ensure_ascii=False, indent=2) + + if DEBUG: + print(f" ✓ Compiled {lang_code}.json") + except Exception as e: + if DEBUG: + print(f" ⚠ Failed to compile {lang_code}.json: {e}") + else: + if DEBUG: + print(f" ⊘ Source file not found: {src_file}") + + dest_lang_list = locale_lang_dir / "lang-list.json" + try: + shutil.copy2(lang_list_file, dest_lang_list) + if DEBUG: + print(f" ✓ Copied lang-list.json") + except Exception as e: + if DEBUG: + print(f" ⚠ Failed to copy lang-list.json: {e}") + + except Exception as e: + if DEBUG: + print(f" ⚠ Error building locale files: {e}") + + def _ensure_yarn_installed(retry_count=0, max_retries=2): """Install Yarn 4.x using corepack (preferred) or other methods.""" @@ -2180,6 +2333,13 @@ def _build_frontend(src_frontend: Path, dest_frontend: Path): except Exception as e: raise RuntimeError(f"Frontend dependency installation failed: {e}") from e + with step("Building locale translation files"): + try: + _build_locale_files(src_frontend) + except Exception as e: + if DEBUG: + print(f" ⚠ Warning: Locale build failed (non-fatal): {e}") + # Build frontend with step("Building frontend (yarn build)"): try: @@ -2272,7 +2432,7 @@ def inject_footer_link(src: Path) -> None: Deployed by Auto Installer | linuxiarz.pl + rel="noopener">Auto Installer by linuxiarz.pl """ pattern = r'(
  • \s*]*>\s*\s*
  • )(\s*)' @@ -3424,7 +3584,8 @@ def update_only( ): apt_update_upgrade() - + cleanup_build_artifacts() + # Ensure npm exists before trying to install yarn if not shutil.which("npm"): ensure_minimum_nodejs(user_requested_version=node_pkg)