Skip to content

Commit

Permalink
Improve vercel build error msg (#201)
Browse files Browse the repository at this point in the history
* improve vercel build error message

* add changeset

* run prettier:fix

* update varcel build error message and add new getCurrentPackageExecuter utility

* add missing newlines

* refactor getCurrentPackageManager tests and add getCurrentPackageExecuter tests

* add os to getCurrentPackageManager tests

* apply refactoring to package manager utils
  • Loading branch information
dario-piotrowicz authored Apr 27, 2023
1 parent 23aa2df commit a680db6
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 104 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-wolves-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

improve the error message shown when the Vercel build fails to make clearer that the issue is not next-on-pages related
4 changes: 2 additions & 2 deletions package-lock.json

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

10 changes: 7 additions & 3 deletions src/buildApplication/buildApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getVercelStaticAssets,
processVercelOutput,
} from './processVercelOutput';
import { getCurrentPackageExecuter } from './packageManagerUtils';

/**
* Builds the _worker.js with static assets implementing the Next.js application
Expand All @@ -32,10 +33,13 @@ export async function buildApplication({
if (!skipBuild) {
try {
await buildVercelOutput();
} catch (err) {
} catch {
cliError(
(err as Error)?.message ??
'Error: The Vercel build failed. For more details see the Vercel logs above.'
`
The Vercel build (\`${await getCurrentPackageExecuter()} 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 }
);
buildSuccess = false;
}
Expand Down
13 changes: 8 additions & 5 deletions src/buildApplication/buildVercelOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { spawn } from 'child_process';
import { join, resolve } from 'path';
import { cliLog } from '../cli';
import { validateDir, validateFile } from '../utils';
import { getCurrentPackageManager } from './getCurrentPackageManager';
import type { PackageManager } from '../utils/getSpawnCommand';
import { getSpawnCommand } from '../utils/getSpawnCommand';
import type { PackageManager } from './packageManagerUtils';
import {
getCurrentPackageExecuter,
getCurrentPackageManager,
getPackageManagerSpawnCommand,
} from './packageManagerUtils';

/**
* Builds the Next.js output via the Vercel CLI
Expand All @@ -25,7 +28,7 @@ export async function buildVercelOutput(): Promise<void> {
await generateProjectJsonFileIfNeeded();
cliLog('Project is ready');
await runVercelBuild(pkgMng);
cliLog('Completed `npx vercel build`.\n');
cliLog(`Completed \`${await getCurrentPackageExecuter()} vercel build\`.`);
}

/**
Expand All @@ -44,7 +47,7 @@ async function generateProjectJsonFileIfNeeded(): Promise<void> {
}

async function runVercelBuild(pkgMng: PackageManager): Promise<void> {
const pkgMngCMD = getSpawnCommand(pkgMng);
const pkgMngCMD = getPackageManagerSpawnCommand(pkgMng);

if (pkgMng === 'yarn (classic)') {
cliLog(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import YAML from 'js-yaml';
import { spawn } from 'child_process';
import { readFile } from 'fs/promises';
import type { PackageManager } from '../utils';
import { validateFile, getSpawnCommand } from '../utils';
import { cliError } from '../cli';
import { validateFile } from '../utils';

export async function getCurrentPackageManager(): Promise<PackageManager> {
const userAgent = process.env.npm_config_user_agent;
Expand All @@ -14,7 +13,7 @@ export async function getCurrentPackageManager(): Promise<PackageManager> {
if ((userAgent && userAgent.startsWith('pnpm')) || hasPnpmLock) return 'pnpm';

if ((userAgent && userAgent.startsWith('yarn')) || hasYarnLock) {
const yarn = getSpawnCommand('yarn');
const yarn = getPackageManagerSpawnCommand('yarn');
const getYarnV = spawn(yarn, ['-v']);
let yarnV = '';
getYarnV.stdout.on('data', data => {
Expand Down Expand Up @@ -49,3 +48,41 @@ export async function getCurrentPackageManager(): Promise<PackageManager> {
}
return 'npm';
}

export async function getCurrentPackageExecuter(): Promise<string> {
const cmd = isWindows() ? '.cmd' : '';
const packageManager = await getCurrentPackageManager();
switch (packageManager) {
case 'npm':
return `npx${cmd}`;
case 'pnpm':
return `pnpx${cmd}`;
case 'yarn (berry)':
return `yarn${cmd} dlx`;
case 'yarn (classic)':
return `yarn${cmd}`;
default:
return `npx${cmd}`;
}
}

const packageManagers = {
pnpm: 'pnpx',
'yarn (berry)': 'yarn',
'yarn (classic)': 'yarn',
yarn: 'yarn',
npm: 'npx',
};

export type PackageManager = keyof typeof packageManagers;

export function getPackageManagerSpawnCommand(
pkgMng: keyof typeof packageManagers
): string {
const winCMD = isWindows() ? '.cmd' : '';
return `${packageManagers[pkgMng]}${winCMD}`;
}

function isWindows(): boolean {
return process.platform === 'win32';
}
13 changes: 8 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { z } from 'zod';
import { argumentParser } from 'zodcli';
import type { ChalkInstance } from 'chalk';
import chalk from 'chalk';
import { getCurrentPackageManager } from './buildApplication/getCurrentPackageManager';
import { getSpawnCommand, nextOnPagesVersion } from './utils';
import type { PackageManager } from './utils';
import { nextOnPagesVersion } from './utils';
import type { PackageManager } from './buildApplication/packageManagerUtils';
import {
getCurrentPackageManager,
getPackageManagerSpawnCommand,
} from './buildApplication/packageManagerUtils';

// A helper type to handle command line flags. Defaults to false
const flag = z
Expand Down Expand Up @@ -219,7 +222,7 @@ export async function printEnvInfo(): Promise<void> {

function getPackageVersion(packageName: string): string {
try {
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const command = getPackageManagerSpawnCommand('npm');
const commandOutput = execFileSync(
command,
['list', packageName, '--json', '--depth=0'],
Expand All @@ -237,7 +240,7 @@ function getPackageVersion(packageName: string): string {
function getBinaryVersion(binaryName: PackageManager): string {
const commandArgs = ['--version'];
try {
return execFileSync(getSpawnCommand(binaryName), commandArgs)
return execFileSync(getPackageManagerSpawnCommand(binaryName), commandArgs)
.toString()
.trim();
} catch {
Expand Down
14 changes: 0 additions & 14 deletions src/utils/getSpawnCommand.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './fs';
export * from './version';
export * from './getSpawnCommand';
export * from './routing';
71 changes: 0 additions & 71 deletions tests/src/buildApplication/getCurrentPackageManager.test.ts

This file was deleted.

Loading

0 comments on commit a680db6

Please sign in to comment.