From 26c864ae6fc1a12da96b34ca0e8fb68a724ea69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Tue, 28 Mar 2023 11:40:32 +0100 Subject: [PATCH] fix(angular): add angular cache dir to .prettierignore (#15923) --- .../angular/src/generators/init/init.spec.ts | 229 ++++++++++++++---- packages/angular/src/generators/init/init.ts | 36 ++- 2 files changed, 209 insertions(+), 56 deletions(-) diff --git a/packages/angular/src/generators/init/init.spec.ts b/packages/angular/src/generators/init/init.spec.ts index 477af7542b..62bbbf3fc8 100644 --- a/packages/angular/src/generators/init/init.spec.ts +++ b/packages/angular/src/generators/init/init.spec.ts @@ -4,7 +4,14 @@ jest.mock('@nrwl/devkit', () => ({ // and be able to test with different versions ensurePackage: jest.fn(), })); -import { NxJsonConfiguration, readJson, Tree, updateJson } from '@nrwl/devkit'; +import { + NxJsonConfiguration, + readJson, + readNxJson, + Tree, + updateJson, + updateNxJson, +} from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Linter } from '@nrwl/linter'; import { backwardCompatibleVersions } from '../../utils/backward-compatible-versions'; @@ -238,41 +245,103 @@ describe('init', () => { }); }); - it('should add .angular to gitignore', async () => { - tree.write('.gitignore', ''); + describe('angular cache dir', () => { + it('should add .angular to .gitignore', async () => { + tree.write('.gitignore', ''); - await init(tree, { - unitTestRunner: UnitTestRunner.Jest, - e2eTestRunner: E2eTestRunner.Cypress, - linter: Linter.EsLint, - skipFormat: false, + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + expect(tree.read('.gitignore', 'utf-8')).toContain('.angular'); }); - expect(tree.read('.gitignore', 'utf-8')).toContain('.angular'); - }); - - it('should not add .angular to gitignore when it already exists', async () => { - tree.write( - '.gitignore', - `foo + it('should not add .angular to .gitignore when it already exists', async () => { + tree.write( + '.gitignore', + `foo bar .angular ` - ); + ); - await init(tree, { - unitTestRunner: UnitTestRunner.Jest, - e2eTestRunner: E2eTestRunner.Cypress, - linter: Linter.EsLint, - skipFormat: false, + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + const angularEntries = tree + .read('.gitignore', 'utf-8') + .match(/^.angular$/gm); + expect(angularEntries).toHaveLength(1); }); - const angularEntries = tree - .read('.gitignore', 'utf-8') - .match(/^.angular$/gm); - expect(angularEntries).toHaveLength(1); + it('should add .angular to .prettierignore', async () => { + tree.write('.prettierignore', ''); + + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + expect(tree.read('.prettierignore', 'utf-8')).toContain('.angular'); + }); + + it('should not add .angular to .prettierignore when it already exists', async () => { + tree.write( + '.prettierignore', + `/coverage +/dist + +.angular + +` + ); + + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + const angularEntries = tree + .read('.prettierignore', 'utf-8') + .match(/^.angular$/gm); + expect(angularEntries).toHaveLength(1); + }); + + it('should add configured angular cache dir to .gitignore and .prettierignore', async () => { + tree.write('.gitignore', ''); + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + cli: { cache: { path: 'node_modules/.cache/angular' } }, + } as any); + + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + expect(tree.read('.gitignore', 'utf-8')).toContain( + 'node_modules/.cache/angular' + ); + expect(tree.read('.prettierignore', 'utf-8')).toContain( + 'node_modules/.cache/angular' + ); + }); }); describe('v14 support', () => { @@ -532,41 +601,103 @@ bar }); }); - it('should add .angular to gitignore', async () => { - tree.write('.gitignore', ''); + describe('angular cache dir', () => { + it('should add .angular to .gitignore', async () => { + tree.write('.gitignore', ''); - await init(tree, { - unitTestRunner: UnitTestRunner.Jest, - e2eTestRunner: E2eTestRunner.Cypress, - linter: Linter.EsLint, - skipFormat: false, + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + expect(tree.read('.gitignore', 'utf-8')).toContain('.angular'); }); - expect(tree.read('.gitignore', 'utf-8')).toContain('.angular'); - }); - - it('should not add .angular to gitignore when it already exists', async () => { - tree.write( - '.gitignore', - `foo + it('should not add .angular to .gitignore when it already exists', async () => { + tree.write( + '.gitignore', + `foo bar .angular ` - ); + ); - await init(tree, { - unitTestRunner: UnitTestRunner.Jest, - e2eTestRunner: E2eTestRunner.Cypress, - linter: Linter.EsLint, - skipFormat: false, + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + const angularEntries = tree + .read('.gitignore', 'utf-8') + .match(/^.angular$/gm); + expect(angularEntries).toHaveLength(1); }); - const angularEntries = tree - .read('.gitignore', 'utf-8') - .match(/^.angular$/gm); - expect(angularEntries).toHaveLength(1); + it('should add .angular to .prettierignore', async () => { + tree.write('.prettierignore', ''); + + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + expect(tree.read('.prettierignore', 'utf-8')).toContain('.angular'); + }); + + it('should not add .angular to .prettierignore when it already exists', async () => { + tree.write( + '.prettierignore', + `/coverage +/dist + +.angular + +` + ); + + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + const angularEntries = tree + .read('.prettierignore', 'utf-8') + .match(/^.angular$/gm); + expect(angularEntries).toHaveLength(1); + }); + + it('should add configured angular cache dir to .gitignore and .prettierignore', async () => { + tree.write('.gitignore', ''); + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + cli: { cache: { path: 'node_modules/.cache/angular' } }, + } as any); + + await init(tree, { + unitTestRunner: UnitTestRunner.Jest, + e2eTestRunner: E2eTestRunner.Cypress, + linter: Linter.EsLint, + skipFormat: false, + }); + + expect(tree.read('.gitignore', 'utf-8')).toContain( + 'node_modules/.cache/angular' + ); + expect(tree.read('.prettierignore', 'utf-8')).toContain( + 'node_modules/.cache/angular' + ); + }); }); }); }); diff --git a/packages/angular/src/generators/init/init.ts b/packages/angular/src/generators/init/init.ts index ce2fc8f161..58f3d4cc75 100755 --- a/packages/angular/src/generators/init/init.ts +++ b/packages/angular/src/generators/init/init.ts @@ -16,7 +16,6 @@ import { initGenerator as jsInitGenerator } from '@nrwl/js'; import { E2eTestRunner, UnitTestRunner } from '../../utils/test-runners'; import { addDependenciesToPackageJsonIfDontExist, - getInstalledAngularVersionInfo, getInstalledPackageVersion, versions, } from '../utils/version-utils'; @@ -27,7 +26,6 @@ export async function angularInitGenerator( tree: Tree, rawOptions: Schema ): Promise { - const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree); const tasks: GeneratorCallback[] = []; const options = normalizeOptions(rawOptions); @@ -82,7 +80,7 @@ export async function angularInitGenerator( const e2eTask = await addE2ETestRunner(tree, options); tasks.push(e2eTask); - addGitIgnoreEntry(tree, '.angular'); + ignoreAngularCacheDirectory(tree); if (!options.skipFormat) { await formatFiles(tree); @@ -208,18 +206,42 @@ async function addE2ETestRunner( } } -function addGitIgnoreEntry(host: Tree, entry: string) { - if (host.exists('.gitignore')) { - let content = host.read('.gitignore', 'utf-8'); +function ignoreAngularCacheDirectory(tree: Tree): void { + const { cli } = readNxJson(tree); + // angular-specific cli config is supported though is not included in the + // NxJsonConfiguration type + const angularCacheDir = (cli as any)?.cache?.path ?? '.angular'; + + addGitIgnoreEntry(tree, angularCacheDir); + addPrettierIgnoreEntry(tree, angularCacheDir); +} + +function addGitIgnoreEntry(tree: Tree, entry: string): void { + if (tree.exists('.gitignore')) { + let content = tree.read('.gitignore', 'utf-8'); if (/^\.angular$/gm.test(content)) { return; } content = `${content}\n${entry}\n`; - host.write('.gitignore', content); + tree.write('.gitignore', content); } else { logger.warn(`Couldn't find .gitignore file to update`); } } +function addPrettierIgnoreEntry(tree: Tree, entry: string): void { + if (!tree.exists('.prettierignore')) { + return; + } + + let content = tree.read('.prettierignore', 'utf-8'); + if (/^\.angular$/gm.test(content)) { + return; + } + + content = `${content}\n${entry}\n`; + tree.write('.prettierignore', content); +} + export default angularInitGenerator;