Unverified Commit 27c207da authored by Darsh Patel's avatar Darsh Patel Committed by GitHub

Fix: UnhandledPromiseRejectionWarning when unknown flag provided for cli commands (#15422)

parent 91242ca6
...@@ -8,21 +8,26 @@ import { startedDevelopmentServer } from '../build/output' ...@@ -8,21 +8,26 @@ import { startedDevelopmentServer } from '../build/output'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
const nextDev: cliCommand = (argv) => { const nextDev: cliCommand = (argv) => {
const args = arg( const validArgs: arg.Spec = {
{ // Types
// Types '--help': Boolean,
'--help': Boolean, '--port': Number,
'--port': Number, '--hostname': String,
'--hostname': String,
// Aliases
'-h': '--help',
'-p': '--port',
'-H': '--hostname',
},
{ argv }
)
// Aliases
'-h': '--help',
'-p': '--port',
'-H': '--hostname',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
if (args['--help']) { if (args['--help']) {
// tslint:disable-next-line // tslint:disable-next-line
console.log(` console.log(`
......
...@@ -7,22 +7,27 @@ import { printAndExit } from '../server/lib/utils' ...@@ -7,22 +7,27 @@ import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
const nextExport: cliCommand = (argv) => { const nextExport: cliCommand = (argv) => {
const args = arg( const validArgs: arg.Spec = {
{ // Types
// Types '--help': Boolean,
'--help': Boolean, '--silent': Boolean,
'--silent': Boolean, '--outdir': String,
'--outdir': String, '--threads': Number,
'--threads': Number,
// Aliases
'-h': '--help',
'-s': '--silent',
'-o': '--outdir',
},
{ argv }
)
// Aliases
'-h': '--help',
'-s': '--silent',
'-o': '--outdir',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
if (args['--help']) { if (args['--help']) {
// tslint:disable-next-line // tslint:disable-next-line
console.log(` console.log(`
......
...@@ -3,25 +3,31 @@ ...@@ -3,25 +3,31 @@
import { resolve } from 'path' import { resolve } from 'path'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import startServer from '../server/lib/start-server' import startServer from '../server/lib/start-server'
import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
const nextStart: cliCommand = (argv) => { const nextStart: cliCommand = (argv) => {
const args = arg( const validArgs: arg.Spec = {
{ // Types
// Types '--help': Boolean,
'--help': Boolean, '--port': Number,
'--port': Number, '--hostname': String,
'--hostname': String,
// Aliases
'-h': '--help',
'-p': '--port',
'-H': '--hostname',
},
{ argv }
)
// Aliases
'-h': '--help',
'-p': '--port',
'-H': '--hostname',
}
let args: arg.Result<arg.Spec>
try {
args = arg(validArgs, { argv })
} catch (error) {
if (error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
if (args['--help']) { if (args['--help']) {
// tslint:disable-next-line // tslint:disable-next-line
console.log(` console.log(`
......
#!/usr/bin/env node #!/usr/bin/env node
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
const nextTelemetry: cliCommand = (argv) => { const nextTelemetry: cliCommand = (argv) => {
const args = arg( const validArgs: arg.Spec = {
{ // Types
// Types '--help': Boolean,
'--help': Boolean, '--enable': Boolean,
'--enable': Boolean, '--disable': Boolean,
'--disable': Boolean, // Aliases
// Aliases '-h': '--help',
'-h': '--help', }
}, let args: arg.Result<arg.Spec>
{ argv } try {
) args = arg(validArgs, { argv })
} catch (error) {
if (error.code === 'ARG_UNKNOWN_OPTION') {
return printAndExit(error.message, 1)
}
throw error
}
if (args['--help']) { if (args['--help']) {
console.log( console.log(
......
...@@ -87,13 +87,4 @@ describe('Build warnings', () => { ...@@ -87,13 +87,4 @@ describe('Build warnings', () => {
})) }))
expect(stdout).not.toContain('no-cache') expect(stdout).not.toContain('no-cache')
}) })
it('should warn when unknown argument provided', async () => {
const { stderr } = await nextBuild(appDir, ['--random'], { stderr: true })
expect(stderr).toEqual('Unknown or unexpected option: --random\n')
})
it('should not throw UnhandledPromiseRejectionWarning', async () => {
const { stderr } = await nextBuild(appDir, ['--random'], { stderr: true })
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
}) })
...@@ -59,6 +59,19 @@ describe('CLI Usage', () => { ...@@ -59,6 +59,19 @@ describe('CLI Usage', () => {
/Compiles the application for production deployment/ /Compiles the application for production deployment/
) )
}) })
test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['build', '--random'], {
stderr: true,
})
expect(stderr).toEqual('Unknown or unexpected option: --random\n')
})
test('should not throw UnhandledPromiseRejectionWarning', async () => {
const { stderr } = await runNextCommand(['build', '--random'], {
stderr: true,
})
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
}) })
describe('dev', () => { describe('dev', () => {
...@@ -121,6 +134,19 @@ describe('CLI Usage', () => { ...@@ -121,6 +134,19 @@ describe('CLI Usage', () => {
) )
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`)) expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
}) })
test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['dev', '--random'], {
stderr: true,
})
expect(stderr).toEqual('Unknown or unexpected option: --random\n')
})
test('should not throw UnhandledPromiseRejectionWarning', async () => {
const { stderr } = await runNextCommand(['dev', '--random'], {
stderr: true,
})
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
}) })
describe('start', () => { describe('start', () => {
...@@ -137,6 +163,19 @@ describe('CLI Usage', () => { ...@@ -137,6 +163,19 @@ describe('CLI Usage', () => {
}) })
expect(help.stdout).toMatch(/Starts the application in production mode/) expect(help.stdout).toMatch(/Starts the application in production mode/)
}) })
test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['start', '--random'], {
stderr: true,
})
expect(stderr).toEqual('Unknown or unexpected option: --random\n')
})
test('should not throw UnhandledPromiseRejectionWarning', async () => {
const { stderr } = await runNextCommand(['start', '--random'], {
stderr: true,
})
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
}) })
describe('export', () => { describe('export', () => {
...@@ -153,5 +192,51 @@ describe('CLI Usage', () => { ...@@ -153,5 +192,51 @@ describe('CLI Usage', () => {
}) })
expect(help.stdout).toMatch(/Exports the application/) expect(help.stdout).toMatch(/Exports the application/)
}) })
test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['export', '--random'], {
stderr: true,
})
expect(stderr).toEqual('Unknown or unexpected option: --random\n')
})
test('should not throw UnhandledPromiseRejectionWarning', async () => {
const { stderr } = await runNextCommand(['export', '--random'], {
stderr: true,
})
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
})
describe('telemetry', () => {
test('--help', async () => {
const help = await runNextCommand(['telemetry', '--help'], {
stdout: true,
})
expect(help.stdout).toMatch(
/Allows you to control Next\.js' telemetry collection/
)
})
test('-h', async () => {
const help = await runNextCommand(['telemetry', '-h'], {
stdout: true,
})
expect(help.stdout).toMatch(
/Allows you to control Next\.js' telemetry collection/
)
})
test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['telemetry', '--random'], {
stderr: true,
})
expect(stderr).toEqual('Unknown or unexpected option: --random\n')
})
test('should not throw UnhandledPromiseRejectionWarning', async () => {
const { stderr } = await runNextCommand(['telemetry', '--random'], {
stderr: true,
})
expect(stderr).not.toContain('UnhandledPromiseRejectionWarning')
})
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment