fix(angular): add angular cache dir to .prettierignore (#15923)

This commit is contained in:
Leosvel Pérez Espinosa 2023-03-28 11:40:32 +01:00 committed by GitHub
parent b4eef17707
commit 26c864ae6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 209 additions and 56 deletions

View File

@ -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,7 +245,8 @@ describe('init', () => {
});
});
it('should add .angular to gitignore', async () => {
describe('angular cache dir', () => {
it('should add .angular to .gitignore', async () => {
tree.write('.gitignore', '');
await init(tree, {
@ -251,7 +259,7 @@ describe('init', () => {
expect(tree.read('.gitignore', 'utf-8')).toContain('.angular');
});
it('should not add .angular to gitignore when it already exists', async () => {
it('should not add .angular to .gitignore when it already exists', async () => {
tree.write(
'.gitignore',
`foo
@ -275,6 +283,67 @@ bar
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', () => {
let tree: Tree;
beforeEach(() => {
@ -532,7 +601,8 @@ bar
});
});
it('should add .angular to gitignore', async () => {
describe('angular cache dir', () => {
it('should add .angular to .gitignore', async () => {
tree.write('.gitignore', '');
await init(tree, {
@ -545,7 +615,7 @@ bar
expect(tree.read('.gitignore', 'utf-8')).toContain('.angular');
});
it('should not add .angular to gitignore when it already exists', async () => {
it('should not add .angular to .gitignore when it already exists', async () => {
tree.write(
'.gitignore',
`foo
@ -568,5 +638,66 @@ bar
.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'
);
});
});
});
});

View File

@ -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<GeneratorCallback> {
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;