Skip to content

Commit 28015f4

Browse files
chore(release): 2.0.1 [skip ci]
## [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))
1 parent bfc34d8 commit 28015f4

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

dist/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#! /usr/bin/env node
2+
import * as fs from "node:fs";
3+
import chalk from "chalk";
4+
import { program } from "commander";
5+
import { dump, load } from "js-yaml";
6+
import { request } from "undici";
7+
import { z } from "zod";
8+
console.log(chalk.blue(`${chalk.blue.bold("OpenAPI Endpoint Trimmer")} ${chalk.gray("v" + process.env.npm_package_version)}`));
9+
program
10+
.name("openapi-endpoint-trimmer")
11+
.description("OpenAPI Endpoint Trimmer.")
12+
.option("-i, --input <input>", "Input File (Local or Absolute Path). (Required: Either this or --url).")
13+
.option("-u, --url <URL>", "Input URL")
14+
.option("-o, --output <output>", "Output File")
15+
.option("-v, --version", "Display the current version.")
16+
.option("-p, --prefixes <path>", "A comma-separated, zero-spaces list of paths to keep. (Ex. /api/v1/users,/api/v1/organizations)")
17+
.option("--help", "Display all flags, commands, and descriptions.");
18+
program.parse();
19+
if (program.opts().help) {
20+
program.help();
21+
process.exit(0);
22+
}
23+
if (program.opts().version) {
24+
console.log(process.env.npm_package_version);
25+
process.exit(0);
26+
}
27+
const options = z
28+
.object({
29+
input: z.string().optional(),
30+
url: z.string().url().optional(),
31+
output: z.string(),
32+
prefixes: z.string(),
33+
})
34+
.refine((data) => {
35+
if ((data.input && data.url) || (!data.input && !data.url)) {
36+
throw new Error("Please specify either an input file or a URL (exactly one).");
37+
}
38+
return true;
39+
})
40+
.parse(program.opts());
41+
let data;
42+
if (options.input) {
43+
data = fs.readFileSync(options.input, "utf8");
44+
}
45+
else if (options.url) {
46+
const response = await request(options.url);
47+
if (response.statusCode !== 200) {
48+
throw new Error(`Received a non-200 response when downloading from ${options.url}. Received ${response.statusCode}. Please double check your setup.`);
49+
}
50+
data = await response.body.text();
51+
}
52+
else {
53+
throw new Error(`Found neither an input URL or an input file!`);
54+
}
55+
const prefixes = options.prefixes.split(",");
56+
console.log(chalk.gray(`Trimming to just paths ${prefixes.join(", ")}...`));
57+
let parsed = load(data);
58+
const paths = {};
59+
for (const path of Object.keys(parsed.paths)) {
60+
if (prefixes.some((retain) => path.startsWith(retain))) {
61+
paths[path] = parsed.paths[path];
62+
}
63+
}
64+
parsed = {
65+
...parsed,
66+
paths,
67+
};
68+
const filePath = options.output ?? (options.input ?? options.url) + "-trimmed.yaml";
69+
fs.writeFileSync(filePath, dump(parsed));
70+
console.log(`Output To: ${filePath}`);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)