Skip to content

Commit

Permalink
fix(angular): update tsconfig included and excluded files when genera…
Browse files Browse the repository at this point in the history
…ting a library secondary entry point
  • Loading branch information
leosvelperez committed Sep 28, 2023
1 parent 971a020 commit 8daf6f8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './add-files';
export * from './add-path-mapping';
export * from './normalize-options';
export * from './update-linting-file-patterns';
export * from './update-tsconfig-included-files';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { joinPathFragments, updateJson, type Tree } from '@nx/devkit';
import type { NormalizedGeneratorOptions } from '../schema';

export function updateTsConfigIncludedFiles(
tree: Tree,
options: NormalizedGeneratorOptions
): void {
const candidateTsConfigPaths = [
options.libraryProject.targets?.build?.options?.tsConfig,
joinPathFragments(options.libraryProject.root, 'tsconfig.lib.json'),
joinPathFragments(options.libraryProject.root, 'tsconfig.json'),
];

const tsConfigPath = candidateTsConfigPaths.find(
(path) => path && tree.exists(path)
);
if (!tsConfigPath) {
// ignore if the library has a custom tsconfig setup
return;
}

updateJson(tree, tsConfigPath, (json) => {
if (json.include?.length) {
json.include = json.include.map((path: string) =>
path.replace(/^(?:\.\/)?src\//, '')
);
}

if (json.exclude?.length) {
json.exclude = json.exclude.map((path: string) =>
path.replace(/^(?:\.\/)?src\//, '')
);
}

return json;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Tree,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { generateTestLibrary } from '../utils/testing';
import { librarySecondaryEntryPointGenerator } from './library-secondary-entry-point';

describe('librarySecondaryEntryPoint generator', () => {
Expand Down Expand Up @@ -180,6 +181,38 @@ describe('librarySecondaryEntryPoint generator', () => {
);
});

it('should update the tsconfig "include" and "exclude" options', async () => {
await generateTestLibrary(tree, {
name: 'lib1',
directory: 'libs/lib1',
importPath: '@my-org/lib1',
publishable: true,
});
// verify initial state
let tsConfig = readJson(tree, 'libs/lib1/tsconfig.lib.json');
expect(tsConfig.include).toStrictEqual(['src/**/*.ts']);
expect(tsConfig.exclude).toStrictEqual([
'src/**/*.spec.ts',
'src/test-setup.ts',
'jest.config.ts',
'src/**/*.test.ts',
]);

await librarySecondaryEntryPointGenerator(tree, {
name: 'testing',
library: 'lib1',
});

tsConfig = readJson(tree, 'libs/lib1/tsconfig.lib.json');
expect(tsConfig.include).toStrictEqual(['**/*.ts']);
expect(tsConfig.exclude).toStrictEqual([
'**/*.spec.ts',
'test-setup.ts',
'jest.config.ts',
'**/*.test.ts',
]);
});

it('should format files', async () => {
jest.spyOn(devkit, 'formatFiles');
addProjectConfiguration(tree, 'lib1', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
addPathMapping,
normalizeOptions,
updateLintingFilePatterns,
updateTsConfigIncludedFiles,
} from './lib';
import { GeneratorOptions } from './schema';

Expand All @@ -15,6 +16,7 @@ export async function librarySecondaryEntryPointGenerator(

addFiles(tree, options);
addPathMapping(tree, options);
updateTsConfigIncludedFiles(tree, options);
updateLintingFilePatterns(tree, options);

await formatFiles(tree);
Expand Down

0 comments on commit 8daf6f8

Please sign in to comment.