libs/nx/src/generators/building-block-example/generator.ts
Required
Properties |
buildingBlockWithPrefix | |
Type |
string
|
nameWithPrefix | |
Type |
string
|
path | |
Type |
string
|
import { LibraryJsonSchema } from '@allianz/taly-core/schemas';
import { assertValidName } from '@allianz/taly-sdk';
import { generateFiles, joinPathFragments, readProjectConfiguration, Tree } from '@nx/devkit';
import * as strings from '@nx/devkit/src/utils/string-utils';
import { extractLibraryData } from '../utils/extract-library-data';
import { BuildingBlockExampleGeneratorSchema } from './schema';
interface AddExampleOptionsWithPrefixedNameAndBuildingBlock
extends Required<BuildingBlockExampleGeneratorSchema>,
LibraryJsonSchema {
nameWithPrefix: string;
buildingBlockWithPrefix: string;
path: string;
}
export default function generate(tree: Tree, options: BuildingBlockExampleGeneratorSchema) {
assertValidName(options.name);
const projectRoot = readProjectConfiguration(tree, options.project).root;
const libraryData = extractLibraryData(tree, projectRoot);
const nameWithPrefix = strings.dasherize(`${libraryData.prefix}-${options.name}`);
const buildingBlockWithPrefix = strings.dasherize(
`${libraryData.prefix}-${options.buildingBlock}`
);
const addExampleOptionsWithPrefixedNameAndBuildingBlock: AddExampleOptionsWithPrefixedNameAndBuildingBlock =
{
...options,
...libraryData,
nameWithPrefix,
buildingBlockWithPrefix,
path: projectRoot
};
addExample(tree, addExampleOptionsWithPrefixedNameAndBuildingBlock);
updateMdFile(tree, addExampleOptionsWithPrefixedNameAndBuildingBlock);
updateExportIndexFile(tree, addExampleOptionsWithPrefixedNameAndBuildingBlock);
}
function addExample(tree: Tree, options: AddExampleOptionsWithPrefixedNameAndBuildingBlock) {
const templateOptions = {
...strings,
...options
};
generateFiles(tree, joinPathFragments(__dirname, 'files'), options.path, templateOptions);
}
function updateExportIndexFile(
tree: Tree,
options: AddExampleOptionsWithPrefixedNameAndBuildingBlock
) {
const exportComponent = `export * from './${strings.dasherize(
options.nameWithPrefix
)}/${strings.dasherize(options.nameWithPrefix)}.component';`;
const exportModule = `export * from './${strings.dasherize(
options.nameWithPrefix
)}/${strings.dasherize(options.nameWithPrefix)}.module';`;
const exportForDoc = exportComponent + '\n' + exportModule;
const docIndexFilePath = `${options.path}/documentation/examples/src/index.ts`;
if (false === tree.exists(docIndexFilePath)) {
throw new Error(`
Could not find expected index.ts file to export the new example for the Building Block from!
To add an example to your library there needs to be a file
"/documentation/examples/src/index.ts" in the given path "${options.path}".
Please check the path and make sure that the "${docIndexFilePath}"
exists and then run this command again.
`);
}
writeToFile(tree, docIndexFilePath, exportForDoc);
return tree;
}
function updateMdFile(tree: Tree, options: AddExampleOptionsWithPrefixedNameAndBuildingBlock) {
const exportComponent =
'\n' + `<!-- example(${strings.dasherize(options.nameWithPrefix)}) -->` + '\n';
const mdFilePath = `${options.path}/src/lib/${strings.dasherize(
options.buildingBlockWithPrefix
)}/${strings.dasherize(options.buildingBlockWithPrefix)}.md`;
if (false === tree.exists(mdFilePath)) {
throw new Error(`
Could not find expected ${strings.dasherize(
options.buildingBlockWithPrefix
)}.md file to add the new example for the Building Block!
To add an example to your library there needs to be a file
"/src/lib/${strings.dasherize(options.buildingBlockWithPrefix)}/${strings.dasherize(
options.buildingBlockWithPrefix
)}.md"
in the given path "${options.path}".
Please check the path and make sure that the
"${mdFilePath}"
exists and then run this command again.
`);
}
writeToFile(tree, mdFilePath, exportComponent);
return tree;
}
function writeToFile(tree: Tree, path: string, newContent: string) {
if (!tree.exists(path)) {
return;
}
const libIndexFile = tree.read(path);
const newLibIndexFileContent = libIndexFile + '\n' + newContent + '\n';
tree.write(path, newLibIndexFileContent);
}