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

Switch to zodparser for parsing command line args #142

Merged
merged 7 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/eleven-cars-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Parses cli arguments with the `zodcli` package
48 changes: 41 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "eslint src",
"types-check": "tsc --noEmit",
"build": "npx esbuild --bundle --platform=node ./src/index.ts --external:esbuild --external:chokidar --outfile=./dist/index.js",
"watch": "npm run build -- --watch",
jculvey marked this conversation as resolved.
Show resolved Hide resolved
"prepare": "npm run build",
"publish": "npm run build && npx changeset publish",
"changeset": "npx changeset",
Expand All @@ -24,7 +25,9 @@
"astring": "^1.8.4",
"chokidar": "^3.5.3",
"cookie": "^0.5.0",
"esbuild": "^0.15.3"
"esbuild": "^0.15.3",
"zod": "^3.21.4",
"zodcli": "^0.0.4"
},
"peerDependencies": {
"vercel": "^28.0.2"
Expand Down
38 changes: 31 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
import dedent from 'dedent-tabs';
import { z } from 'zod';
import { argumentParser } from 'zodcli';

// A helper type to handle command line flags. Defaults to false
const flag = z
.union([
z.literal('true').transform(() => true),
z.literal('false').transform(() => false),
z.null().transform(() => true),
])
.default('false');

const cliOptions = z
.object({
help: flag,
watch: flag,
skipBuild: flag,
experimentalMinify: flag,
version: flag,
})
.strict();

export type CliOptions = z.infer<typeof cliOptions>;

export type CliOptions = {
help: boolean;
watch: boolean;
skipBuild: boolean;
experimentalMinify: boolean;
version: boolean;
};
/**
* parses the options provided to the CLI
*
* @returns the provided options
*/
export function parseCliArgs() {
return argumentParser({ options: cliOptions }).parse(process.argv.slice(2));
}

/**
* parses the options provided to the CLI
Expand Down
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import { watch } from 'chokidar';
import pLimit from 'p-limit';
import { cliLog, CliOptions, getCliOptions, printCliHelpMessage } from './cli';
import { cliLog, CliOptions, parseCliArgs, printCliHelpMessage } from './cli';
import { buildApplication } from './buildApplication';
import { nextOnPagesVersion } from './utils';

const limit = pLimit(1);

const cliOptions = getCliOptions();
runNextOnPages(cliOptions);
runNextOnPages();

function runNextOnPages(options: CliOptions): void {
if (options.version) {
function runNextOnPages(): void {
const args = parseCliArgs();

if (args.version) {
// eslint-disable-next-line no-console -- for the version lets simply print it plainly
console.log(nextOnPagesVersion);
return;
}

cliLog(`@cloudflare/next-on-pages CLI v.${nextOnPagesVersion}`);

if (options.help) {
if (args.help) {
printCliHelpMessage();
return;
}

if (options.watch) {
setWatchMode(() => runBuild(options));
// Run the build once
runBuild(args);

// If the watch flag is thrown, run in watch mode
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
if (args.watch) {
setWatchMode(() => runBuild(args));
}
}

Expand Down