Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: extract common get-validated-args #53165

Merged
merged 2 commits into from
Jul 25, 2023
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
12 changes: 3 additions & 9 deletions packages/next/src/cli/next-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import build from '../build'
import { printAndExit } from '../server/lib/utils'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { getValidatedArgs } from '../lib/get-validated-args'

const nextBuild: CliCommand = (argv) => {
const validArgs: arg.Spec = {
Expand All @@ -25,15 +26,8 @@ const nextBuild: CliCommand = (argv) => {
'-d': '--debug',
}

let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs(validArgs, argv)

if (args['--help']) {
printAndExit(
`
Expand Down
12 changes: 2 additions & 10 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { startServer, StartServerOptions } from '../server/lib/start-server'
import { getPort, printAndExit } from '../server/lib/utils'
import * as Log from '../build/output/log'
import { CliCommand } from '../lib/commands'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { CONFIG_FILES, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import path from 'path'
Expand All @@ -20,6 +19,7 @@ import Watchpack from 'watchpack'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { getPossibleInstrumentationHookFilenames } from '../build/worker'
import { resetEnv } from '@next/env'
import { getValidatedArgs } from '../lib/get-validated-args'

let dir: string
let config: NextConfigComplete
Expand Down Expand Up @@ -131,15 +131,7 @@ const nextDev: CliCommand = async (argv) => {
'-p': '--port',
'-H': '--hostname',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs(validArgs, argv)
if (args['--help']) {
console.log(`
Description
Expand Down
12 changes: 2 additions & 10 deletions packages/next/src/cli/next-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as Log from '../build/output/log'
import { printAndExit } from '../server/lib/utils'
import { CliCommand } from '../lib/commands'
import { trace } from '../trace'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { getValidatedArgs } from '../lib/get-validated-args'

const nextExport: CliCommand = (argv) => {
const nextExportCliSpan = trace('next-export-cli')
Expand All @@ -25,15 +25,7 @@ const nextExport: CliCommand = (argv) => {
'-o': '--outdir',
'-s': '--silent',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs(validArgs, argv)
if (args['--help']) {
console.log(`
Description
Expand Down
13 changes: 2 additions & 11 deletions packages/next/src/cli/next-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import arg from 'next/dist/compiled/arg/index.js'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}
import { printAndExit } from '../server/lib/utils'
import { CliCommand } from '../lib/commands'
import isError from '../lib/is-error'
import { PHASE_INFO } from '../shared/lib/constants'
import loadConfig from '../server/config'
import { getValidatedArgs } from '../lib/get-validated-args'

const dir = process.cwd()

Expand Down Expand Up @@ -50,15 +49,7 @@ const nextInfo: CliCommand = async (argv) => {
// Aliases
'-h': '--help',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs(validArgs, argv)

if (args['--help']) {
console.log(
Expand Down
13 changes: 3 additions & 10 deletions packages/next/src/cli/next-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import loadConfig from '../server/config'
import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
import { eventLintCheckCompleted } from '../telemetry/events'
import { CompileError } from '../lib/compile-error'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { findPagesDir } from '../lib/find-pages-dir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { getValidatedArgs } from '../lib/get-validated-args'

const eslintOptions = (args: arg.Spec, defaultCacheLocation: string) => ({
overrideConfigFile: args['--config'] || null,
Expand Down Expand Up @@ -93,15 +93,8 @@ const nextLint: CliCommand = async (argv) => {
'-o': '--output-file',
}

let args: arg.Result<arg.Spec>
try {
args = arg({ ...validArgs, ...validEslintArgs }, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs({ ...validArgs, ...validEslintArgs }, argv)

if (args['--help']) {
printAndExit(
`
Expand Down
12 changes: 2 additions & 10 deletions packages/next/src/cli/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import arg from 'next/dist/compiled/arg/index.js'
import { startServer } from '../server/lib/start-server'
import { getPort, printAndExit } from '../server/lib/utils'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { CliCommand } from '../lib/commands'
import { resolve } from 'path'
import { PHASE_PRODUCTION_SERVER } from '../shared/lib/constants'
import loadConfig from '../server/config'
import { getValidatedArgs } from '../lib/get-validated-args'

const nextStart: CliCommand = async (argv) => {
const validArgs: arg.Spec = {
Expand All @@ -23,15 +23,7 @@ const nextStart: CliCommand = async (argv) => {
'-p': '--port',
'-H': '--hostname',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs(validArgs, argv)
if (args['--help']) {
console.log(`
Description
Expand Down
13 changes: 2 additions & 11 deletions packages/next/src/cli/next-telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env node
import chalk from 'next/dist/compiled/chalk'
import arg from 'next/dist/compiled/arg/index.js'
import { printAndExit } from '../server/lib/utils'
import { CliCommand } from '../lib/commands'
import { Telemetry } from '../telemetry/storage'
import isError from '../lib/is-error'
import { getValidatedArgs } from '../lib/get-validated-args'

const nextTelemetry: CliCommand = (argv) => {
const validArgs: arg.Spec = {
Expand All @@ -15,15 +14,7 @@ const nextTelemetry: CliCommand = (argv) => {
// Aliases
'-h': '--help',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
const args = getValidatedArgs(validArgs, argv)

if (args['--help']) {
console.log(
Expand Down
16 changes: 16 additions & 0 deletions packages/next/src/lib/get-validated-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import arg from 'next/dist/compiled/arg/index.js'
import { printAndExit } from '../server/lib/utils'
import isError from './is-error'

export function getValidatedArgs(validArgs: arg.Spec, argv?: string[]) {
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
printAndExit(error.message, 1)
}
throw error
}
return args
}