Skip to content

Commit

Permalink
Fix the issue of refetching the vercel package every single time when…
Browse files Browse the repository at this point in the history
… using yarn berry. (#247)

* fix: refetch vercel package with yarn (#240)

* make the code more readable (#240)
  • Loading branch information
noobnooc authored May 18, 2023
1 parent 29b7547 commit 4f43b9b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-flowers-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Fix the issue of refetching the vercel package every single time when using yarn berry.
32 changes: 26 additions & 6 deletions src/buildApplication/buildVercelOutput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFile, mkdir, rm, rmdir } from 'fs/promises';
import { spawn } from 'child_process';
import { spawn, type ChildProcessWithoutNullStreams } from 'child_process';
import { join, resolve } from 'path';
import { cliLog } from '../cli';
import { validateDir, validateFile } from '../utils';
Expand Down Expand Up @@ -85,11 +85,7 @@ async function runVercelBuild(pkgMng: PackageManager): Promise<void> {

cliLog('Building project...');

const vercelBuild = spawn(pkgMngCMD, [
...(pkgMng === 'yarn (berry)' ? ['dlx'] : []),
'next-on-pages-vercel-cli',
'build',
]);
const vercelBuild = await getVercelBuildChildProcess(pkgMng);

vercelBuild.stdout.on('data', data =>
cliLog(`\n${data}`, { fromVercelCli: true })
Expand All @@ -112,6 +108,30 @@ async function runVercelBuild(pkgMng: PackageManager): Promise<void> {
});
}

async function getVercelBuildChildProcess(
pkgMng: PackageManager
): Promise<ChildProcessWithoutNullStreams> {
const pkgMngCMD = getPackageManagerSpawnCommand(pkgMng);
if (pkgMng === 'yarn (berry)') {
const vercelPackageIsInstalled = await isVercelPackageInstalled(pkgMng);
if (!vercelPackageIsInstalled) {
return spawn(pkgMngCMD, ['dlx', 'next-on-pages-vercel-cli', 'build']);
}
}

return spawn(pkgMngCMD, ['next-on-pages-vercel-cli', 'build']);
}

async function isVercelPackageInstalled(
pkgMng: PackageManager
): Promise<boolean> {
const pkgMngCMD = getPackageManagerSpawnCommand(pkgMng);
const infoVercelExitCode = await new Promise(resolve =>
spawn(pkgMngCMD, ['info', 'vercel']).on('exit', resolve)
);
return infoVercelExitCode === 0;
}

/**
* Vercel and Next.js generate *private* files for telemetry purposes that are accessible as static assets (`.vercel/output/static/_next/__private/...`).
*
Expand Down

0 comments on commit 4f43b9b

Please sign in to comment.