Skip to content

Commit

Permalink
chore(release): 2.0.1 [skip ci]
Browse files Browse the repository at this point in the history
## [2.0.1](v2.0.0...v2.0.1) (2024-07-29)

### Bug Fixes

* force new version rollout ([888fecf](888fecf))
* try again to push new version ([bfc34d8](bfc34d8))
  • Loading branch information
semantic-release-bot committed Jul 29, 2024
1 parent bfc34d8 commit 28015f4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
70 changes: 70 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#! /usr/bin/env node
import * as fs from "node:fs";
import chalk from "chalk";
import { program } from "commander";
import { dump, load } from "js-yaml";
import { request } from "undici";
import { z } from "zod";
console.log(chalk.blue(`${chalk.blue.bold("OpenAPI Endpoint Trimmer")} ${chalk.gray("v" + process.env.npm_package_version)}`));
program
.name("openapi-endpoint-trimmer")
.description("OpenAPI Endpoint Trimmer.")
.option("-i, --input <input>", "Input File (Local or Absolute Path). (Required: Either this or --url).")
.option("-u, --url <URL>", "Input URL")
.option("-o, --output <output>", "Output File")
.option("-v, --version", "Display the current version.")
.option("-p, --prefixes <path>", "A comma-separated, zero-spaces list of paths to keep. (Ex. /api/v1/users,/api/v1/organizations)")
.option("--help", "Display all flags, commands, and descriptions.");
program.parse();
if (program.opts().help) {
program.help();
process.exit(0);
}
if (program.opts().version) {
console.log(process.env.npm_package_version);
process.exit(0);
}
const options = z
.object({
input: z.string().optional(),
url: z.string().url().optional(),
output: z.string(),
prefixes: z.string(),
})
.refine((data) => {
if ((data.input && data.url) || (!data.input && !data.url)) {
throw new Error("Please specify either an input file or a URL (exactly one).");
}
return true;
})
.parse(program.opts());
let data;
if (options.input) {
data = fs.readFileSync(options.input, "utf8");
}
else if (options.url) {
const response = await request(options.url);
if (response.statusCode !== 200) {
throw new Error(`Received a non-200 response when downloading from ${options.url}. Received ${response.statusCode}. Please double check your setup.`);
}
data = await response.body.text();
}
else {
throw new Error(`Found neither an input URL or an input file!`);
}
const prefixes = options.prefixes.split(",");
console.log(chalk.gray(`Trimming to just paths ${prefixes.join(", ")}...`));
let parsed = load(data);
const paths = {};
for (const path of Object.keys(parsed.paths)) {
if (prefixes.some((retain) => path.startsWith(retain))) {
paths[path] = parsed.paths[path];
}
}
parsed = {
...parsed,
paths,
};
const filePath = options.output ?? (options.input ?? options.url) + "-trimmed.yaml";
fs.writeFileSync(filePath, dump(parsed));
console.log(`Output To: ${filePath}`);
4 changes: 2 additions & 2 deletions package-lock.json

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

0 comments on commit 28015f4

Please sign in to comment.