All files / libs/tools/plugin/src/generators/heal/healthpacks ts-config.ts

97.72% Statements 86/88
42.85% Branches 6/14
100% Functions 4/4
97.72% Lines 86/88

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 891x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 13x 13x 13x 13x 13x 13x 13x 13x 13x     13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 65x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x  
import { updateJson, type ProjectConfiguration, type Tree } from '@nx/devkit';
import { isEmpty, omit } from 'lodash-es';
import { type TsConfigJson } from 'type-fest';
 
const maybeCompilerOptions = (options?: TsConfigJson.CompilerOptions) => (isEmpty(options) ? undefined : options);
 
const omitDefaults = (options?: TsConfigJson.CompilerOptions) =>
  options
    ? omit(
        options,
        (
          [
            'strict',
            'esModuleInterop',
            'allowSyntheticDefaultImports',
            'strictNullChecks',
            'noImplicitAny',
            'strictBindCallApply',
            'forceConsistentCasingInFileNames',
            'noFallthroughCasesInSwitch',
            'noImplicitOverride',
            'noPropertyAccessFromIndexSignature',
            'noImplicitReturns',
            'emitDecoratorMetadata',
            'module',
            'moduleResolution',
            'allowImportingTsExtensions',
            'noEmit',
            !options.allowJs && 'allowJs',
          ] as const
        ).filter(Boolean),
      )
    : undefined;
 
/**
 * Update the tsconfig to remove options that are the same as in the root tsconfig.
 * Options and their values are hardcoded but ig we could get them programmatically from the root tsconfig idk.
 */
const updateTsConfig = (
  tree: Tree,
  tsConfigPath: string,
  {
    explicitStrict = false,
    isTestTsConfig = false,
    isLibConfig = false,
    withModuleResolution = false,
  }: { explicitStrict?: boolean; isTestTsConfig?: boolean; isLibConfig?: boolean; withModuleResolution?: boolean } = {},
) => {
  if (tree.exists(tsConfigPath)) {
    updateJson(tree, tsConfigPath, (json: TsConfigJson) => ({
      ...json,
      compilerOptions: maybeCompilerOptions({
        ...omitDefaults(json.compilerOptions),
 
        // we run everything through webpack.
        ...(withModuleResolution && { module: 'esnext', moduleResolution: 'bundler' }),
 
        ...(explicitStrict && {
          strict: json.compilerOptions?.strict ?? true, // don't override a specific false value.
          strictNullChecks: json.compilerOptions?.strictNullChecks ?? true, // don't override a specific false value.
        }),
 
        // isolatedModules is needed in test tsconfigs.
        ...(isTestTsConfig && { isolatedModules: true }),
      }),
      exclude:
        !isLibConfig || json.exclude?.includes('src/**/*.e2e-spec.ts')
          ? json.exclude
          : [...(json.exclude ?? []), 'src/**/*.e2e-spec.ts'],
      include:
        !isTestTsConfig || json.include?.includes('src/**/*.e2e-spec.ts')
          ? json.include
          : [...(json.include ?? []), 'src/**/*.e2e-spec.ts'],
    }));
  }
};
 
export const updateTsConfigs = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  updateTsConfig(tree, `${projectConfiguration.root}/tsconfig.json`, {
    explicitStrict: true,
    withModuleResolution: true,
  });
 
  updateTsConfig(tree, `${projectConfiguration.root}/tsconfig.lib.json`, { isLibConfig: true });
  updateTsConfig(tree, `${projectConfiguration.root}/tsconfig.app.json`);
  updateTsConfig(tree, `${projectConfiguration.root}/tsconfig.spec.json`, { isTestTsConfig: true });
  updateTsConfig(tree, `${projectConfiguration.root}/tsconfig.e2e.json`, { isTestTsConfig: true });
};