-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchode.js
More file actions
72 lines (60 loc) · 2.08 KB
/
chode.js
File metadata and controls
72 lines (60 loc) · 2.08 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
67
68
69
70
71
72
const fs = require('fs');
const chodeignoreParser = require('./src/chodeIgnoreParser');
const chodeArgumentParser = require('./src/chodeArgumentParser');
const chodeLogger = require('./src/chodeLogger');
const {rootDirectory, chodeignoreFilePath, encoding, verbose,} = chodeArgumentParser.parseCommandLineArguments(process.argv);
let chodeignoreRules = chodeignoreParser.parseChodeignore(chodeignoreFilePath);
let problemFiles = [];
let isValid = true;
if (fs.lstatSync(rootDirectory).isDirectory()) {
isValid = recurseFileTree(rootDirectory)
} else {
isValid = analyzeFile(rootDirectory)
}
chodeLogger.log();
if (isValid) {
chodeLogger.logGreen('Overall status: Valid!');
process.exit(0);
} else {
chodeLogger.logRed('Overall status: Rejected.');
chodeLogger.logRed('Problem files:');
problemFiles.forEach(problemFile => {
chodeLogger.logRed(problemFile);
});
process.exit(1);
}
function recurseFileTree(path) {
if (chodeignoreRules.includes(path.replace(rootDirectory + '/', ''))) {
return true;
}
let isValid = true;
if (fs.lstatSync(path).isDirectory()) {
fs.readdirSync(path).forEach(subPath => {
isValid = isValid & recurseFileTree(path + '/' + subPath);
});
} else {
isValid = analyzeFile(path);
}
return isValid;
}
function analyzeFile(filename) {
const rawData = fs.readFileSync(filename, encoding);
const text = rawData.toString(encoding);
const textLines = text.split("\n");
let maxLineLength = 0;
textLines.forEach(line => {
if (line.length > maxLineLength) {
maxLineLength = line.length;
}
});
chodeLogger.log(filename + ":", verbose);
chodeLogger.log(textLines.length + " lines long.", verbose);
chodeLogger.log(maxLineLength + " columns wide.", verbose);
if (maxLineLength > textLines.length) {
chodeLogger.log('\x1b[32mChode.js approved!\x1b[0m', verbose);
return true;
}
chodeLogger.log('\x1b[31mRejected - too long.\x1b[0m', verbose);
problemFiles.push(filename);
return false;
}