Skip to content

refactor CLI command runner to use native NodeJS parseArgs utility #1552

@thescientist13

Description

@thescientist13

Current State

Currently, Greenwood's command runner / entrypoint script uses commander to grab args passing in from the command line to determine the right command to invoke Greenwood with

#!/usr/bin/env node

import program from "commander";
import { run } from "./index.js";

const greenwoodPackageJson = (
  await import(new URL("../package.json", import.meta.url), { with: { type: "json" } })
).default;

let command = "";

console.info("-------------------------------------------------------");
console.info(`Welcome to Greenwood (v${greenwoodPackageJson.version}) ♻️`);
console.info("-------------------------------------------------------");

program
  .version(greenwoodPackageJson.version)
  .arguments("<command>")
  .usage("<command> [options]");

program
  .command("build")
  .description("Build a static site for production.")
  .action((cmd) => {
    command = cmd._name;
  });

// ...

program.parse(process.argv);

if (program.parse.length === 0) {
  program.help();
}

run(command);

Given the very simplistic use case for this within Greenwood, I don't think it warrants an entire dependency for this now that NodeJS has a built-in parseArgs utility

Desired State

From what I can tell, to implement our own version, we would need to supplement some of commander's features, in particular the --help terminal output and version output.

➜  greenwood git:(enhancement/update-CLI-command-descriptions) ✗ node ./packages/cli/src/bin.js --help
-------------------------------------------------------
Welcome to Greenwood (v0.33.0-alpha.5) ♻️
-------------------------------------------------------
Usage: bin <script-mode> [options]

Options:
  -V, --version    output the version number
  -h, --help       output usage information

Commands:
  build            Generate a production build.
  develop          Start a local development server.
  serve            View a production build locally.
  eject [options]  (DEPRECATED) Eject greenwood configurations.

But this should be relatively trivial for us to re-implement by just looking for those arguments ourselves and just outputting the console logs ourselves.

Additional Context

No response

Metadata

Metadata

Assignees

Labels

Type

Projects

Status

✅ Done

Relationships

None yet

Development

No branches or pull requests

Issue actions