Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions package/src/plugin/detectPackageManagerRunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { existsSync } from "node:fs";
import path from "node:path";

type PackageManagerRunner = "npx" | "pnpm exec" | "yarn" | "bunx";

export function detectPackageManagerRunner(): PackageManagerRunner {
// 1. Check environment variable
const packageManagerFromEnv = detectPackageManagerRunnerFromEnv();
if (packageManagerFromEnv) return packageManagerFromEnv;

// 2. Check lock files
const packageManagerFromLockFile = detectPackageManagerRunnerFromLockFiles();
if (packageManagerFromLockFile) return packageManagerFromLockFile;

// 3. Fallback to npx
return "npx";
}

function detectPackageManagerRunnerFromEnv(): PackageManagerRunner | undefined {
const userAgent = process.env.npm_config_user_agent;
if (!userAgent) return;
if (userAgent.startsWith("pnpm")) return "pnpm exec";
if (userAgent.startsWith("yarn")) return "yarn";
if (userAgent.startsWith("bun")) return "bunx";
if (userAgent.startsWith("npm")) return "npx";
}

function detectPackageManagerRunnerFromLockFiles():
| PackageManagerRunner
| undefined {
const cwd = process.cwd();
if (existsSync(path.join(cwd, "pnpm-lock.yaml"))) return "pnpm exec";
if (existsSync(path.join(cwd, "yarn.lock"))) return "yarn";
if (existsSync(path.join(cwd, "bun.lockb"))) return "bunx";
if (existsSync(path.join(cwd, "package-lock.json"))) return "npx";
}
2 changes: 1 addition & 1 deletion package/src/plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("plugin", () => {
await withNextGlobeGenPlugin(CONFIG_PATH)({})("phase-production-build");
expect(spawn).not.toBeCalled();
expect(spawnSync).toHaveBeenCalledWith(
"npx next-globe-gen --plugin --config ./src/__mocks__/i18n.config.ts",
"pnpm exec next-globe-gen --plugin --config ./src/__mocks__/i18n.config.ts",
expect.objectContaining({
stdio: "inherit",
shell: true,
Expand Down
24 changes: 15 additions & 9 deletions package/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createRequire } from "node:module";
import { resolve } from "node:path";
import type { Config, DomainConfig } from "~/config/types";
import { compile } from "~/utils/ts-utils";
import { detectPackageManagerRunner } from "./detectPackageManagerRunner";

type Phase =
| typeof PHASE_EXPORT
Expand Down Expand Up @@ -56,9 +57,10 @@ async function useGenerator(configPath: string, phase: Phase) {
if (nextjsVersion.major >= 16 && phase === "phase-development-server" && !process.env.NEXT_PRIVATE_WORKER) return;
if (nextjsVersion.major < 16 && process.env.NEXT_PRIVATE_WORKER) return;
if (process.env.NEXT_DEPLOYMENT_ID !== undefined) return;
const runner = detectPackageManagerRunner();
try {
if (phase !== "phase-production-server") {
spawnSync(`npx next-globe-gen --plugin --config ${configPath}`, {
spawnSync(`${runner} next-globe-gen --plugin --config ${configPath}`, {
cwd: process.cwd(),
stdio: "inherit",
shell: true,
Expand All @@ -77,16 +79,20 @@ async function useGenerator(configPath: string, phase: Phase) {
abortController.abort();
process.exit();
});
spawn(`npx next-globe-gen --plugin --watch --config ${configPath}`, {
cwd: process.cwd(),
stdio: "inherit",
shell: true,
detached: false,
signal: abortController.signal,
});
spawn(
`${runner} next-globe-gen --plugin --watch --config ${configPath}`,
{
cwd: process.cwd(),
stdio: "inherit",
shell: true,
detached: false,
signal: abortController.signal,
},
);
}
} catch (_e) {
} catch (error) {
console.error("Failed to spawn the NextGlobeGen compiler process");
throw error;
}
}

Expand Down
Loading