|
| 1 | +import { cp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import { $ } from "execa"; |
| 5 | + |
| 6 | +import { packageOutputPath, packagePrefix } from "@/scripts/project.js"; |
| 7 | +import { build } from "@/scripts/tasks/build.js"; |
| 8 | +import pkg from "@/setup/package.json" with { type: "json" }; |
| 9 | + |
| 10 | +export const installPackage = async () => { |
| 11 | + const XDG_DATA_HOME = process.env.XDG_DATA_HOME; |
| 12 | + if (!XDG_DATA_HOME) { |
| 13 | + throw new Error("XDG_DATA_HOME is not set."); |
| 14 | + } |
| 15 | + |
| 16 | + await build(); |
| 17 | + |
| 18 | + // Copy the dist folder to a temporary location |
| 19 | + const packageName = `${packagePrefix}${pkg.name}`; |
| 20 | + const tempPath = `/tmp/${packageName}`; |
| 21 | + await rm(tempPath, { recursive: true, force: true }); |
| 22 | + await mkdir(tempPath, { recursive: true }); |
| 23 | + await cp(packageOutputPath, tempPath, { recursive: true }); |
| 24 | + await writeFile(path.resolve(tempPath, "pnpm-workspace.yaml"), ""); |
| 25 | + await writeFile( |
| 26 | + path.resolve(tempPath, ".npmrc"), |
| 27 | + [ |
| 28 | + "lockfile=false", |
| 29 | + "resolution-mode=time-based", |
| 30 | + "shared-workspace-lockfile=false", |
| 31 | + "inject-workspace-packages=true", |
| 32 | + ].join("\n"), |
| 33 | + ); |
| 34 | + |
| 35 | + // Install the package globally after pnpm deploy. |
| 36 | + process.env.NODE_ENV = "production"; |
| 37 | + const installPath = path.resolve(XDG_DATA_HOME, pkg.name); |
| 38 | + await rm(installPath, { recursive: true, force: true }); |
| 39 | + const $$ = $({ stdio: "inherit", verbose: "full", cwd: tempPath }); |
| 40 | + await $$`pnpm install`; |
| 41 | + await $$`pnpm deploy --filter=${packageName} --prod ${installPath}`; |
| 42 | + await $$({ reject: false })`pnpm uninstall --silent --global ${packageName}`; |
| 43 | + await $$`pnpm install --global ${installPath}`; |
| 44 | + await $$`rm --recursive ${tempPath}`; |
| 45 | +}; |
0 commit comments