All files / generators/cdk-app generator.ts

100% Statements 58/58
100% Branches 4/4
100% Functions 7/7
100% Lines 57/57

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2191x                       1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x       11x                             1x         22x   22x             1x       22x   22x                         1x                                           1x       22x   22x           22x                       22x       22x 22x 22x 22x 22x   22x     1x         22x   22x                 1x         22x   22x         22x 11x         11x             1x       22x 22x           22x   22x           22x 11x     22x   22x 22x 22x   22x   22x 4x     22x     1x  
import {
  addDependenciesToPackageJson,
  addProjectConfiguration,
  formatFiles,
  generateFiles,
  GeneratorCallback,
  joinPathFragments,
  offsetFromRoot,
  runTasksInSerial,
  Tree,
  writeJson,
} from '@nx/devkit';
import { Linter, lintProjectGenerator } from '@nx/eslint';
import { configurationGenerator } from '@nx/jest';
import { getRelativePathToRootTsConfig } from '@nx/js';
import { ProjectType } from '@nx/workspace';
import { resolve } from 'node:path';
import normalizeProjectOptions, {
  NormalizedProjectOptionsApplication,
} from '../../utils/normalize-project-options';
import { getVersions, Versions } from '../../utils/versions';
import e2eProjectGenerator from '../e2e-project/generator';
import initGenerator from '../init/generator';
import { AppType } from './app-type';
import { createProjectConfiguration } from './create-project-configuration';
import { CdkAppSchema } from './schema';
 
const addLambdaDependencies = (
  tree: Tree,
  versions: Versions,
): GeneratorCallback => {
  return addDependenciesToPackageJson(
    tree,
    {
      // Lambda dependencies
      '@aws-lambda-powertools/logger':
        versions['@aws-lambda-powertools/logger'],
    },
    {
      // Lambda development dependencies
      '@types/aws-lambda': versions['@types/aws-lambda'],
      esbuild: versions['esbuild'],
    },
  );
};
 
const addConfiguration = (
  tree: Tree,
  options: CdkAppSchema,
  projectOptions: NormalizedProjectOptionsApplication,
): void => {
  const { projectName } = projectOptions;
 
  addProjectConfiguration(
    tree,
    projectName,
    createProjectConfiguration(options, projectOptions),
  );
};
 
const addESLint = async (
  tree: Tree,
  projectOptions: NormalizedProjectOptionsApplication,
): Promise<GeneratorCallback> => {
  const { projectName, projectRoot } = projectOptions;
 
  return await lintProjectGenerator(tree, {
    project: projectName,
    linter: Linter.EsLint,
    eslintFilePatterns: [`${projectRoot}/**/*.ts`],
    tsConfigPaths: [joinPathFragments(projectRoot, 'tsconfig.cdk.json')],
    skipFormat: true,
    setParserOptionsProject: false,
    skipPackageJson: false,
    unitTestRunner: 'jest',
    rootProject: false,
  });
};
 
const jestConfigSnippet = `,
  collectCoverageFrom: [
    'cdk/**/*.ts',
    'shared/**/*.ts',
    'src/**/*.ts',
    '!cdk/main.ts',
    '!cdk.out/**/*',
    '!jest.config.ts',
  ],
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80,
    },
  },
  coverageReporters: ['lcov', 'text'],
  resetMocks: true,
};
`;
 
const addJest = async (
  tree: Tree,
  projectOptions: NormalizedProjectOptionsApplication,
): Promise<GeneratorCallback> => {
  const { projectName, projectRoot } = projectOptions;
 
  writeJson(
    tree,
    joinPathFragments(projectOptions.projectRoot, 'tsconfig.json'),
    {},
  );
 
  const callback = await configurationGenerator(tree, {
    project: projectName,
    supportTsx: false,
    setupFile: 'none',
    skipSerializers: true,
    testEnvironment: 'node',
    skipFormat: true,
    compiler: 'tsc',
    skipPackageJson: false,
    js: false,
  });
 
  const jestConfig = tree.read(
    `${projectRoot}/jest.config.ts`,
    'utf-8',
  ) as string;
  const lines = jestConfig.split('\n');
  lines.pop();
  lines.pop();
  const extendedJestConfig = lines.join('\n') + jestConfigSnippet;
  tree.write(`${projectRoot}/jest.config.ts`, extendedJestConfig);
 
  return callback;
};
 
const addE2ETestsProject = async (
  tree: Tree,
  options: CdkAppSchema,
  projectOptions: NormalizedProjectOptionsApplication,
): Promise<GeneratorCallback> => {
  const { projectName, projectRoot } = projectOptions;
 
  return await e2eProjectGenerator(tree, {
    name: `${projectName}-e2e`,
    directory: `${projectRoot}-e2e`,
    project: projectName,
    type: options.type,
    skipFormat: true,
  });
};
 
const addFiles = (
  tree: Tree,
  options: CdkAppSchema,
  projectOptions: NormalizedProjectOptionsApplication,
): void => {
  const { projectName, projectRoot } = projectOptions;
 
  generateFiles(tree, resolve(__dirname, 'files'), projectRoot, {
    offset: offsetFromRoot(projectRoot),
    rootTsConfigPath: getRelativePathToRootTsConfig(tree, projectRoot),
    tmpl: '',
  });
  if (options.type === AppType.generic) {
    generateFiles(tree, resolve(__dirname, 'files-generic'), projectRoot, {
      projectName,
      tmpl: '',
    });
  } else {
    generateFiles(tree, resolve(__dirname, 'files-lambda'), projectRoot, {
      projectName,
      tmpl: '',
    });
  }
};
 
export const cdkAppGenerator = async (
  tree: Tree,
  options: CdkAppSchema,
): Promise<GeneratorCallback> => {
  const versions = getVersions();
  const projectOptions = normalizeProjectOptions(tree, {
    name: options.name,
    directory: options.directory,
    projectType: ProjectType.Application,
  });
 
  const tasks: GeneratorCallback[] = [];
 
  tasks.push(
    await initGenerator(tree, {
      skipFormat: true,
    }),
  );
 
  if (options.type === AppType.lambda) {
    tasks.push(addLambdaDependencies(tree, versions));
  }
 
  addConfiguration(tree, options, projectOptions);
 
  tasks.push(await addESLint(tree, projectOptions));
  tasks.push(await addJest(tree, projectOptions));
  tasks.push(await addE2ETestsProject(tree, options, projectOptions));
 
  addFiles(tree, options, projectOptions);
 
  if (!options.skipFormat) {
    await formatFiles(tree);
  }
 
  return runTasksInSerial(...tasks);
};
 
export default cdkAppGenerator;