@rxap/plugin-library
Version:
This package provides generators and executors for managing and maintaining Nx plugin libraries. It includes functionality for generating index exports, fixing dependencies, generating JSON schemas, and more. It helps streamline the development process fo
86 lines • 3.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const json_schema_to_typescript_1 = require("@rxap/json-schema-to-typescript");
const plugin_utilities_1 = require("@rxap/plugin-utilities");
const utilities_1 = require("@rxap/utilities");
const fs_1 = require("fs");
const glob_1 = require("glob");
const path_1 = require("path");
function getSchemaJsonPaths(basePath, excludePatterns = []) {
excludePatterns.push('**/node_modules/**', '**/dist/**', '**/docs/**', '**/coverage/**');
// Use glob to find all `schema.json` within the basePath
const schemas = (0, glob_1.globSync)('**/schema.json', {
cwd: basePath,
ignore: excludePatterns,
nodir: true,
absolute: false
});
// Return the found paths relative to the base path
return schemas;
}
function isGenerator(path) {
return path.includes('/generators/');
}
function isExecutor(path) {
return path.includes('/executors/');
}
function getSuffix(path) {
let suffix = undefined;
if (isExecutor(path)) {
suffix = 'Executor';
}
if (isGenerator(path)) {
suffix = 'Generator';
}
return suffix;
}
function generateSchema(schema, suffix) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let name = (0, utilities_1.camelize)(schema.$id || schema.title || 'schema');
if (suffix) {
if (name.endsWith(suffix)) {
name = name.replace(new RegExp(`${suffix}$`), '');
}
if (name === 'schema') {
suffix = undefined;
}
else {
suffix = [suffix, 'Schema'].join('');
}
}
const generator = new json_schema_to_typescript_1.TypescriptInterfaceGenerator(schema, { suffix, useStringTuple: true });
const sourceFile = yield generator.build(name);
return sourceFile.getFullText();
});
}
function loadSchema(path) {
if (!(0, fs_1.existsSync)(path)) {
throw new Error(`File ${path} does not exist`);
}
const content = (0, fs_1.readFileSync)(path, 'utf-8');
try {
return JSON.parse(content);
}
catch (error) {
throw new Error(`${path} failed to parse schema`);
}
}
const runExecutor = (options, context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
console.log('Executor ran for GenerateSchema', options);
const projectRoot = (0, plugin_utilities_1.GetProjectRoot)(context);
const schemaJsonPaths = getSchemaJsonPaths(projectRoot, options.excludes);
for (let schemaJsonPath of schemaJsonPaths) {
schemaJsonPath = (0, path_1.join)(context.root, projectRoot, schemaJsonPath);
const suffix = getSuffix(schemaJsonPath);
const schema = loadSchema(schemaJsonPath);
const output = yield generateSchema(schema, suffix);
const outputPath = schemaJsonPath.replace(/\.json$/, '.d.ts');
(0, fs_1.writeFileSync)(outputPath, output);
}
return {
success: true,
};
});
exports.default = runExecutor;
//# sourceMappingURL=executor.js.map