Skip to content

Commit

Permalink
Add node version validations & support for env var use (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcampos authored Jul 23, 2024
1 parent 9807d60 commit fdad5fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
"chalk": "^5.1.2",
"dotenv": "^16.4.5",
"mime-types": "^2.1.35",
"ora": "^8.0.1",
"pino": "^9.3.1",
"pino-pretty": "^11.2.1",
"ora": "^8.0.1",
"semver": "^7.5.2",
"yargs": "^17.7.2"
},
"files": [
Expand Down
42 changes: 40 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,50 @@ import dotenv from "dotenv";
import { hideBin } from "yargs/helpers";
import yargs from "yargs";
import lint from "./cmds/lint.js";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { printCriticalFailureToConsoleAndExit } from "./common/output.js";
import { gte } from "semver";

dotenv.config();

void yargs(hideBin(process.argv))
const MIN_NODE_VERSION = "20.0.0";

if (gte(MIN_NODE_VERSION, process.versions.node)) {
await printCriticalFailureToConsoleAndExit(
`The Rate My OpenAPI CLI requires at least node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}. Please update your version of node.js.
Consider using a Node.js version manager such as https://github.com/nvm-sh/nvm.`,
);
}

let packageJson;
try {
packageJson = JSON.parse(
readFileSync(
fileURLToPath(new URL("../package.json", import.meta.url)),
"utf-8",
),
);
} catch (e) {
await printCriticalFailureToConsoleAndExit(
`Unable to load rmoa. The package.json is missing or malformed.`,
);
}

const cli = yargs(hideBin(process.argv))
.env()
.command(lint)
.demandCommand()
.strictCommands()
.version()
.help().argv;
.version(packageJson?.version)
.fail(false)
.help();

try {
await cli.argv;
} catch (err) {
await printCriticalFailureToConsoleAndExit(err.message ?? err);
cli.showHelp();
}

0 comments on commit fdad5fe

Please sign in to comment.