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 // and be able to test with different versions
ensurePackage: jest.fn(), 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 { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Linter } from '@nrwl/linter'; import { Linter } from '@nrwl/linter';
import { backwardCompatibleVersions } from '../../utils/backward-compatible-versions'; import { backwardCompatibleVersions } from '../../utils/backward-compatible-versions';
@ -238,41 +245,103 @@ describe('init', () => {
}); });
}); });
it('should add .angular to gitignore', async () => { describe('angular cache dir', () => {
tree.write('.gitignore', ''); it('should add .angular to .gitignore', async () => {
tree.write('.gitignore', '');
await init(tree, { await init(tree, {
unitTestRunner: UnitTestRunner.Jest, unitTestRunner: UnitTestRunner.Jest,
e2eTestRunner: E2eTestRunner.Cypress, e2eTestRunner: E2eTestRunner.Cypress,
linter: Linter.EsLint, linter: Linter.EsLint,
skipFormat: false, 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',
it('should not add .angular to gitignore when it already exists', async () => { `foo
tree.write(
'.gitignore',
`foo
bar bar
.angular .angular
` `
); );
await init(tree, { await init(tree, {
unitTestRunner: UnitTestRunner.Jest, unitTestRunner: UnitTestRunner.Jest,
e2eTestRunner: E2eTestRunner.Cypress, e2eTestRunner: E2eTestRunner.Cypress,
linter: Linter.EsLint, linter: Linter.EsLint,
skipFormat: false, skipFormat: false,
});
const angularEntries = tree
.read('.gitignore', 'utf-8')
.match(/^.angular$/gm);
expect(angularEntries).toHaveLength(1);
}); });
const angularEntries = tree it('should add .angular to .prettierignore', async () => {
.read('.gitignore', 'utf-8') tree.write('.prettierignore', '');
.match(/^.angular$/gm);
expect(angularEntries).toHaveLength(1); 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', () => { describe('v14 support', () => {
@ -532,41 +601,103 @@ bar
}); });
}); });
it('should add .angular to gitignore', async () => { describe('angular cache dir', () => {
tree.write('.gitignore', ''); it('should add .angular to .gitignore', async () => {
tree.write('.gitignore', '');
await init(tree, { await init(tree, {
unitTestRunner: UnitTestRunner.Jest, unitTestRunner: UnitTestRunner.Jest,
e2eTestRunner: E2eTestRunner.Cypress, e2eTestRunner: E2eTestRunner.Cypress,
linter: Linter.EsLint, linter: Linter.EsLint,
skipFormat: false, 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',
it('should not add .angular to gitignore when it already exists', async () => { `foo
tree.write(
'.gitignore',
`foo
bar bar
.angular .angular
` `
); );
await init(tree, { await init(tree, {
unitTestRunner: UnitTestRunner.Jest, unitTestRunner: UnitTestRunner.Jest,
e2eTestRunner: E2eTestRunner.Cypress, e2eTestRunner: E2eTestRunner.Cypress,
linter: Linter.EsLint, linter: Linter.EsLint,
skipFormat: false, skipFormat: false,
});
const angularEntries = tree
.read('.gitignore', 'utf-8')
.match(/^.angular$/gm);
expect(angularEntries).toHaveLength(1);
}); });
const angularEntries = tree it('should add .angular to .prettierignore', async () => {
.read('.gitignore', 'utf-8') tree.write('.prettierignore', '');
.match(/^.angular$/gm);
expect(angularEntries).toHaveLength(1); 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 { E2eTestRunner, UnitTestRunner } from '../../utils/test-runners';
import { import {
addDependenciesToPackageJsonIfDontExist, addDependenciesToPackageJsonIfDontExist,
getInstalledAngularVersionInfo,
getInstalledPackageVersion, getInstalledPackageVersion,
versions, versions,
} from '../utils/version-utils'; } from '../utils/version-utils';
@ -27,7 +26,6 @@ export async function angularInitGenerator(
tree: Tree, tree: Tree,
rawOptions: Schema rawOptions: Schema
): Promise<GeneratorCallback> { ): Promise<GeneratorCallback> {
const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree);
const tasks: GeneratorCallback[] = []; const tasks: GeneratorCallback[] = [];
const options = normalizeOptions(rawOptions); const options = normalizeOptions(rawOptions);
@ -82,7 +80,7 @@ export async function angularInitGenerator(
const e2eTask = await addE2ETestRunner(tree, options); const e2eTask = await addE2ETestRunner(tree, options);
tasks.push(e2eTask); tasks.push(e2eTask);
addGitIgnoreEntry(tree, '.angular'); ignoreAngularCacheDirectory(tree);
if (!options.skipFormat) { if (!options.skipFormat) {
await formatFiles(tree); await formatFiles(tree);
@ -208,18 +206,42 @@ async function addE2ETestRunner(
} }
} }
function addGitIgnoreEntry(host: Tree, entry: string) { function ignoreAngularCacheDirectory(tree: Tree): void {
if (host.exists('.gitignore')) { const { cli } = readNxJson(tree);
let content = host.read('.gitignore', 'utf-8'); // 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)) { if (/^\.angular$/gm.test(content)) {
return; return;
} }
content = `${content}\n${entry}\n`; content = `${content}\n${entry}\n`;
host.write('.gitignore', content); tree.write('.gitignore', content);
} else { } else {
logger.warn(`Couldn't find .gitignore file to update`); 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; export default angularInitGenerator;