Skip to content

Commit

Permalink
feat: add .netlify to .gitignore in Netlify Dev (#4975)
Browse files Browse the repository at this point in the history
* feat: add `.netlify` to `.gitignore` in Netlify Dev

* chore: update tests
  • Loading branch information
eduardoboucas authored Aug 18, 2022
1 parent c0a4cad commit ac4e0e4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/commands/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
NETLIFYDEVWARN,
chalk,
detectServerSettings,
ensureNetlifyIgnore,
error,
exit,
generateNetlifyGraphJWT,
Expand Down Expand Up @@ -509,6 +510,13 @@ const dev = async (options, command) => {
error(`Could not start local development server\n\n${startDevError.message}\n\n${startDevError.stack}`)
}

// Try to add `.netlify` to `.gitignore`.
try {
await ensureNetlifyIgnore(repositoryRoot)
} catch {
// no-op
}

// TODO: We should consolidate this with the existing config watcher.
const getUpdatedConfig = async () => {
const cwd = options.cwd || process.cwd()
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/0.command.dev.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Handlers are meant to be async outside tests
const { promises: fs } = require('fs')
const http = require('http')
const { join } = require('path')

// eslint-disable-next-line ava/use-test
const avaTest = require('ava')
Expand Down Expand Up @@ -353,3 +355,45 @@ test('should generate an ETag for static assets', async (t) => {
})
})
})

test('should add `.netlify` to an existing `.gitignore` file', async (t) => {
await withSiteBuilder('site-with-gitignore', async (builder) => {
const existingGitIgnore = ['.vscode/', 'node_modules/', '!node_modules/cool_module']

await builder
.withContentFile({
path: '.gitignore',
content: existingGitIgnore.join('\n'),
})
.withContentFile({
path: 'index.html',
content: '<html><h1>Hi',
})
.buildAsync()

await withDevServer({ cwd: builder.directory }, async () => {
const gitignore = await fs.readFile(join(builder.directory, '.gitignore'), 'utf8')
const entries = gitignore.split('\n')

t.true(entries.includes('.netlify'))
})
})
})

test('should create a `.gitignore` file with `.netlify`', async (t) => {
await withSiteBuilder('site-with-no-gitignore', async (builder) => {
await builder
.withContentFile({
path: 'index.html',
content: '<html><h1>Hi',
})
.buildAsync()

await withDevServer({ cwd: builder.directory }, async () => {
const gitignore = await fs.readFile(join(builder.directory, '.gitignore'), 'utf8')
const entries = gitignore.split('\n')

t.true(entries.includes('.netlify'))
})
})
})

1 comment on commit ac4e0e4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 222 MB

Please sign in to comment.