|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
| 5 | +import { InstallOptions } from "types/open-next"; |
| 6 | + |
| 7 | +import logger from "../logger.js"; |
| 8 | + |
| 9 | +export function installDependencies( |
| 10 | + outputDir: string, |
| 11 | + installOptions?: InstallOptions, |
| 12 | +) { |
| 13 | + try { |
| 14 | + if (!installOptions) { |
| 15 | + return; |
| 16 | + } |
| 17 | + const name = outputDir.split("/").pop(); |
| 18 | + // First we create a tempDir |
| 19 | + const tempInstallDir = fs.mkdtempSync( |
| 20 | + path.join(os.tmpdir(), `open-next-install-${name}`), |
| 21 | + ); |
| 22 | + logger.info(`Installing dependencies for ${name}...`); |
| 23 | + // We then need to run install in the tempDir |
| 24 | + // We don't install in the output dir directly because it could contain a package.json, and npm would then try to reinstall not complete deps from tracing the files |
| 25 | + const installCommand = `npm install --arch=${installOptions.arch ?? "arm64"} --platform=linux --target=${installOptions.nodeVersion ?? "18"} --libc=${installOptions.libc ?? "glibc"} ${installOptions.packages.join(" ")}`; |
| 26 | + execSync(installCommand, { |
| 27 | + stdio: "pipe", |
| 28 | + cwd: tempInstallDir, |
| 29 | + env: { |
| 30 | + ...process.env, |
| 31 | + SHARP_IGNORE_GLOBAL_LIBVIPS: "1", |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + // Copy the node_modules to the outputDir |
| 36 | + fs.cpSync( |
| 37 | + path.join(tempInstallDir, "node_modules"), |
| 38 | + path.join(outputDir, "node_modules"), |
| 39 | + |
| 40 | + { recursive: true, force: true, dereference: true }, |
| 41 | + ); |
| 42 | + |
| 43 | + // Cleanup tempDir |
| 44 | + fs.rmSync(tempInstallDir, { recursive: true, force: true }); |
| 45 | + logger.info(`Dependencies installed for ${name}`); |
| 46 | + } catch (e: any) { |
| 47 | + logger.error(e.stdout.toString()); |
| 48 | + logger.error("Could not install dependencies"); |
| 49 | + } |
| 50 | +} |
0 commit comments