@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
112 lines • 5.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.indexExportGenerator = indexExportGenerator;
const tslib_1 = require("tslib");
// rxap-no-index-export
const devkit_1 = require("@nx/devkit");
const utilities_1 = require("@rxap/utilities");
const workspace_utilities_1 = require("@rxap/workspace-utilities");
const path_1 = require("path");
function generateIndexFile(tree, sourceRoot, libRootFolder = 'lib') {
var _a;
const libRoot = (0, path_1.join)(sourceRoot, libRootFolder);
let filePathList = [];
for (const { path, isFile } of (0, workspace_utilities_1.VisitTree)(tree, libRoot)) {
if (isFile &&
path.endsWith('.ts') &&
!path.endsWith('.spec.ts') &&
!path.endsWith('.cy.ts') &&
!path.endsWith('.stories.ts') &&
!path.endsWith('.d.ts')) {
const content = (_a = tree.read(path)) === null || _a === void 0 ? void 0 : _a.toString('utf-8');
if (content === null || content === void 0 ? void 0 : content.match(/^export /gm)) {
filePathList.push(path);
}
}
}
filePathList = filePathList.filter(path => {
var _a;
const content = (_a = tree.read(path)) === null || _a === void 0 ? void 0 : _a.toString('utf-8');
return content && !content.split('\n').some(line => line.match(/rxap-no-index-export/));
});
filePathList = filePathList.map(path => path.replace(new RegExp(`^${libRoot}/`), ''));
const map = new Map();
for (const filePath of filePathList) {
const fragments = filePath.split('/');
const fileName = fragments.pop();
const basePath = fragments.join('/');
if (!map.has(basePath)) {
map.set(basePath, []);
}
map.get(basePath).push(fileName);
}
let rootIndexFile = '';
for (const basePath of Array.from(map.keys()).sort().reverse()) {
rootIndexFile += `${rootIndexFile ? '\n' : ''}// region ${basePath.split('/').join(' ')}\n`;
if (map.has('index.ts')) {
console.log('skip folder with index.ts file', basePath);
rootIndexFile += `export * from './${(0, path_1.join)(libRootFolder, basePath)}/index';\n`;
}
else {
for (const fileName of map.get(basePath)) {
const fullPathToLibRoot = (0, path_1.join)(libRootFolder, basePath, fileName);
const importPath = fullPathToLibRoot.replace(/\.ts$/, '');
if (importPath === 'index') {
continue;
}
rootIndexFile += `export * from './${importPath}';\n`;
}
}
rootIndexFile += `// endregion\n`;
}
if (!rootIndexFile) {
rootIndexFile = 'export {};';
}
(0, workspace_utilities_1.CoerceFile)(tree, (0, path_1.join)(sourceRoot, 'index.ts'), rootIndexFile, true);
}
function skipProject(tree, options, project, projectName) {
if (options.project === projectName) {
return false;
}
if ((0, workspace_utilities_1.SkipNonLibraryProject)(tree, options, project, projectName)) {
return true;
}
return false;
}
function indexExportGenerator(tree, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f;
(_a = options.projects) !== null && _a !== void 0 ? _a : (options.projects = []);
if (options.project) {
(0, utilities_1.CoerceArrayItems)(options.projects, [options.project]);
}
console.log('index export generator: ', options);
for (const [projectName, project] of (0, devkit_1.getProjects)(tree).entries()) {
if (skipProject(tree, options, project, projectName)) {
continue;
}
console.log('generate index file for project: ', projectName);
for (const { path } of (0, workspace_utilities_1.SearchFile)(tree, (0, workspace_utilities_1.GetProjectRoot)(tree, projectName))) {
if (path.endsWith('ng-package.json')) {
generateIndexFile(tree, (0, path_1.join)((0, path_1.dirname)(path), 'src'));
}
}
const additionalEntryPoints = ((_b = options.additionalEntryPoints) !== null && _b !== void 0 ? _b : []).concat((_f = (_e = (_d = (_c = project.targets) === null || _c === void 0 ? void 0 : _c.build) === null || _d === void 0 ? void 0 : _d.options) === null || _e === void 0 ? void 0 : _e.additionalEntryPoints) !== null && _f !== void 0 ? _f : []);
for (const entryPoint of additionalEntryPoints) {
if (entryPoint.endsWith('index.ts')) {
generateIndexFile(tree, (0, path_1.dirname)(entryPoint), '');
}
}
const projectSourceRoot = (0, workspace_utilities_1.GetProjectSourceRoot)(tree, projectName);
if (!projectSourceRoot) {
console.warn(`no source root found for project: ${projectName}`);
continue;
}
if (options.generateRootExport) {
generateIndexFile(tree, projectSourceRoot);
}
}
});
}
exports.default = indexExportGenerator;
//# sourceMappingURL=generator.js.map