Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andenacitelli committed Apr 21, 2023
2 parents d2dcb6b + ba4ac56 commit 45ef441
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
71 changes: 71 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#! /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, --paths <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(),
paths: 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) {
// eslint-disable-next-line unicorn/no-await-expression-member
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 pathsToRetain = options.paths.split(",");
console.log(chalk.gray(`Trimming to just paths ${pathsToRetain.join(", ")}...`));
let parsed = load(data);
const paths = {};
for (const path of Object.keys(parsed.paths)) {
if (pathsToRetain.includes(path)) {
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi-endpoint-trimmer",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"type": "module",
"files": [
Expand Down

0 comments on commit 45ef441

Please sign in to comment.