-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathgitignore.ts
41 lines (34 loc) · 1.3 KB
/
gitignore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { readFile, writeFile } from 'fs/promises'
import path from 'path'
import parseIgnore from 'parse-gitignore'
import { fileExistsAsync } from '../lib/fs.js'
import { log } from './command-helpers.js'
const hasGitIgnore = async function (dir: string) {
const gitIgnorePath = path.join(dir, '.gitignore')
const hasIgnore = await fileExistsAsync(gitIgnorePath)
return hasIgnore
}
export const ensureNetlifyIgnore = async function (dir: string) {
const gitIgnorePath = path.join(dir, '.gitignore')
const ignoreContent = '# Local Netlify folder\n.netlify\n'
/* No .gitignore file. Create one and ignore .netlify folder */
if (!(await hasGitIgnore(dir))) {
await writeFile(gitIgnorePath, ignoreContent, 'utf8')
return false
}
let gitIgnoreContents
let ignorePatterns
try {
gitIgnoreContents = await readFile(gitIgnorePath, 'utf8')
ignorePatterns = parseIgnore.parse(gitIgnoreContents)
} catch {
// ignore
}
/* Not ignoring .netlify folder. Add to .gitignore */
if (!ignorePatterns || !ignorePatterns.patterns.some((pattern) => /(^|\/|\\)\.netlify($|\/|\\)/.test(pattern))) {
log()
log('Adding local .netlify folder to .gitignore file...')
const newContents = `${gitIgnoreContents}\n${ignoreContent}`
await writeFile(gitIgnorePath, newContents, 'utf8')
}
}