Skip to content

Commit

Permalink
refactor(cli): removes dependency 'semver'
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Dec 22, 2024
1 parent 63d8b46 commit e6cc9e7
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 60 deletions.
19 changes: 2 additions & 17 deletions dist/cli/cli.mjs

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

2 changes: 1 addition & 1 deletion package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"ajv": "8.17.1",
"fast-xml-parser": "4.5.1",
"he": "1.2.0",
"semver": "^7.6.2",
"traverse": "0.6.8"
},
"devDependencies": {
Expand Down
28 changes: 2 additions & 26 deletions src/cli/cli.mts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
/* eslint-disable max-lines-per-function */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { readFileSync } from "node:fs";
import { type Writable } from "node:stream";
import { parseArgs } from "node:util";
import satisfies from "semver/functions/satisfies.js";
import { version } from "../version.mjs";
import { formatError, displayLicense, transform } from "./actions.mjs";
import normalize from "./normalize.mjs";
import validations from "./validations.mjs";

const $package = JSON.parse(
// eslint-disable-next-line security/detect-non-literal-fs-filename
readFileSync(new URL("../../package.json", import.meta.url), "utf8"),
);

const HELP_TEXT = `Usage: smcat [options] [infile]
Write beautiful state charts - https://github.com/sverweij/state-machine-cat
Expand Down Expand Up @@ -160,20 +154,6 @@ function parseArguments(pArguments: string[]): {
return { values: camelizeObject(values), positionals };
}

function assertNodeVersion(
pCurrentNodeVersion: string,
pSupportedEngines: string,
) {
/* c8 ignore start */
if (!satisfies(pCurrentNodeVersion, pSupportedEngines)) {
throw new Error(
`\nERROR: your node version (${pCurrentNodeVersion}) is not recent enough.\n` +
` state-machine-cat is supported on node ${pSupportedEngines}\n\n`,
);
}
/* c8 ignore stop */
}

interface ICommandLineOptions {
currentNodeVersion: string;
supportedEngines: string;
Expand All @@ -186,15 +166,11 @@ export default async function cli(
pOptions?: Partial<ICommandLineOptions>,
) {
const lOptions = {
currentNodeVersion: process.versions.node,
supportedEngines: $package.engines.node,
outStream: process.stdout,
errorStream: process.stderr,
...pOptions,
};
try {
assertNodeVersion(lOptions.currentNodeVersion, lOptions.supportedEngines);

// eslint-disable-next-line no-magic-numbers
const { values, positionals } = parseArguments(pArguments.slice(2));

Expand All @@ -203,7 +179,7 @@ export default async function cli(
return;
}
if (values.version) {
(lOptions.outStream as Writable).write(`${$package.version}\n`, "utf8");
(lOptions.outStream as Writable).write(`${version}\n`, "utf8");
return;
}
if (values.license) {
Expand Down
15 changes: 0 additions & 15 deletions test/cli/cli.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,4 @@ describe("#cli - execute-command-line", () => {
errorStream: lErrorStream,
});
});

it("shows an error on stdout when state-machine-cat doesn't support the node version", async () => {
const lOutStream = new WritableTestStream();
const lErrorStream = new WritableTestStream([
/ERROR: your node version \(10.0.0\) is not recent enough./,
/state-machine-cat is supported on node >=20/,
]);

await cli(["node", "smcat.js", "--license"], {
outStream: lOutStream,
errorStream: lErrorStream,
currentNodeVersion: "10.0.0",
supportedEngines: ">=20",
});
});
});

0 comments on commit e6cc9e7

Please sign in to comment.