Skip to content

Commit fea645f

Browse files
dario-piotrowiczvicbjames-elicx
authored
Apply quality of life improvements to the opennextjs-cloudflare CLI (#1097)
--------- Co-authored-by: Victor Berchet <[email protected]> Co-authored-by: James Anderson <[email protected]>
1 parent 3acd272 commit fea645f

8 files changed

Lines changed: 43 additions & 10 deletions

File tree

.changeset/yellow-pears-relax.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

packages/cloudflare/src/cli/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async function buildCommand(
5353
*/
5454
export 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)

packages/cloudflare/src/cli/commands/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu
7474
*/
7575
export 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))

packages/cloudflare/src/cli/commands/populate-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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))

packages/cloudflare/src/cli/commands/preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function previewCommand(
5252
*/
5353
export 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", {

packages/cloudflare/src/cli/commands/upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu
7171
*/
7272
export 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))

packages/cloudflare/src/cli/commands/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff 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

packages/cloudflare/src/cli/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env node
22

3+
import logger from "@opennextjs/aws/logger.js";
34
import yargs from "yargs";
45

6+
import { getVersion } from "./build/utils/version.js";
57
import { addBuildCommand } from "./commands/build.js";
68
import { addDeployCommand } from "./commands/deploy.js";
79
import { addMigrateCommand } from "./commands/migrate.js";
@@ -12,7 +14,22 @@ import { addUploadCommand } from "./commands/upload.js";
1214
export 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);

0 commit comments

Comments
 (0)