open-rpc-compiler
Version:
Organize OpenRPC doc into files and directories and compile a complete OpenRPC document
81 lines (80 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var path = require("path");
var OPENRPC_VERSION = '1.2.4';
var doc = {
openrpc: OPENRPC_VERSION,
};
function addInfo(doc, jsonPath) {
var fileContent = fs.readFileSync(jsonPath);
var info = JSON.parse(fileContent.toString());
doc["info"] = info;
return doc;
}
function addMethod(doc, jsonPath, tag) {
if (!("methods" in doc)) {
doc["methods"] = [];
}
var fileContent = fs.readFileSync(jsonPath);
var method = JSON.parse(fileContent.toString());
if (tag) {
method["tags"] = [{ "name": tag }];
}
doc["methods"].push(method);
return doc;
}
function addMethods(doc, methodsDirectoryPath, tag) {
try {
var files = fs.readdirSync(methodsDirectoryPath);
files.forEach(function (fileName) {
var fullPath = path.join(methodsDirectoryPath, fileName);
var stats = fs.statSync(fullPath);
if (stats.isFile()) {
doc = addMethod(doc, fullPath, tag);
}
else {
doc = addMethods(doc, fullPath, fileName);
}
});
}
catch (err) {
console.error("Error:", err);
}
return doc;
}
function addSchema(doc, name, jsonPath) {
var fileContent = fs.readFileSync(jsonPath);
var schema = JSON.parse(fileContent.toString());
if (!("components" in doc)) {
doc["components"] = { "schemas": {} };
}
doc["components"]["schemas"][name] = schema;
return doc;
}
function addSchemas(doc, schemasDirectoryPath) {
try {
var files = fs.readdirSync(schemasDirectoryPath);
files.forEach(function (fileName) {
var fullPath = path.join(schemasDirectoryPath, fileName);
var stats = fs.statSync(fullPath);
if (stats.isFile() && path.extname(fileName) == ".json") {
var name_1 = path.basename(fileName, '.json');
doc = addSchema(doc, name_1, fullPath);
}
});
}
catch (err) {
console.error("Error:", err);
}
return doc;
}
var cwd = process.cwd();
var infoPath = path.join(cwd, 'info.json');
doc = addInfo(doc, infoPath);
var methodsPath = path.join(cwd, 'methods');
doc = addMethods(doc, methodsPath);
var schemasPath = path.join(cwd, 'components', 'schemas');
doc = addSchemas(doc, schemasPath);
console.log(JSON.stringify(doc, null, 2));