nope/modules/mod-Broker/helpers/converter.helper.ts

118 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-09-10 16:21:19 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-03-09 12:17:36
* @modify date 2020-09-09 15:59:10
* @desc [description]
*/
import { mapLimit } from "async";
import { StaticPool } from "node-worker-threads-pool";
import { cpus } from "os";
import * as TJS from "typescript-json-schema";
import { SystemLogger } from "../../mod-Logger/src/Unique.Logger";
import { FOLDER_SEPERATOR } from "./config-generator";
/**
* Helper Function, that will use multiple Threads to create the JSON-Schemas.
*
* @export
* @param {string[]} extensionPathes
* @param {string[]} files
* @param {string} schemaDir
*/
export async function generateJSONSchemas(
extensionPathes: string[],
files: string[],
schemaDir: string
) {
const pool = new StaticPool({
size: Math.max(cpus().length - 1, 1),
workerData: {
level: SystemLogger.logger.level
},
task: async (params: { name: string; schemaDir: string; file: string }) => {
const path = require("path");
const fs = require("fs");
const util = require("util");
const TJS = require("typescript-json-schema");
const { workerData } = require("worker_threads");
const SystemLogger = require(path.join(
workerData.currentDir,
"../../mod-Logger/src/Unique.Logger"
)).SystemLogger.logger;
SystemLogger.level = this.workerData.level;
const logger = SystemLogger.getLogger("module-loader");
// Promisified Version of Write File.
const _writeFile = util.promisify(fs.writeFile);
/** Get the JSON Schema, make Parameters Required*/
const _settings: TJS.PartialArgs = {
required: true
};
const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: true,
skipLibCheck: true
};
const _program = TJS.getProgramFromFiles(
[params.file],
compilerOptions
);
// We can either get the schema for one file and one type...
const _schema = TJS.generateSchema(_program, "*", _settings);
await _writeFile(
path.join(params.schemaDir, params.name + ".json"),
JSON.stringify(_schema, undefined, 4)
);
logger.info("Generating JSON-Schema for " + params.name);
},
workerData: {
currentDir: __dirname
}
});
const _modsToGenerate: {
name: string;
schemaDir: string;
file: string;
}[] = [];
for (const _mod of extensionPathes) {
const _name = _mod.split(FOLDER_SEPERATOR)[
_mod.split(FOLDER_SEPERATOR).length - 1
];
let _typeName = _name.slice(0, _name.length - ".js".length) + "-Types.ts";
let _typeFile: string | undefined = undefined;
/** Search for the Type-Defintion File */
for (const _foundTypeFile of files) {
if (_foundTypeFile.endsWith(_typeName)) {
_typeFile = _foundTypeFile;
break;
}
}
/** Adapt the Name */
_typeName = _typeName.split("-Module-Types.ts")[0];
/** Only if a type File is provided => Get the JSON-Schema */
if (_typeFile) {
_modsToGenerate.push({ name: _typeName, file: _typeFile, schemaDir });
}
}
await mapLimit(_modsToGenerate, cpus().length, async (data) => {
return pool.exec(data);
});
pool.destroy();
}