Files
otunes/apps/desktop/scripts/materialize-links.mjs
T
joshuafhigginsandClaude Opus 5.5 Verified 2a77c92332 feat(desktop): ship stock Electron for Windows and Linux
Drop the custom EAC-3 Chromium build and macOS packaging. Windows NSIS
installers and Flatpaks for x64 and ARM64 now build in one Linux
container image.

Packaged installers previously lost the bundled Next.js dependencies:
electron-builder drops the pnpm store and NSIS cannot carry symlinks.
The bundle now replaces pnpm links with nested real copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 00:14:47 -05:00

65 lines
2.3 KiB
JavaScript

import { cp, readdir, realpath, rm } from "node:fs/promises"
import path from "node:path"
// Installers cannot carry pnpm's symlinks: NSIS extracts them as broken files
// and electron-builder drops the `.pnpm` store. Replace every link outside the
// store with a real copy, nesting each package's dependencies under it (npm v2
// layout) so Node resolves the same versions pnpm chose. Then drop the store.
export async function materializeLinks(directory) {
const store = path.join(directory, "node_modules", ".pnpm")
for (const link of await findLinks(directory, store)) {
const real = await realpath(link)
await rm(link)
await copyPackage(real, link, new Set())
}
await rm(store, { recursive: true, force: true })
}
async function findLinks(directory, store) {
const links = []
for (const entry of await readdir(directory, { withFileTypes: true })) {
const file = path.join(directory, entry.name)
if (file === store) continue
if (entry.isSymbolicLink()) links.push(file)
else if (entry.isDirectory()) links.push(...(await findLinks(file, store)))
}
return links
}
// `real` is <store>/<id>/node_modules/<name>; its siblings are its dependencies.
async function copyPackage(real, destination, ancestors) {
const match = /^(.*[\\/]\.pnpm[\\/][^\\/]+[\\/]node_modules)[\\/](.+)$/.exec(
real
)
if (!match) throw new Error(`Link outside the pnpm store: ${real}`)
const [, root, name] = match
await cp(real, destination, { recursive: true })
const chain = new Set(ancestors).add(real)
for (const dependency of await packageNames(root)) {
if (dependency === name.split(path.sep).join("/")) continue
const source = await realpath(path.join(root, dependency))
// A package that depends on an ancestor resolves it by walking up.
if (chain.has(source)) continue
await copyPackage(
source,
path.join(destination, "node_modules", dependency),
chain
)
}
}
// Package names in a node_modules directory, including @scope/name.
async function packageNames(directory) {
const names = []
for (const entry of await readdir(directory)) {
if (entry.startsWith(".")) continue
if (!entry.startsWith("@")) {
names.push(entry)
continue
}
for (const scoped of await readdir(path.join(directory, entry)))
names.push(`${entry}/${scoped}`)
}
return names
}