Skip to content

Commit

Permalink
fix(angular): add angular cache dir to .prettierignore (#15923)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Mar 28, 2023
1 parent b4eef17 commit 26c864a
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 56 deletions.
229 changes: 180 additions & 49 deletions packages/angular/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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'
);
});
});
});
});
36 changes: 29 additions & 7 deletions packages/angular/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

0 comments on commit 26c864a

Please sign in to comment.