libs/nx/src/generators/building-block/generator.ts
Required
Properties |
nameWithPrefix | |
Type |
string
|
path | |
Type |
string
|
import { LibraryJsonSchema } from '@allianz/taly-core/schemas';
import { assertValidName } from '@allianz/taly-sdk';
import {
Tree,
formatFiles,
generateFiles,
installPackagesTask,
joinPathFragments,
readProjectConfiguration
} from '@nx/devkit';
import * as strings from '@nx/devkit/src/utils/string-utils';
import { default as addExampleGenerator } from '../building-block-example/generator';
import { extractLibraryData } from '../utils/extract-library-data';
import { addBbDependenciesToLibrary } from './lib/add-bb-dependencies-to-library/add-bb-dependencies-to-library';
import { addBbDependenciesToWorkspace } from './lib/add-bb-dependencies-to-workspace/add-bb-dependencies-to-workspace';
import { BuildingBlockGeneratorSchema } from './schema';
interface AddBuildingBlockOptionsWithNameWithPrefix
extends Required<BuildingBlockGeneratorSchema>,
LibraryJsonSchema {
nameWithPrefix: string;
path: string;
}
export default async function buildingBlock(tree: Tree, options: BuildingBlockGeneratorSchema) {
assertValidName(options.name);
const projectRoot = readProjectConfiguration(tree, options.project).root;
const libraryData = extractLibraryData(tree, projectRoot);
const nameWithPrefix = strings.dasherize(`${libraryData.prefix}-${options.name}`);
const addBuildingBlockOptionsWithNameWithPrefix: AddBuildingBlockOptionsWithNameWithPrefix = {
...options,
...libraryData,
nameWithPrefix,
path: projectRoot
};
createBuildingBlock(tree, addBuildingBlockOptionsWithNameWithPrefix);
updateExportIndexFile(tree, addBuildingBlockOptionsWithNameWithPrefix);
addExampleGenerator(tree, {
name: addBuildingBlockOptionsWithNameWithPrefix.name,
project: addBuildingBlockOptionsWithNameWithPrefix.project,
buildingBlock: addBuildingBlockOptionsWithNameWithPrefix.name
});
addBbDependenciesToLibrary(tree, { projectRoot: projectRoot });
addBbDependenciesToWorkspace(tree);
await formatFiles(tree);
return () => {
// Nx sets npm_config_legacy_peer_deps = true by default inside the `installPackagesTask` (https://github.com/nrwl/nx/blob/5ded713c3c487bd28874e75b1623ece07a79d91d/packages/nx/src/utils/package-manager.ts#L130)
// We have to explicitly set it to prevent the default behavior
process.env['npm_config_legacy_peer_deps'] ??= 'false';
installPackagesTask(tree);
};
}
function createBuildingBlock(tree: Tree, options: AddBuildingBlockOptionsWithNameWithPrefix) {
const templateOptions = {
...strings,
...options
};
generateFiles(tree, joinPathFragments(__dirname, 'files'), options.path, templateOptions);
}
function updateExportIndexFile(tree: Tree, options: AddBuildingBlockOptionsWithNameWithPrefix) {
const exportApi = `export * from './lib/${strings.dasherize(
options.nameWithPrefix
)}/public-api';`;
const libIndexFilePath = `${options.path}/src/index.ts`;
if (false === tree.exists(libIndexFilePath)) {
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 "${libIndexFilePath}"
exists and then run the generator again.
`);
}
const libIndexFile = tree.read(libIndexFilePath);
const newLibIndexFileContent = libIndexFile + '\n' + exportApi + '\n';
tree.write(libIndexFilePath, newLibIndexFileContent);
}