-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-dockerignore.js
49 lines (45 loc) · 1.46 KB
/
generate-dockerignore.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
46
47
48
49
const fs = require("fs");
const path = require("path");
function findDockerIgnoreFiles(
dirPath,
ignoreDirs = [],
dockerIgnoreFiles = []
) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory() && !ignoreDirs.includes(file)) {
findDockerIgnoreFiles(filePath, ignoreDirs, dockerIgnoreFiles);
} else if (file === ".dockerignore") {
dockerIgnoreFiles.push(path.relative(process.cwd(), filePath));
}
});
return dockerIgnoreFiles;
}
const dockerIgnoreFiles = findDockerIgnoreFiles(process.cwd(), [
"node_modules",
"dist",
"build",
"deps",
"Accounts",
".git",
]);
console.log(dockerIgnoreFiles);
const rootDockerIgnorePath = path.join(__dirname, "dockerignore.txt");
fs.writeFileSync(rootDockerIgnorePath, "");
for (const dockerIgnoreFile of dockerIgnoreFiles) {
const dirName = path.dirname(dockerIgnoreFile);
const contents = fs.readFileSync(dockerIgnoreFile, "utf8");
const lines = contents.split("\n");
const added = {};
for (const line of lines) {
if (line.trim() !== "" && Array.from(line)[0] !== "#") {
if (added[`${dirName}/${line}\n`]) {
continue;
}
added[`${dirName}/${line}\n`] = true;
fs.appendFileSync(rootDockerIgnorePath, `${dirName}/${line}\n`);
}
}
}