Skip to content

Commit

Permalink
feat(core): update default cache directory to .nx/cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Oct 10, 2023
1 parent 8ccd88c commit 743d15c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
`);
});
});
16 changes: 16 additions & 0 deletions packages/nx/src/migrations/update-17-0-0/move-cache-directory.ts
Original file line number Diff line number Diff line change
@@ -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'));
}
}
4 changes: 1 addition & 3 deletions packages/nx/src/utils/cache-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit 743d15c

Please sign in to comment.