File tree Expand file tree Collapse file tree
packages/cloudflare/src/cli Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " @opennextjs/cloudflare " : patch
3+ ---
4+
5+ Add ` --help ` and ` --version ` to the ` opennextjs-cloudflare ` CLI
6+
7+ Improve the ` opennextjs-cloudflare ` CLI by:
8+
9+ - ensuring that unknown commands (e.g., ` opennextjs-cloudflare foo ` ) display a clear and helpful error message
10+ - adding a ` -h ` |` --help ` flag to display the CLI's help message
11+ - adding a ` -v ` |` --version ` flag to display the package's version
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ async function buildCommand(
5353 */
5454export function addBuildCommand < T extends yargs . Argv > ( y : T ) {
5555 return y . command (
56- "build" ,
56+ "build [args..] " ,
5757 "Build an OpenNext Cloudflare worker" ,
5858 ( c ) =>
5959 withWranglerOptions ( c )
Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu
7474 */
7575export function addDeployCommand < T extends yargs . Argv > ( y : T ) {
7676 return y . command (
77- "deploy" ,
77+ "deploy [args..] " ,
7878 "Deploy a built OpenNext app to Cloudflare Workers" ,
7979 ( c ) => withPopulateCacheOptions ( c ) ,
8080 ( args ) => deployCommand ( withWranglerPassthroughArgs ( args ) )
Original file line number Diff line number Diff line change @@ -389,13 +389,13 @@ export function addPopulateCacheCommand<T extends yargs.Argv>(y: T) {
389389 return y . command ( "populateCache" , "Populate the cache for a built Next.js app" , ( c ) =>
390390 c
391391 . command (
392- "local" ,
392+ "local [args..] " ,
393393 "Local dev server cache" ,
394394 ( c ) => withPopulateCacheOptions ( c ) ,
395395 ( args ) => populateCacheCommand ( "local" , withWranglerPassthroughArgs ( args ) )
396396 )
397397 . command (
398- "remote" ,
398+ "remote [args..] " ,
399399 "Remote Cloudflare Worker cache" ,
400400 ( c ) => withPopulateCacheOptions ( c ) ,
401401 ( args ) => populateCacheCommand ( "remote" , withWranglerPassthroughArgs ( args ) )
Original file line number Diff line number Diff line change @@ -52,7 +52,7 @@ export async function previewCommand(
5252 */
5353export function addPreviewCommand < T extends yargs . Argv > ( y : T ) {
5454 return y . command (
55- "preview" ,
55+ "preview [args..] " ,
5656 "Preview a built OpenNext app with a Wrangler dev server" ,
5757 ( c ) =>
5858 withPopulateCacheOptions ( c ) . option ( "remote" , {
Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu
7171 */
7272export function addUploadCommand < T extends yargs . Argv > ( y : T ) {
7373 return y . command (
74- "upload" ,
74+ "upload [args..] " ,
7575 "Upload a built OpenNext app to Cloudflare Workers" ,
7676 ( c ) => withPopulateCacheOptions ( c ) ,
7777 ( args ) => uploadCommand ( withWranglerPassthroughArgs ( args ) )
Original file line number Diff line number Diff line change @@ -142,7 +142,12 @@ type WranglerInputArgs = {
142142 * @param args
143143 * @returns An array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
144144 */
145- function getWranglerArgs ( args : WranglerInputArgs & { _ : ( string | number ) [ ] } ) : string [ ] {
145+ function getWranglerArgs (
146+ args : WranglerInputArgs & {
147+ _ : ( string | number ) [ ] ;
148+ args ?: ( string | number ) [ ] ;
149+ }
150+ ) : string [ ] {
146151 if ( args . configPath ) {
147152 logger . warn ( "The `--configPath` flag is deprecated, please use `--config` instead." ) ;
148153
@@ -159,8 +164,8 @@ function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }):
159164 ...( args . config ? [ "--config" , args . config ] : [ ] ) ,
160165 ...( args . env ? [ "--env" , args . env ] : [ ] ) ,
161166 ...( args . remote ? [ "--remote" ] : [ ] ) ,
162- // Note: the first args in `_` will be the commands .
163- ...args . _ . slice ( args . _ [ 0 ] === "populateCache" ? 2 : 1 ) . map ( ( a ) => `${ a } ` ) ,
167+ // Note: the ` args` array contains unrecognised flags .
168+ ...( args . args ? .map ( ( a ) => `${ a } ` ) ?? [ ] ) ,
164169 ] ;
165170}
166171
Original file line number Diff line number Diff line change 11#!/usr/bin/env node
22
3+ import logger from "@opennextjs/aws/logger.js" ;
34import yargs from "yargs" ;
45
6+ import { getVersion } from "./build/utils/version.js" ;
57import { addBuildCommand } from "./commands/build.js" ;
68import { addDeployCommand } from "./commands/deploy.js" ;
79import { addMigrateCommand } from "./commands/migrate.js" ;
@@ -12,7 +14,22 @@ import { addUploadCommand } from "./commands/upload.js";
1214export function runCommand ( ) {
1315 const y = yargs ( process . argv . slice ( 2 ) . filter ( ( arg ) => arg !== "--" ) )
1416 . scriptName ( "opennextjs-cloudflare" )
15- . parserConfiguration ( { "unknown-options-as-args" : true } ) ;
17+ . parserConfiguration ( { "unknown-options-as-args" : true } )
18+ . strictCommands ( )
19+ . help ( )
20+ . alias ( "h" , "help" )
21+ . version ( getVersion ( ) . cloudflare )
22+ . alias ( "v" , "version" )
23+ . fail ( ( msg , err , yargs ) => {
24+ if ( msg ) {
25+ logger . error ( `${ msg } \n` ) ;
26+ }
27+ if ( err ) {
28+ throw err ;
29+ }
30+ yargs . showHelp ( ) ;
31+ process . exit ( 1 ) ;
32+ } ) ;
1633
1734 addBuildCommand ( y ) ;
1835 addPreviewCommand ( y ) ;
You can’t perform that action at this time.
0 commit comments