-
Notifications
You must be signed in to change notification settings - Fork 37
/
checkFiles.js
61 lines (51 loc) · 1.66 KB
/
checkFiles.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
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const path = require('path');
const chardet = require('chardet');
const checkFileEncodingAndLineEndings = (filepath) => {
// Check encoding
const encoding = chardet.detectFileSync(filepath);
if (encoding !== 'UTF-8' && encoding !== 'UTF-8-BOM') {
console.log(`File ${filepath} is not UTF-8 with BOM.`);
return false;
}
// Check for BOM
const fileContent = fs.readFileSync(filepath);
if (!fileContent.slice(0, 3).equals(Buffer.from([0xEF, 0xBB, 0xBF]))) {
console.log(`File ${filepath} does not have BOM.`);
return false;
}
// Check line endings
const content = fileContent.toString('utf8');
if (content.indexOf('\r\n') === -1) {
console.log(`File ${filepath} does not have CRLF line endings.`);
return false;
}
return true;
};
const main = () => {
const baseDir = 'resources/lang';
let allFilesValid = true;
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
const filepath = path.join(dir, file);
if (fs.statSync(filepath).isDirectory()) {
filelist = walkSync(filepath, filelist);
} else {
filelist.push(filepath);
}
});
return filelist;
};
const rcFiles = walkSync(baseDir).filter(file => file.endsWith('.rc'));
rcFiles.forEach(file => {
if (!checkFileEncodingAndLineEndings(file)) {
allFilesValid = false;
}
});
if (!allFilesValid) {
process.exit(1);
} else {
console.log("All .rc files have the correct encoding and line endings.");
}
};
main();