@nu-art/schema-to-types
Version:
schema-to-types - Express & Typescript based backend framework
57 lines (56 loc) • 2.32 kB
JavaScript
;
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.processConfig = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
const generateTypeScriptType = async (inputPath, outputPath) => {
const options = {
$refOptions: {
resolve: {
// Resolves local file references
file: {
read(file) {
const filePath = path.resolve(path.dirname(inputPath), file.url);
return fs.promises.readFile(filePath, 'utf8');
}
}
}
}
};
const ts = await (0, json_schema_to_typescript_1.compileFromFile)(inputPath, options);
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, ts);
};
const processConfig = async (configPath) => {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
for (const schemaConfig of config)
await generateTypeScriptType(schemaConfig.input, schemaConfig.output);
};
exports.processConfig = processConfig;