-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensure-gitignore.js
45 lines (39 loc) · 1.04 KB
/
ensure-gitignore.js
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
42
43
44
45
import { promises as fs } from 'fs'
import ensureFile from './ensure-file.js'
import path from 'path'
let MARKER = '# views'
let CONTENT = `# views
**/view.js
**/DesignSystem/Fonts/*.js
**/DesignSystem/Fonts/*.module.css
**/*.graphql.js
**/data.js
**/index.js
!src/index.js
**/flow.js
**/view.module.css
src/Views`
export default async function ensureGitignore({ pass, src }) {
if (pass > 0) return false
let file = path.join(src, '..', '.gitignore')
let content = ''
try {
content = await fs.readFile(file, 'utf8')
} catch (error) {}
// convert crlf to lf
let lines = content.replace(/\r\n/g, '\n').split('\n')
let markerIndex = lines.indexOf(MARKER)
if (markerIndex > -1) {
lines = lines.slice(0, markerIndex)
lines.push(CONTENT)
let rest = lines.slice(markerIndex)
let endIndex = rest.map((item) => item.trim()).indexOf('')
if (endIndex > -1) {
lines = [...lines, ...rest.slice(endIndex)]
}
} else {
lines.push(CONTENT)
}
content = `${lines.join('\n')}\n`
return ensureFile({ file, content })
}