From 743d15ce460e16075fa80b50e93bc13557aeb93e Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Mon, 9 Oct 2023 18:30:27 -0400 Subject: [PATCH] feat(core): update default cache directory to .nx/cache --- packages/nx/migrations.json | 6 +++ .../move-cache-directory.spec.ts | 43 +++++++++++++++++++ .../update-17-0-0/move-cache-directory.ts | 16 +++++++ packages/nx/src/utils/cache-directory.ts | 4 +- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 packages/nx/src/migrations/update-17-0-0/move-cache-directory.spec.ts create mode 100644 packages/nx/src/migrations/update-17-0-0/move-cache-directory.ts diff --git a/packages/nx/migrations.json b/packages/nx/migrations.json index f3dddf4f46893b..04cd83433c6ea8 100644 --- a/packages/nx/migrations.json +++ b/packages/nx/migrations.json @@ -89,6 +89,12 @@ "version": "16.8.0-beta.3", "description": "Escape $ in env variables", "implementation": "./src/migrations/update-16-8-0/escape-dollar-sign-env-variables" + }, + "17.0.0-move-cache-directory": { + "cli": "nx", + "version": "17.0.0-beta.1", + "description": "Updates the default cache directory to .nx/cache", + "implementation": "./src/migrations/update-17-0-0/move-cache-directory" } } } diff --git a/packages/nx/src/migrations/update-17-0-0/move-cache-directory.spec.ts b/packages/nx/src/migrations/update-17-0-0/move-cache-directory.spec.ts new file mode 100644 index 00000000000000..6c5facd577f9a7 --- /dev/null +++ b/packages/nx/src/migrations/update-17-0-0/move-cache-directory.spec.ts @@ -0,0 +1,43 @@ +import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; +import migrate from './move-cache-directory'; + +describe('move-cache-directory', () => { + it('should add .nx/cache to the gitignore', () => { + const tree = createTreeWithEmptyWorkspace(); + tree.write('.gitignore', 'node_modules'); + migrate(tree); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + "node_modules + .nx/cache" + `); + }); + + it('should work if .gitignore is not present', () => { + const tree = createTreeWithEmptyWorkspace(); + tree.delete('.gitignore'); + migrate(tree); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot( + `".nx/cache"` + ); + }); + + it('should not change gitignore if directly ignored', () => { + const tree = createTreeWithEmptyWorkspace(); + tree.write('.gitignore', 'node_modules\n.nx/cache'); + migrate(tree); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + "node_modules + .nx/cache" + `); + }); + + it('should not change gitignore if ignored by another pattern', () => { + const tree = createTreeWithEmptyWorkspace(); + tree.write('.gitignore', 'node_modules\n.*/cache'); + migrate(tree); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + "node_modules + .*/cache" + `); + }); +}); diff --git a/packages/nx/src/migrations/update-17-0-0/move-cache-directory.ts b/packages/nx/src/migrations/update-17-0-0/move-cache-directory.ts new file mode 100644 index 00000000000000..bcce77a3db77ea --- /dev/null +++ b/packages/nx/src/migrations/update-17-0-0/move-cache-directory.ts @@ -0,0 +1,16 @@ +import { Tree } from '../../generators/tree'; +import ignore from 'ignore'; + +export default function moveCacheDirectory(tree: Tree) { + const gitignore = tree.exists('.gitignore') + ? tree.read('.gitignore', 'utf-8') + : ''; + const ig = ignore(); + ig.add(gitignore); + if (!ig.ignores('.nx/cache')) { + const updatedLines = gitignore.length + ? [gitignore, '.nx/cache'] + : ['.nx/cache']; + tree.write('.gitignore', updatedLines.join('\n')); + } +} diff --git a/packages/nx/src/utils/cache-directory.ts b/packages/nx/src/utils/cache-directory.ts index 2c9b04cb1ad001..83365008b292bd 100644 --- a/packages/nx/src/utils/cache-directory.ts +++ b/packages/nx/src/utils/cache-directory.ts @@ -37,9 +37,7 @@ function cacheDirectory(root: string, cacheDirectory: string) { } function defaultCacheDirectory(root: string) { - return existsSync(join(root, '.nx')) - ? join(root, '.nx', 'cache') - : join(root, 'node_modules', '.cache', 'nx'); + return join(root, '.nx', 'cache'); } /**