fets
Version:
TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience
54 lines (53 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cli = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const node_util_1 = require("node:util");
const js_yaml_1 = require("js-yaml");
const fetch_1 = require("@whatwg-node/fetch");
const options = {
'openapi-url': {
type: 'string',
},
'output-file': {
type: 'string',
},
};
async function cli(argv = process.argv.slice(2)) {
const args = [...argv];
while (args[0]?.startsWith('/') || args[0] === '--') {
args.shift();
}
const { values: { 'openapi-url': openapiUrl, 'output-file': outputFile = 'oas.ts' }, } = (0, node_util_1.parseArgs)({ args, options, allowPositionals: true });
if (!openapiUrl) {
throw new Error('You have to run with --openapi-url <url>');
}
if (!outputFile.endsWith('.ts')) {
throw new Error('output-file must be ts file like `--output-file oas.ts`');
}
console.log(`Fetching ${openapiUrl}`);
let rawData;
if (openapiUrl.startsWith('http')) {
const res = await (0, fetch_1.fetch)(openapiUrl);
rawData = await res.text();
if (!res.ok) {
throw new Error(`Failed to fetch ${openapiUrl}: ${rawData}`);
}
}
else {
rawData = await node_fs_1.promises.readFile(openapiUrl, 'utf-8');
}
const jsonData = (0, js_yaml_1.load)(rawData);
const oas = JSON.stringify(jsonData, null, 2);
const content = `// This file is generated by fets-cli
// Do not edit this file directly
// @eslint-ignore
// prettier-ignore
export default ${oas} as const
`;
const absoluteOutputFile = (0, node_path_1.isAbsolute)(outputFile) ? outputFile : (0, node_path_1.join)(process.cwd(), outputFile);
console.log(`Writing to ${absoluteOutputFile}`);
await node_fs_1.promises.writeFile(outputFile, content);
}
exports.cli = cli;