-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (39 loc) · 1.42 KB
/
Copy pathindex.js
File metadata and controls
52 lines (39 loc) · 1.42 KB
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
46
47
48
49
50
51
52
import walk from 'ignore-walk'
import { basename, dirname, join } from 'node:path'
import { readFile } from 'node:fs/promises'
function transformLine (line) {
if (line.trim() === '') return ''
if (line.startsWith('#')) return line
if (line.startsWith('!/')) return '!' + line.slice(2)
if (line.startsWith('!')) return '!**/' + line.slice(1)
if (line.startsWith('/')) return line.slice(1)
return '**/' + line
}
export function convertToDockerignore (input) {
return '.git/\n' + input.split(/\r?\n/g).map(transformLine).join('\n')
}
export default async function generateDockerignore (directory) {
const allFiles = await walk({ path: directory, ignoreFiles: ['.gitignore'] })
const ignoreFiles = allFiles.filter(file => basename(file) === '.gitignore')
let result = '.git/\n'
for (const file of ignoreFiles) {
const lines = (await readFile(join(directory, file), 'utf8')).split(/\r?\n/g)
// Top-most file
if (file === '.gitignore') {
result += lines.map(transformLine).join('\n') + '\n'
continue
}
result += '# From ' + file + '\n'
for (const line of lines) {
const out = transformLine(line)
if (out === '' || out.startsWith('#')) {
result += out + '\n'
} else if (out.startsWith('!')) {
result += '!' + dirname(file) + '/' + out.slice(1) + '\n'
} else {
result += dirname(file) + '/' + out + '\n'
}
}
}
return result
}