diff --git a/packages/js/src/generators/init/init.spec.ts b/packages/js/src/generators/init/init.spec.ts index b6ab997d39be5..f4e9454a960c6 100644 --- a/packages/js/src/generators/init/init.spec.ts +++ b/packages/js/src/generators/init/init.spec.ts @@ -26,6 +26,7 @@ describe('js init generator', () => { const prettierignore = tree.read('.prettierignore', 'utf-8'); expect(prettierignore).toMatch(/\n\/coverage/); expect(prettierignore).toMatch(/\n\/dist/); + expect(prettierignore).toMatch(/\n\/\.nx\/cache/); }); it('should not overwrite existing .prettierrc and .prettierignore files', async () => { diff --git a/packages/js/src/generators/init/init.ts b/packages/js/src/generators/init/init.ts index b2db674b0daa7..94b1c80dca404 100644 --- a/packages/js/src/generators/init/init.ts +++ b/packages/js/src/generators/init/init.ts @@ -121,6 +121,7 @@ export async function initGenerator( # Add files here to ignore them from prettier formatting /dist /coverage + /.nx/cache ` ); } diff --git a/packages/nx/migrations.json b/packages/nx/migrations.json index b0a038c80ade6..49cb44c679ead 100644 --- a/packages/nx/migrations.json +++ b/packages/nx/migrations.json @@ -59,6 +59,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 0000000000000..6d4376ca71f77 --- /dev/null +++ b/packages/nx/src/migrations/update-17-0-0/move-cache-directory.spec.ts @@ -0,0 +1,71 @@ +import { createTree } from '../../generators/testing-utils/create-tree'; +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" + `); + }); + + it('should not update gitignore for lerna repos without nx.json', () => { + const tree = createTree(); + tree.write('.gitignore', 'node_modules'); + tree.write('lerna.json', '{}'); + migrate(tree); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot( + `"node_modules"` + ); + }); + + it('should handle prettierignore', () => { + const tree = createTree(); + tree.write('.prettierignore', '/dist'); + migrate(tree); + expect(tree.read('.prettierignore', 'utf-8')).toMatchInlineSnapshot(` + "/dist + /.nx/cache" + `); + }); + + it('should handle missing prettierignore', () => { + const tree = createTree(); + tree.delete('.prettierignore'); + migrate(tree); + expect(tree.exists('.prettierignore')).toBeFalsy(); + }); +}); 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 0000000000000..e214dbad541aa --- /dev/null +++ b/packages/nx/src/migrations/update-17-0-0/move-cache-directory.ts @@ -0,0 +1,37 @@ +import { Tree } from '../../generators/tree'; +import ignore from 'ignore'; + +export default function moveCacheDirectory(tree: Tree) { + // If nx.json doesn't exist the repo can't utilize + // caching, so .nx/cache is less relevant. Lerna users + // that don't want to fully opt in to Nx at this time + // may also be caught off guard by the appearance of + // a .nx directory, so we are going to special case + // this for the time being. + if (tree.exists('lerna.json') && !tree.exists('nx.json')) { + return; + } + + updateGitIgnore(tree); + + if (tree.exists('.prettierignore')) { + const ignored = tree.read('.prettierignore', 'utf-8'); + if (!ignored.includes('.nx/cache')) { + tree.write('.prettierignore', [ignored, '/.nx/cache'].join('\n')); + } + } +} + +function updateGitIgnore(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 93e4e982e4d58..16bd772b45afc 100644 --- a/packages/nx/src/utils/cache-directory.ts +++ b/packages/nx/src/utils/cache-directory.ts @@ -37,9 +37,19 @@ 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'); + // If nx.json doesn't exist the repo can't utilize + // caching, so .nx/cache is less relevant. Lerna users + // that don't want to fully opt in to Nx at this time + // may also be caught off guard by the appearance of + // a .nx directory, so we are going to special case + // this for the time being. + if ( + existsSync(join(root, 'lerna.json')) && + !existsSync(join(root, 'nx.json')) + ) { + return join(root, 'node_modules', '.cache', 'nx'); + } + return join(root, '.nx', 'cache'); } /** diff --git a/packages/workspace/src/generators/new/files-integrated-repo/__dot__gitignore b/packages/workspace/src/generators/new/files-integrated-repo/__dot__gitignore index 51b9af5269c1d..98a6e38939fa2 100644 --- a/packages/workspace/src/generators/new/files-integrated-repo/__dot__gitignore +++ b/packages/workspace/src/generators/new/files-integrated-repo/__dot__gitignore @@ -37,3 +37,5 @@ testem.log # System Files .DS_Store Thumbs.db + +.nx/cache \ No newline at end of file diff --git a/packages/workspace/src/generators/new/files-package-based-repo/__dot__gitignore b/packages/workspace/src/generators/new/files-package-based-repo/__dot__gitignore index 51b9af5269c1d..ef9dcfb186332 100644 --- a/packages/workspace/src/generators/new/files-package-based-repo/__dot__gitignore +++ b/packages/workspace/src/generators/new/files-package-based-repo/__dot__gitignore @@ -37,3 +37,5 @@ testem.log # System Files .DS_Store Thumbs.db + +.nx/cache diff --git a/packages/workspace/src/generators/new/files-root-app/__dot__gitignore b/packages/workspace/src/generators/new/files-root-app/__dot__gitignore index 51b9af5269c1d..98a6e38939fa2 100644 --- a/packages/workspace/src/generators/new/files-root-app/__dot__gitignore +++ b/packages/workspace/src/generators/new/files-root-app/__dot__gitignore @@ -37,3 +37,5 @@ testem.log # System Files .DS_Store Thumbs.db + +.nx/cache \ No newline at end of file