@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
239 lines • 11.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = runExecutor;
const tslib_1 = require("tslib");
const json_schema_ref_parser_1 = tslib_1.__importDefault(require("@apidevtools/json-schema-ref-parser"));
const devkit_1 = require("@nx/devkit");
const plugin_utilities_1 = require("@rxap/plugin-utilities");
const utilities_1 = require("@rxap/utilities");
const fs_1 = require("fs");
const Handlebars = tslib_1.__importStar(require("handlebars"));
const path_1 = require("path");
function readGetStartedFile(context) {
// TODO : support GET_STARTED.md but with an fallback to GETSTARTED.md
return (0, plugin_utilities_1.readFileFromProjectRoot)(context, 'GETSTARTED.md');
}
function readGetGuidsFile(context) {
return (0, plugin_utilities_1.readFileFromProjectRoot)(context, 'GUIDES.md');
}
function getTemplate(context) {
const readmeTemplateFile = (0, plugin_utilities_1.readFileFromProjectRoot)(context, 'README.md.handlebars', (0, fs_1.readFileSync)((0, path_1.join)(__dirname, 'README.md.handlebars'), 'utf-8'));
return Handlebars.compile(readmeTemplateFile);
}
function normalizeSchema(context, schema, basePath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
schema = yield json_schema_ref_parser_1.default.bundle(schema, {
resolve: {
external: true,
file: {
canRead: /schema\.json$/,
read(file) {
// fix the file url. The url is already resolved relative the current cwd.
// this results in an incorrect path. Eg. $ref: ../init-application/schema.json
// will be resolved to file.url = /<project-root>/../init-application/schema.json
// A workaround is need to fix the url to be resolved relative to the schema.json
// file in the schematic/generator/executor/builder folder
const restoredRef = (0, path_1.relative)(context.root, file.url);
const schemaFilePath = (0, path_1.join)(basePath, restoredRef);
return (0, plugin_utilities_1.readFileFromProjectRoot)(context, schemaFilePath, true);
},
},
},
});
if (schema.allOf) {
schema.properties = {};
schema.required = [];
for (const item of schema.allOf) {
if (typeof item === 'object') {
schema.properties = (0, utilities_1.deepMerge)(schema.properties, (_a = item.properties) !== null && _a !== void 0 ? _a : {});
schema.required = [
...schema.required, ...(typeof item.required !== 'boolean' ? (_b = item.required) !== null && _b !== void 0 ? _b : [] : []),
].filter((0, utilities_1.unique)());
}
}
}
// ensure the property 'properties' is defined
(_c = schema.properties) !== null && _c !== void 0 ? _c : (schema.properties = {});
return schema;
});
}
function getSchematics(context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;
const { schematics } = yield (0, plugin_utilities_1.readPackageJsonForProjectWithRetry)(context);
if (!schematics) {
return [];
}
const projectRoot = (0, plugin_utilities_1.GetProjectRoot)(context);
const projectSourceRoot = (0, plugin_utilities_1.GetProjectSourceRoot)(context);
const collectionJson = (0, devkit_1.readJsonFile)((0, path_1.join)(context.root, projectRoot, schematics));
const schematicList = [];
for (const [schematic, config] of Object.entries((_a = collectionJson.schematics) !== null && _a !== void 0 ? _a : {})) {
let path = (0, path_1.join)(context.root, projectRoot, config.schema);
if (!(0, fs_1.existsSync)(path)) {
path = (0, path_1.join)(context.root, projectSourceRoot, config.schema);
}
if (!(0, fs_1.existsSync)(path)) {
console.warn(`Schema file for schematic '${schematic}' not found at '${path}'`);
continue;
}
schematicList.push({
name: schematic,
description: config.description,
schema: yield normalizeSchema(context, (0, devkit_1.readJsonFile)(path), (0, path_1.dirname)(config.schema)),
});
}
return schematicList;
});
}
function getGenerators(context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;
const { generators } = yield (0, plugin_utilities_1.readPackageJsonForProjectWithRetry)(context);
if (!generators) {
return [];
}
const projectRoot = (0, plugin_utilities_1.GetProjectRoot)(context);
const projectSourceRoot = (0, plugin_utilities_1.GetProjectSourceRoot)(context);
const collectionJson = (0, devkit_1.readJsonFile)((0, path_1.join)(context.root, projectRoot, generators));
const generatorList = [];
for (const [generator, config] of Object.entries((_a = collectionJson.generators) !== null && _a !== void 0 ? _a : {})) {
let path = (0, path_1.join)(context.root, projectRoot, config.schema);
if (!(0, fs_1.existsSync)(path)) {
path = (0, path_1.join)(context.root, projectSourceRoot, config.schema);
}
if (!(0, fs_1.existsSync)(path)) {
console.warn(`Schema file for generator '${generator}' not found at '${path}'`);
continue;
}
generatorList.push({
name: generator,
description: config.description,
schema: yield normalizeSchema(context, (0, devkit_1.readJsonFile)(path), (0, path_1.dirname)(config.schema)),
});
}
const schematicList = yield getSchematics(context);
return [...generatorList, ...schematicList].filter((item, index, self) => self.findIndex(s => s.name === item.name) === index);
});
}
function getBuilders(context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;
const { builders } = yield (0, plugin_utilities_1.readPackageJsonForProjectWithRetry)(context);
if (!builders) {
return [];
}
const projectRoot = (0, plugin_utilities_1.GetProjectRoot)(context);
const projectSourceRoot = (0, plugin_utilities_1.GetProjectSourceRoot)(context);
const buildersJson = (0, devkit_1.readJsonFile)((0, path_1.join)(context.root, projectRoot, builders));
const builderList = [];
for (const [builder, config] of Object.entries((_a = buildersJson.builders) !== null && _a !== void 0 ? _a : {})) {
let path = (0, path_1.join)(context.root, projectRoot, config.schema);
if (!(0, fs_1.existsSync)(path)) {
path = (0, path_1.join)(context.root, projectSourceRoot, config.schema);
}
if (!(0, fs_1.existsSync)(path)) {
console.warn(`Schema file for builder '${builder}' not found at '${path}'`);
continue;
}
builderList.push({
name: builder,
description: config.description,
schema: yield normalizeSchema(context, (0, devkit_1.readJsonFile)(path), (0, path_1.dirname)(config.schema)),
});
}
return builderList;
});
}
function getExecutors(context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;
const { executors } = yield (0, plugin_utilities_1.readPackageJsonForProjectWithRetry)(context);
if (!executors) {
return [];
}
const projectRoot = (0, plugin_utilities_1.GetProjectRoot)(context);
const projectSourceRoot = (0, plugin_utilities_1.GetProjectSourceRoot)(context);
const executorsJson = (0, devkit_1.readJsonFile)((0, path_1.join)(context.root, projectRoot, executors));
const executorList = [];
for (const [executor, config] of Object.entries((_a = executorsJson.executors) !== null && _a !== void 0 ? _a : {})) {
let path = (0, path_1.join)(context.root, projectRoot, config.schema);
if (!(0, fs_1.existsSync)(path)) {
path = (0, path_1.join)(context.root, projectSourceRoot, config.schema);
}
if (!(0, fs_1.existsSync)(path)) {
console.warn(`Schema file for executor '${executor}' not found at '${path}'`);
continue;
}
executorList.push({
name: executor,
description: config.description,
schema: yield normalizeSchema(context, (0, devkit_1.readJsonFile)(path), (0, path_1.dirname)(config.schema)),
});
}
const builderList = yield getBuilders(context);
return [...executorList, ...builderList].filter((item, index, self) => self.findIndex(s => s.name === item.name) === index);
});
}
function getPeerDependencyList(context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;
const packageJson = yield (0, plugin_utilities_1.readPackageJsonForProjectWithRetry)(context);
const peerDependencyList = [];
for (const [packageName, version] of Object.entries((_a = packageJson.peerDependencies) !== null && _a !== void 0 ? _a : {})) {
peerDependencyList.push({
name: packageName,
version,
});
}
return peerDependencyList;
});
}
Handlebars.registerHelper('hasProperties', function (record, options) {
console.log('check:', JSON.stringify(record));
if (Object.keys(record).length > 0) {
return options.fn(this);
}
});
function runExecutor(options, context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const getStartedContent = readGetStartedFile(context);
const guidesContent = readGetGuidsFile(context);
const packageJson = yield (0, plugin_utilities_1.readPackageJsonForProjectWithRetry)(context);
const template = getTemplate(context);
const generatorList = yield getGenerators(context);
const executorsList = yield getExecutors(context);
const peerDependencyList = yield getPeerDependencyList(context);
console.log('Input for README.md template ready');
let readme;
const templateContext = {
packageJson,
getStartedContent,
guidesContent,
generatorList,
executorsList,
peerDependencyList,
hasPeerDependencies: peerDependencyList.length > 0,
hasGenerators: generatorList.length > 0,
hasExecutors: executorsList.length > 0,
hasInitGenerator: generatorList.find((generator) => generator.name === 'init') !== undefined,
hasConfigGenerator: generatorList.find((generator) => generator.name === 'config') !== undefined,
};
try {
readme = template(templateContext);
}
catch (e) {
console.error(`Error while generating README.md: ${e.message}`, templateContext);
return {
success: false,
};
}
console.log('README.md generated');
(0, plugin_utilities_1.writeFileToProjectRoot)(context, 'README.md', readme);
return {
success: true,
};
});
}
//# sourceMappingURL=executor.js.map