Files
otunes/apps/desktop/scripts/package.mjs
T

94 lines
3.0 KiB
JavaScript

import { build, Platform, Arch } from "electron-builder"
import { mkdir, copyFile, writeFile, rm, readFile } from "node:fs/promises"
import path from "node:path"
import { customRuntime, desktopRoot, electronVersion } from "./config.mjs"
const arch = process.env.OTUNES_DESKTOP_ARCH ?? process.arch
if (
!["x64", "arm64", "universal"].includes(arch) ||
(arch === "universal" && process.platform !== "darwin")
) {
throw new Error("Supported targets are x64, arm64 and macOS universal")
}
const runtimes =
arch === "universal"
? {
x64: await customRuntime("x64", process.env.OTUNES_ELECTRON_DIST_X64),
arm64: await customRuntime(
"arm64",
process.env.OTUNES_ELECTRON_DIST_ARM64
),
}
: { [arch]: await customRuntime(arch) }
const platform =
process.platform === "darwin"
? Platform.MAC
: process.platform === "win32"
? Platform.WINDOWS
: Platform.LINUX
const staging = path.join(desktopRoot, ".package")
await rm(staging, { recursive: true, force: true })
await mkdir(staging, { recursive: true })
await copyFile(
path.join(desktopRoot, "main.cjs"),
path.join(staging, "main.cjs")
)
const pkg = JSON.parse(
await readFile(path.join(desktopRoot, "package.json"), "utf8")
)
const ref = process.env.OTUNES_DESKTOP_REF ?? ""
if (ref.startsWith("refs/tags/"))
pkg.version = ref.slice("refs/tags/".length).replace(/^v/, "")
delete pkg.devDependencies
delete pkg.scripts
await writeFile(
path.join(staging, "package.json"),
JSON.stringify(pkg, null, 2)
)
await copyFile(
path.join(desktopRoot, "server-config.cjs"),
path.join(staging, "server-config.cjs")
)
await build({
targets: platform.createTarget(undefined, Arch[arch]),
config: {
appId: "com.otunes.desktop",
productName: "oTunes",
electronVersion,
// Return a missing path for unexpected architectures so builder fails closed.
// Throwing inside this hook lets electron-builder fall back to stock Electron.
electronDist: ({ arch: runtimeArch }) =>
runtimes[runtimeArch]?.directory ??
path.join(desktopRoot, "runtime", `unsupported-${runtimeArch}`),
artifactName: "oTunes-${version}-${os}-${arch}.${ext}",
directories: { app: staging, output: path.join(desktopRoot, "dist") },
files: ["main.cjs", "server-config.cjs", "package.json"],
extraResources: [
{ from: path.join(desktopRoot, "web"), to: "web", filter: ["**/*"] },
],
npmRebuild: false,
mac: { category: "public.app-category.music", target: ["dmg"] },
win: { target: ["nsis"] },
linux: {
category: "AudioVideo",
target: ["flatpak"],
icon: path.resolve(desktopRoot, "../web/public/logo.png"),
},
flatpak: {
runtimeVersion: "25.08",
baseVersion: "25.08",
finishArgs: [
"--socket=wayland",
"--socket=fallback-x11",
"--share=ipc",
"--device=dri",
"--socket=pulseaudio",
"--share=network",
],
},
publish: null,
},
publish: "never",
})