Skip to content

Commit

Permalink
Switch to zodparser for parsing command line args (#142)
Browse files Browse the repository at this point in the history
* Switch to zodparser for parsing command line args

* Format package.json and package-lock.json

* Update package.json

Co-authored-by: Dario Piotrowicz <[email protected]>

* Updating a comment

* Adding aliases

* Adding aliases to the help text....

* apply minor tweaks

---------

Co-authored-by: Dario Piotrowicz <[email protected]>
  • Loading branch information
jculvey and dario-piotrowicz authored Apr 3, 2023
1 parent 70b2e80 commit dcbbb9a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 40 deletions.
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",
"build:watch": "npm run build -- --watch",
"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
64 changes: 40 additions & 24 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,67 @@
import dedent from 'dedent-tabs';

export type CliOptions = {
help: boolean;
watch: boolean;
skipBuild: boolean;
experimentalMinify: boolean;
version: boolean;
};
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>;

/**
* parses the options provided to the CLI
*
* TODO: this should replaced by yargs or similar
*
* @returns the provided options
*/
export function getCliOptions(): CliOptions {
return {
help: process.argv.includes('--help'),
watch: process.argv.includes('--watch'),
skipBuild: process.argv.includes('--skip-build'),
experimentalMinify: process.argv.includes('--experimental-minify'),
version: process.argv.includes('--version'),
};
export function parseCliArgs() {
return argumentParser({
options: cliOptions,
aliases: {
h: 'help',
v: 'version',
s: 'skipBuild',
e: 'experimentalMinify',
w: 'watch',
},
}).parse(process.argv.slice(2));
}

/**
* Prints the help message that users get when they provide the help option
*
* TODO: we should use yargs or similar and get this autogenerated instead
* Note: this should soon be available by zodcli and not be needed anymore
*/
export function printCliHelpMessage(): void {
cliLog(`
Usage: npx @cloudflare/next-on-pages [options]
Options:
--help: Shows this help message
--help, -h: Shows this help message
--version: Shows the version of the package
--version, -v: Shows the version of the package
--skip-build: Doesn't run 'vercel build' automatically
--skip-build, -s: Doesn't run 'vercel build' automatically
--experimental-minify: Attempts to minify the functions of a project (by de-duping webpack chunks)
--experimental-minify, -m: Attempts to minify the functions of a project (by de-duping webpack chunks)
--watch: Automatically rebuilds when the project is edited
--watch, -w: Automatically rebuilds when the project is edited
GitHub: https://github.com/cloudflare/next-on-pages
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 set, run in watch mode
if (args.watch) {
setWatchMode(() => runBuild(args));
}
}

Expand Down

0 comments on commit dcbbb9a

Please sign in to comment.