Skip to content

Commit

Permalink
Migrate build argument parsing (#11492)
Browse files Browse the repository at this point in the history
Migrate `vc build` to use updated argument parsing helpers.

Adds `try/catch` and `handleError` logic that was previously missing.
  • Loading branch information
erikareads committed Apr 30, 2024
1 parent 52e435a commit d6412be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .changeset/strange-zoos-sit.md
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build/command.ts
Expand Up @@ -10,7 +10,7 @@ export const buildCommand: Command = {
name: 'prod',
description: 'Build a production deployment',
shorthand: null,
type: String,
type: Boolean,
deprecated: false,
},
{
Expand Down
31 changes: 18 additions & 13 deletions packages/cli/src/commands/build/index.ts
Expand Up @@ -43,7 +43,7 @@ import type { VercelConfig } from '@vercel/client';
import pull from '../pull';
import { staticFiles as getFiles } from '../../util/get-files';
import Client from '../../util/client';
import getArgs from '../../util/get-args';
import { parseArguments } from '../../util/get-args';
import cmd from '../../util/output/cmd';
import * as cli from '../../util/pkg-name';
import cliPkg from '../../util/pkg';
Expand All @@ -66,12 +66,13 @@ import {
import { importBuilders } from '../../util/build/import-builders';
import { initCorepack, cleanupCorepack } from '../../util/build/corepack';
import { sortBuilders } from '../../util/build/sort-builders';
import { toEnumerableError } from '../../util/error';
import { handleError, toEnumerableError } from '../../util/error';
import { validateConfig } from '../../util/validate-config';
import { setMonorepoDefaultSettings } from '../../util/build/monorepo';
import { help } from '../help';
import { buildCommand } from './command';
import { scrubArgv } from '../../util/build/scrub-argv';
import { getFlagsSpecification } from '../../util/get-flags-specification';

type BuildResult = BuildResultV2 | BuildResultV3;

Expand Down Expand Up @@ -133,22 +134,26 @@ export default async function main(client: Client): Promise<number> {
process.env.__VERCEL_BUILD_RUNNING = '1';
}

let parsedArgs = null;

const flagsSpecification = getFlagsSpecification(buildCommand.options);

// Parse CLI args
const argv = getArgs(client.argv.slice(2), {
'--output': String,
'--prod': Boolean,
'--yes': Boolean,
'-y': '--yes',
});
try {
parsedArgs = parseArguments(client.argv.slice(2), flagsSpecification);
} catch (error) {
handleError(error);
return 1;
}

if (argv['--help']) {
if (parsedArgs.flags['--help']) {
output.print(help(buildCommand, { columns: client.stderr.columns }));
return 2;
}

// Build `target` influences which environment variables will be used
const target = argv['--prod'] ? 'production' : 'preview';
const yes = Boolean(argv['--yes']);
const target = parsedArgs.flags['--prod'] ? 'production' : 'preview';
const yes = Boolean(parsedArgs.flags['--yes']);

try {
await validateNpmrc(cwd);
Expand Down Expand Up @@ -213,8 +218,8 @@ export default async function main(client: Client): Promise<number> {

// Delete output directory from potential previous build
const defaultOutputDir = join(cwd, projectRootDirectory, OUTPUT_DIR);
const outputDir = argv['--output']
? resolve(argv['--output'])
const outputDir = parsedArgs.flags['--output']
? resolve(parsedArgs.flags['--output'])
: defaultOutputDir;
await Promise.all([
fs.remove(outputDir),
Expand Down

0 comments on commit d6412be

Please sign in to comment.