UNPKG

@aziontech/opennextjs-azion

Version:
65 lines (64 loc) 2.56 kB
/** * This code was originally copied and modified from the @opennextjs/cloudflare repository. * Significant changes have been made to adapt it for use with Azion. */ import { spawnSync } from "node:child_process"; import { readFileSync } from "node:fs"; import path from "node:path"; import { compareSemver } from "@opennextjs/aws/build/helper.js"; import logger from "@opennextjs/aws/logger.js"; /** * Checks the package.json `packageManager` field to determine whether yarn modern is used. * * @param options Build options. * @returns Whether yarn modern is used. */ function isYarnModern(options) { const packageJson = JSON.parse(readFileSync(path.join(options.monorepoRoot, "package.json"), "utf-8")); if (!packageJson.packageManager?.startsWith("yarn")) return false; const [, version] = packageJson.packageManager.split("@"); return version ? compareSemver(version, ">=", "4.0.0") : false; } /** * Prepends CLI flags with `--` so that certain package managers can pass args through to wrangler * properly. * * npm and yarn classic require `--` to be used, while pnpm and bun require that it is not used. * * @param options Build options. * @param args CLI args. * @returns Arguments with a passthrough flag injected when needed. */ function injectPassthroughFlagForArgs(options, args) { if (options.packager === "yarn" && !isYarnModern(options)) { return args; } if (options.packager !== "npm" && (options.packager !== "yarn" || isYarnModern(options))) { return args; } const flagInArgsIndex = args.findIndex((v) => v.startsWith("--")); if (flagInArgsIndex !== -1) { args.splice(flagInArgsIndex, 0, "--"); } return args; } export function runBundler(options, args, bundlerOpts = {}) { const yarnAndYarnClassic = options.packager === "yarn" && !isYarnModern(options); const result = spawnSync(yarnAndYarnClassic ? "npx" : options.packager, [ yarnAndYarnClassic ? "" : options.packager === "bun" ? "x" : "exec", `edge-functions@${bundlerOpts.version ?? "latest"}`, ...injectPassthroughFlagForArgs(options, [...args].filter((v) => !!v)), ], { shell: true, stdio: bundlerOpts.logging === "error" ? ["ignore", "ignore", "inherit"] : "inherit", env: { ...process.env, ...(bundlerOpts.logging === "error" ? { BUNDLER_LOG: "error" } : undefined), }, }); if (result.status !== 0) { logger.error("Bundler command failed"); process.exit(1); } }