forked from auviitkgp/git-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
69 lines (56 loc) · 1.9 KB
/
test.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
62
63
64
65
66
67
68
69
var fs = require('fs');
var crypto = require('crypto');
var filename = 'README.md';
var entryRegex = /\*\s([a-zA-Z]+\s)+\[.*\]\(.*\)/g;
// read the file from the filesystem
fs.readFile(filename, function (err, data) {
if (err) {
console.error(err);
process.exit(1);
} else {
// isolate the part of the file that we want
// to analyse
var fileData = data.toString();
var startString = '<!--ALPHA-->';
var endString = '<!--ALPHAEND-->';
var startIndex = fileData.search(startString) + startString.length;
var endIndex = fileData.search(endString);
var reqSubstr = fileData.substr(startIndex, endIndex);
// make sure that start and end strings are not
// a part of the extracted string
reqSubstr = reqSubstr.replace(startString, '').replace(endString, '');
// split using a newline to get the list of entries
var lines = reqSubstr.split('\n');
// remove all the empty string occurences from the list
while((remIndex = lines.indexOf('')) != -1) {
lines.splice(remIndex, 1);
}
// check if all entries match regex
var checkFormat = lines.slice();
var flag = 0;
for (i in checkFormat) {
if (!checkFormat[i].match(entryRegex)) {
console.log('Fix this entry: ');
console.log('--------------------');
console.log(checkFormat[i]);
console.log('--------------------');
flag = 1;
}
}
if (flag) {
process.exit(1);
}
// make a copy of the list that we will sort
var copy = lines.slice();
copy.sort();
// calculate the SHA256 hash of the string versions
// of the two lists
orgHash = crypto.createHash('sha256').update(lines.toString(), 'utf8').digest('hex');
sortedHash = crypto.createHash('sha256').update(copy.toString(), 'utf8').digest('hex');
if (orgHash !== sortedHash) {
process.exit(1);
} else {
process.exit();
}
}
});