Repository navigation
Expand file tree
/
Copy pathpost-build.js
More file actions
66 lines (56 loc) · 1.94 KB
/
Copy pathpost-build.js
File metadata and controls
66 lines (56 loc) · 1.94 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const fs = require('fs-extra');
const path = require('path');
process.env.NODE_ENV = 'production';
const appDirectory = fs.realpathSync(process.cwd());
const buildDir = path.resolve(appDirectory, 'build');
const resolveBuildPath = (...relativePath) => path.resolve(buildDir, ...relativePath);
const browsersList = ["chrome", "edge", "opera", "firefox", "firefox_selfhost"];
cleanupForWebApp(false);
cleanupBrowserFolders();
if (fs.existsSync(resolveBuildPath('index.html'))) {
fs.copyFileSync(resolveBuildPath('index.html'), resolveBuildPath('404.html'));
}
function getFiles(path, files) {
fs.readdirSync(path).forEach(function (file) {
const subpath = `${path}/${file}`;
if (fs.lstatSync(subpath).isDirectory()) {
getFiles(subpath, files);
} else {
files.push(`${path}/${file}`);
}
});
}
function deleteUnnecessaryFiles(allFilesList, printLogs) {
const deleteFileTypes = /([.]txt|[.]svg)$/;
// Delete all the txt and svg files which are unnecessary
deleteAllFiles(allFilesList.filter(f => deleteFileTypes.test(f)), printLogs);
}
function deleteAllFiles(filesList, printLogs) {
filesList.forEach(f => {
if (printLogs) {
console.log(`Deleting file ${f}`);
}
fs.unlinkSync(f);
});
}
function cleanupForWebApp(printLogs) {
console.log("Post build cleanup action for webapp");
const allFilesList = [];
getFiles(buildDir, allFilesList);
deleteUnnecessaryFiles(allFilesList, printLogs);
}
function cleanupBrowserFolders() {
browsersList.forEach(folder => {
folder = resolveBuildPath(folder);
if (fs.existsSync(folder)) {
fs.emptyDirSync(folder);
fs.rmdirSync(folder);
}
});
['content.js', 'api-pollyfill.js', 'menu.html'].forEach(file => {
const filePath = resolveBuildPath(file);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
});
}