Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): add vite temp files to gitignore #28371 #28443

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`app generated files content - as-provided - my-app general application should add nuxt entries in .gitignore 1`] = `
"
# Nuxt dev/build outputs
"# Nuxt dev/build outputs
.output
.data
.nuxt
.nitro
.cache"
.cache
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"
`;

exports[`app generated files content - as-provided - my-app general application should add the nuxt and vitest plugins 1`] = `
Expand Down Expand Up @@ -364,13 +364,13 @@ export default defineNuxtConfig({
`;

exports[`app generated files content - as-provided - myApp general application should add nuxt entries in .gitignore 1`] = `
"
# Nuxt dev/build outputs
"# Nuxt dev/build outputs
.output
.data
.nuxt
.nitro
.cache"
.cache
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"
`;

exports[`app generated files content - as-provided - myApp general application should add the nuxt and vitest plugins 1`] = `
Expand Down
16 changes: 16 additions & 0 deletions packages/vite/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,20 @@ describe('@nx/vite:init', () => {
"
`);
});

it(`should not add multiple instances of the same vite temp file glob to gitignore`, async () => {
// ARRANGE
tree.write(
'.gitignore',
'**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*'
);

// ACT
await initGenerator(tree, {});

// ASSERT
expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(
`"**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"`
);
});
});
2 changes: 2 additions & 0 deletions packages/vite/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { setupPathsPlugin } from '../setup-paths-plugin/setup-paths-plugin';
import { createNodesV2 } from '../../plugins/plugin';
import { InitGeneratorSchema } from './schema';
import { checkDependenciesInstalled, moveToDevDependencies } from './lib/utils';
import { addViteTempFilesToGitIgnore } from '../../utils/add-vite-temp-files-to-gitignore';

export function updateNxJsonSettings(tree: Tree) {
const nxJson = readNxJson(tree);
Expand Down Expand Up @@ -83,6 +84,7 @@ export async function initGeneratorInternal(
}

updateNxJsonSettings(tree);
addViteTempFilesToGitIgnore(tree);

if (schema.setupPathsPlugin) {
await setupPathsPlugin(tree, { skipFormat: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import addViteTempFilesToGitIgnore from './add-vite-temp-files-to-git-ignore';

describe('addViteTempFilesToGitIgnore', () => {
it('should update an existing .gitignore file to add the glob correctly', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
tree.write('.gitignore', '.idea');

// ACT
addViteTempFilesToGitIgnore(tree);

// ASSERT
expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(`
".idea
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"
`);
});

it('should write a new .gitignore file to add the glob correctly', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
tree.delete('.gitignore');

// ACT
addViteTempFilesToGitIgnore(tree);

// ASSERT
expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(
`"**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"`
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Tree } from '@nx/devkit';
import { addViteTempFilesToGitIgnore as _addViteTempFilesToGitIgnore } from '../../utils/add-vite-temp-files-to-gitignore';

export default function addViteTempFilesToGitIgnore(tree: Tree) {
_addViteTempFilesToGitIgnore(tree);
}
16 changes: 16 additions & 0 deletions packages/vite/src/utils/add-vite-temp-files-to-gitignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { stripIndents, Tree } from '@nx/devkit';

export function addViteTempFilesToGitIgnore(tree: Tree) {
let newGitIgnoreContents = `**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*`;
if (tree.exists('.gitignore')) {
const gitIgnoreContents = tree.read('.gitignore', 'utf-8');
if (!gitIgnoreContents.includes(newGitIgnoreContents)) {
newGitIgnoreContents = stripIndents`${gitIgnoreContents}
${newGitIgnoreContents}`;

tree.write('.gitignore', newGitIgnoreContents);
}
} else {
tree.write('.gitignore', newGitIgnoreContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ exports[`application generator should set up project correctly with given option
[
".eslintignore",
".eslintrc.json",
".gitignore",
".prettierignore",
".prettierrc",
".vscode/extensions.json",
Expand Down