Skip to content

Commit dae4c4c

Browse files
authored
feat: allow a flag to not set gitignore on netlify dev (#7572)
Co-authored-by: Karin <=>
1 parent c064e01 commit dae4c4c

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

docs/commands/dev.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ netlify dev
3636
- `auth` (*string*) - Netlify auth token - can be used to run this command without logging in
3737
- `offline` (*boolean*) - Disables any features that require network access
3838
- `port` (*string*) - port of netlify dev
39+
- `skip-gitignore` (*boolean*) - skip adding .netlify to .gitignore file
3940
- `target-port` (*string*) - port of target app server
4041

4142
| Subcommand | description |
@@ -56,6 +57,7 @@ netlify dev --edge-inspect
5657
netlify dev --edge-inspect=127.0.0.1:9229
5758
netlify dev --edge-inspect-brk
5859
netlify dev --edge-inspect-brk=127.0.0.1:9229
60+
netlify dev --skip-gitignore # skip adding .netlify to .gitignore
5961
BROWSER=none netlify dev # disable browser auto opening
6062
```
6163

src/commands/dev/dev.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,12 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
224224
})
225225

226226
// Try to add `.netlify` to `.gitignore`.
227-
try {
228-
await ensureNetlifyIgnore(repositoryRoot)
229-
} catch {
230-
// no-op
227+
if (!options.skipGitignore) {
228+
try {
229+
await ensureNetlifyIgnore(repositoryRoot)
230+
} catch {
231+
// no-op
232+
}
231233
}
232234

233235
// TODO: We should consolidate this with the existing config watcher.

src/commands/dev/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const createDevCommand = (program: BaseCommand) => {
9393
.conflicts('edgeInspect')
9494
.argParser(validateShortFlagArgs),
9595
)
96+
.addOption(new Option('--skip-gitignore', 'skip adding .netlify to .gitignore file'))
9697
.addExamples([
9798
'netlify dev',
9899
'netlify dev -d public',
@@ -104,6 +105,7 @@ export const createDevCommand = (program: BaseCommand) => {
104105
'netlify dev --edge-inspect=127.0.0.1:9229',
105106
'netlify dev --edge-inspect-brk',
106107
'netlify dev --edge-inspect-brk=127.0.0.1:9229',
108+
'netlify dev --skip-gitignore # skip adding .netlify to .gitignore',
107109
'BROWSER=none netlify dev # disable browser auto opening',
108110
])
109111
.addHelpText('after', () => {

tests/integration/commands/dev/dev.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,52 @@ describe.concurrent('command/dev', () => {
573573
})
574574
})
575575

576+
test('should not add `.netlify` to `.gitignore` when --skip-gitignore flag is used', async (t) => {
577+
await withSiteBuilder(t, async (builder) => {
578+
const existingGitIgnore = ['.vscode/', 'node_modules/', '!node_modules/cool_module']
579+
580+
await builder
581+
.withContentFile({
582+
path: '.gitignore',
583+
content: existingGitIgnore.join('\n'),
584+
})
585+
.withContentFile({
586+
path: 'index.html',
587+
content: '<html><h1>Hi',
588+
})
589+
.build()
590+
591+
await withDevServer({ cwd: builder.directory, args: ['--skip-gitignore'] }, async () => {
592+
const gitignore = await fs.readFile(path.join(builder.directory, '.gitignore'), 'utf8')
593+
const entries = gitignore.split('\n')
594+
595+
t.expect(entries.includes('.netlify')).toBe(false)
596+
t.expect(entries).toEqual(existingGitIgnore)
597+
})
598+
})
599+
})
600+
601+
test('should not create a `.gitignore` file when --skip-gitignore flag is used', async (t) => {
602+
await withSiteBuilder(t, async (builder) => {
603+
await builder
604+
.withContentFile({
605+
path: 'index.html',
606+
content: '<html><h1>Hi',
607+
})
608+
.build()
609+
610+
await withDevServer({ cwd: builder.directory, args: ['--skip-gitignore'] }, async () => {
611+
try {
612+
await fs.access(path.join(builder.directory, '.gitignore'))
613+
t.expect(false).toBe(true) // Should not reach here
614+
} catch (error) {
615+
// File should not exist, which is expected
616+
t.expect((error as NodeJS.ErrnoException).code).toBe('ENOENT')
617+
}
618+
})
619+
})
620+
})
621+
576622
describe.concurrent('blobs', () => {
577623
describe.concurrent('on startup', () => {
578624
test.skipIf(process.env.NETLIFY_TEST_DISABLE_LIVE === 'true')(

0 commit comments

Comments
 (0)