All files / generators/client-feature client-feature.generator.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                        1x 1x 1x   1x   1x           3x 3x   3x 2x 2x     1x                   1x   1x   1x   1x    
import { libraryGenerator } from '@nx/angular/generators';
import { generateFiles, joinPathFragments, type ProjectConfiguration, readProjectConfiguration, type Tree } from '@nx/devkit';
 
import { cleanup } from '../../utils/cleanup.util';
import { finalizeGenerator } from '../../utils/finalizer.util';
import { generateFilesConfig } from '../../utils/generate-files.config';
import { updateProjectLinterConfig } from '../../utils/project-configuration.util';
import type { ISchematicContext } from './schema.interface';
 
/**
 * Adds/replaces files generated for a library by default.
 */
const addFiles = (schema: ISchematicContext, tree: Tree) => {
  const config: ProjectConfiguration = readProjectConfiguration(tree, schema.name);
  const root = config.root;
 
  const generateFilesConf = generateFilesConfig(schema.name, 'client-');
 
  generateFiles(tree, joinPathFragments(__dirname, './files'), root, {
    ...generateFilesConf,
  });
};
 
export default async function (tree: Tree, schema: ISchematicContext) {
  const name = schema.name;
  const tags = schema.tags;
 
  if (!/^client-[a-z-]+$/.test(name)) {
    const message = 'The name must start with client- and contain only lower case letters and dashes.';
    throw new Error(message);
  }
 
  await libraryGenerator(tree, {
    name,
    prefix: 'app',
    routing: false,
    skipModule: true,
    standalone: false,
    tags,
    directory: `libs/${name}`,
  });
 
  addFiles(schema, tree);
 
  updateProjectLinterConfig(schema, tree);
 
  await cleanup();
 
  return async () => finalizeGenerator(schema);
}