@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
123 lines • 5.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenerateSchematicDefinitionsMap = GenerateSchematicDefinitionsMap;
exports.GenerateDefinitionsMap = GenerateDefinitionsMap;
exports.indexJsonSchemaGenerator = indexJsonSchemaGenerator;
const tslib_1 = require("tslib");
const utilities_1 = require("@rxap/utilities");
const workspace_utilities_1 = require("@rxap/workspace-utilities");
const path_1 = require("path");
function getSchematicMap(tree, options) {
const { generators, schematics } = (0, workspace_utilities_1.GetGenerators)(tree, options.project);
const generatorMapList = Object.entries(generators !== null && generators !== void 0 ? generators : {});
(0, utilities_1.CoerceArrayItems)(generatorMapList, Object.entries(schematics !== null && schematics !== void 0 ? schematics : {}), (a, b) => a[0] === b[0]);
return generatorMapList;
}
function generateSchematicInputSchema(tree, options) {
const schemaList = [];
const projectRoot = (0, workspace_utilities_1.GetProjectRoot)(tree, options.project);
const packageJsonName = (0, workspace_utilities_1.GetPackageJson)(tree, projectRoot).name;
const generatorMapList = getSchematicMap(tree, options);
for (const [name, { schema }] of generatorMapList) {
const schemaPath = (0, path_1.dirname)(schema);
const schemaTemplatePath = './' + (0, path_1.join)(schemaPath, 'template.schema.json');
let schemaRefPath;
if (tree.exists((0, path_1.join)(projectRoot, schemaTemplatePath))) {
schemaRefPath = schemaTemplatePath;
}
else {
schemaRefPath = schema;
}
const content = JSON.parse(tree.read((0, path_1.join)(projectRoot, schemaRefPath)).toString());
schemaRefPath = schemaRefPath.replace(/^\.\/[^/]+\//, './');
schemaList.push({
type: 'object',
properties: {
package: {
type: "string",
const: packageJsonName,
},
name: {
type: "string",
const: name,
},
options: {
$ref: `#/definitions/${(0, utilities_1.camelize)(content.$id)}`,
}
}
});
}
return {
$schema: 'http://json-schema.org/schema',
$id: 'schematic-input',
type: 'object',
oneOf: schemaList,
definitions: GenerateSchematicDefinitionsMap(tree, options)
};
}
function GenerateSchematicDefinitionsMap(tree, options, basePath = './src') {
const projectRoot = (0, workspace_utilities_1.GetProjectRoot)(tree, options.project);
const generatorMapList = getSchematicMap(tree, options);
const definitions = {};
for (const [, { schema }] of generatorMapList) {
const schemaPath = (0, path_1.dirname)(schema);
const schemaTemplatePath = (0, path_1.join)(schemaPath, 'template.schema.json');
let schemaRefPath;
if (tree.exists((0, path_1.join)(projectRoot, schemaTemplatePath))) {
schemaRefPath = schemaTemplatePath;
}
else {
schemaRefPath = schema;
}
const content = JSON.parse(tree.read((0, path_1.join)(projectRoot, schemaRefPath)).toString());
definitions[(0, utilities_1.camelize)(content.$id)] = {
$ref: (0, path_1.relative)(basePath, schemaRefPath)
};
}
return definitions;
}
function GenerateDefinitionsMap(tree, options, basePath = './src') {
const projectSourceRoot = (0, workspace_utilities_1.GetProjectSourceRoot)(tree, options.project);
const schematicsFolder = (0, path_1.join)(projectSourceRoot, 'schematics');
const definitions = GenerateSchematicDefinitionsMap(tree, options, basePath);
for (const schematic of tree.children(schematicsFolder)) {
if (schematic.endsWith('schema.json')) {
const schema = JSON.parse(tree.read((0, path_1.join)(schematicsFolder, schematic)).toString());
definitions[(0, utilities_1.camelize)(schema.$id)] = {
$ref: (0, path_1.relative)(basePath, `./src/schematics/${schematic}`)
};
}
}
return definitions;
}
function generateTemplateSchema(tree, options) {
const definitions = GenerateDefinitionsMap(tree, options);
definitions['schematicInput'] = {
$ref: './schematic-input.schema.json',
};
return {
$schema: 'http://json-schema.org/schema',
$id: 'schematic-angular',
oneOf: [
{
type: 'array',
items: {
$ref: '#/definitions/schematicInput'
}
},
{
$ref: '#/definitions/schematicInput'
}
],
definitions
};
}
function indexJsonSchemaGenerator(tree, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const projectSourceRoot = (0, workspace_utilities_1.GetProjectSourceRoot)(tree, options.project);
(0, workspace_utilities_1.CoerceFile)(tree, (0, path_1.join)(projectSourceRoot, 'schematic-input.schema.json'), JSON.stringify(generateSchematicInputSchema(tree, options), null, 2), true);
(0, workspace_utilities_1.CoerceFile)(tree, (0, path_1.join)(projectSourceRoot, 'template.schema.json'), JSON.stringify(generateTemplateSchema(tree, options), null, 2), true);
});
}
exports.default = indexJsonSchemaGenerator;
//# sourceMappingURL=generator.js.map