UNPKG

schema2typebox

Version:

Creates typebox code from JSON schemas

72 lines (71 loc) 2.88 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addCommentThatCodeIsGenerated = exports.schema2typebox = void 0; const cosmiconfig_1 = require("cosmiconfig"); const prettier = __importStar(require("prettier")); const schema_to_typebox_1 = require("./schema-to-typebox"); /** * Use this function for programmatic usage of schema2typebox. The options are * typed and commented. * * @returns The generated code as string * * @throws Error **/ const schema2typebox = async ({ input, }) => { const generatedTypeboxCode = await (0, schema_to_typebox_1.schema2typebox)(input); // post-processing // 1. format code const explorer = (0, cosmiconfig_1.cosmiconfig)("prettier"); const searchResult = await explorer.search(); const prettierConfig = searchResult === null ? {} : searchResult.config; const formattedResult = prettier.format(generatedTypeboxCode, { parser: "typescript", ...prettierConfig, }); // 2. add comment that code is auto generated const result = exports.addCommentThatCodeIsGenerated.run(formattedResult); return result; }; exports.schema2typebox = schema2typebox; /** * Declaring this as an object with a function in order to make it better * testable with mocks with the current/new nodejs test runner. * Allows for tracking the call count. */ exports.addCommentThatCodeIsGenerated = { run: (code) => { return `/** * ATTENTION. This code was AUTO GENERATED by schema2typebox. * While I don't know your use case, there is a high chance that direct changes * to this file get lost. Consider making changes to the underlying JSON schema * you use to generate this file instead. The default file is called * "schema.json", perhaps have a look there! :] */ ${code}`; }, }; //# sourceMappingURL=programmatic-usage.js.map