Skip to content

Commit

Permalink
Revert "use package-manager-manager library (#468)" (#475)
Browse files Browse the repository at this point in the history
* Revert "use package-manager-manager library (#468)"

This reverts commit 5b4a5b7.

* changeset
  • Loading branch information
james-elicx authored Sep 29, 2023
1 parent a6b9d0e commit 072af87
Show file tree
Hide file tree
Showing 10 changed files with 596 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-lobsters-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Temporarily revert the change to package-manager-manager so that the bug fix in the latest release for new next.js versions can still be releases, while a fix for #474 is investigated in the meantime.
26 changes: 3 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/next-on-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"cookie": "^0.5.0",
"esbuild": "^0.15.3",
"js-yaml": "^4.1.0",
"package-manager-manager": "^0.1.1",
"pcre-to-regexp": "^1.1.0",
"semver": "^7.5.2",
"zod": "^3.21.4",
Expand Down
19 changes: 5 additions & 14 deletions packages/next-on-pages/src/buildApplication/buildApplication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { exit } from 'process';
import { join, resolve } from 'path';
import { getPackageManager } from 'package-manager-manager';
import type { CliOptions } from '../cli';
import { cliError, cliLog, cliSuccess } from '../cli';
import { getVercelConfig } from './getVercelConfig';
Expand All @@ -13,6 +12,7 @@ import {
processVercelOutput,
processOutputDir,
} from './processVercelOutput';
import { getCurrentPackageManager, getExecStr } from './packageManagerUtils';
import { printBuildSummary, writeBuildInfo } from './buildSummary';
import type { ProcessedVercelFunctions } from './processVercelFunctions';
import { processVercelFunctions } from './processVercelFunctions';
Expand All @@ -36,26 +36,17 @@ export async function buildApplication({
| 'watch'
| 'outdir'
>) {
const pm = await getPackageManager();

if (!pm) {
throw new Error('Error: Could not detect current package manager in use');
}
const pm = await getCurrentPackageManager();

let buildSuccess = true;
if (!skipBuild) {
try {
await buildVercelOutput(pm);
await buildVercelOutput();
} catch {
const execStr = await pm.getRunExec('vercel', {
args: ['build'],
download: 'prefer-if-needed',
});
const execStr = await getExecStr(pm, 'vercel');
cliError(
`
The Vercel build ${
execStr ? `(\`${execStr}\`) ` : ''
}command failed. For more details see the Vercel logs above.
The Vercel build (\`${execStr} vercel build\`) command failed. For more details see the Vercel logs above.
If you need help solving the issue, refer to the Vercel or Next.js documentation or their repositories.
`,
{ spaced: true },
Expand Down
85 changes: 46 additions & 39 deletions packages/next-on-pages/src/buildApplication/buildVercelOutput.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { writeFile, mkdir, rm, rmdir } from 'fs/promises';
import { spawn, type ChildProcessWithoutNullStreams } from 'child_process';
import { join, resolve } from 'path';
import { ltr as versionLessThan, coerce } from 'semver';
import type { PackageManager } from 'package-manager-manager';
import { cliLog, cliWarn } from '../cli';
import { readJsonFile, validateDir, validateFile } from '../utils';
import { waitForProcessToClose } from './processUtils';
import type { PackageManager } from './packageManagerUtils';
import {
getCurrentPackageManager,
getExecStr,
getPackageManagerInfo,
getPackageVersion,
waitForProcessToClose,
} from './packageManagerUtils';

/**
* Builds the Next.js output via the Vercel CLI
Expand All @@ -20,33 +25,29 @@ import { waitForProcessToClose } from './processUtils';
* Creates a temporary config file when using the Bun package manager so that Vercel knows to use
* Bun to install and build the project.
*
* @param pm the package manager currently in use
*/
export async function buildVercelOutput(pm: PackageManager): Promise<void> {
cliLog(`Detected Package Manager: ${pm.name} (${pm.version})\n`);
export async function buildVercelOutput(): Promise<void> {
const pm = await getCurrentPackageManager();
cliLog(`Detected Package Manager: ${pm}\n`);

cliLog('Preparing project...');
await generateProjectJsonFileIfNeeded();

let tempVercelConfig: TempVercelConfigInfo | undefined;

if (pm.name === 'bun') {
if (pm === 'bun') {
// Vercel introduced proper Bun support in 32.2.1 and 32.2.4 (for monorepos), therefore we should
// ensure the Vercel CLI has a config file telling it to use Bun for older versions. This is done
// to prevent a breaking change for users who are using an older version of the Vercel CLI.
const vercelInfo = await pm.getPackageInfo('vercel');

if (vercelInfo) {
const vercelVersion = coerce(vercelInfo.version);
if (vercelVersion && versionLessThan(vercelVersion, '32.2.4')) {
cliWarn(
'Vercel CLI version is < 32.2.4, creating temporary config for Bun support...',
);
tempVercelConfig = await createTempVercelConfig({
buildCommand: 'bun run build',
installCommand: 'bun install',
});
}
const vercelVersion = await getPackageVersion('vercel', pm);
if (vercelVersion && vercelVersion < '32.2.4') {
cliWarn(
'Vercel CLI version is < 32.2.4, creating temporary config for Bun support...',
);
tempVercelConfig = await createTempVercelConfig({
buildCommand: 'bun run build',
installCommand: 'bun install',
});
}
}

Expand All @@ -57,11 +58,8 @@ export async function buildVercelOutput(pm: PackageManager): Promise<void> {
await rm(tempVercelConfig.tempPath);
}

const execStr = await pm.getRunExec('vercel', {
args: ['build'],
download: 'prefer-if-needed',
});
cliLog(`Completed \`${execStr}\`.`);
const execStr = await getExecStr(pm, 'vercel');
cliLog(`Completed \`${execStr} vercel build\`.`);
}

/**
Expand Down Expand Up @@ -128,18 +126,20 @@ type VercelProjectJson = {
type TempVercelConfigInfo = { additionalArgs: string[]; tempPath: string };

async function runVercelBuild(
pm: PackageManager,
pkgMng: PackageManager,
additionalArgs: string[] = [],
): Promise<void> {
if (pm.name === 'yarn' && pm.version.startsWith('1.')) {
const vercelInfo = await pm.getPackageInfo('vercel');
const { pm, baseCmd } = await getPackageManagerInfo(pkgMng);

if (pm === 'yarn (classic)') {
const vercelVersion = await getPackageVersion('vercel', pkgMng);

if (!vercelInfo) {
if (!vercelVersion) {
cliLog(
`vercel dev dependency missing, installing vercel as a dev dependency with '${pm.name} add vercel -D'...`,
`vercel dev dependency missing, installing vercel as a dev dependency with '${baseCmd} add vercel -D'...`,
);

const installVercel = spawn(pm.name, ['add', 'vercel', '-D']);
const installVercel = spawn(baseCmd, ['add', 'vercel', '-D']);

logVercelProcessOutput(installVercel);

Expand All @@ -159,19 +159,26 @@ async function runVercelBuild(
}

async function getVercelBuildChildProcess(
pm: PackageManager,
pkgMng: PackageManager,
additionalArgs: string[] = [],
): Promise<ChildProcessWithoutNullStreams> {
const spawnCmd = await pm.getRunExecStruct('vercel', {
args: ['build', ...additionalArgs],
download: 'prefer-if-needed',
});
const { pm, baseCmd, execCmd, execArgs, dlxOrExec } =
await getPackageManagerInfo(pkgMng);

let dlxArgs: string[] = [];

if (!spawnCmd) {
throw new Error('Error: Failed to generate vercel build command');
if (dlxOrExec) {
const vercelPackageIsInstalled = await getPackageVersion('vercel', pm);
dlxArgs = dlxOrExec(!vercelPackageIsInstalled);
}

return spawn(spawnCmd.cmd, spawnCmd.cmdArgs);
return spawn(execCmd ?? baseCmd, [
...dlxArgs,
...(execArgs ?? []),
'vercel',
'build',
...additionalArgs,
]);
}

/**
Expand Down
Loading

0 comments on commit 072af87

Please sign in to comment.