-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (83 loc) · 2.49 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const args = process.argv.slice(2);
const DIRECTORY = args[0];
const EXCLUDE_PATTERNS = args.slice(1);
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
function textFromJson(json) {
if (json === null || json === undefined) return "";
if (
!Array.isArray(json) &&
!Object.getPrototypeOf(json).isPrototypeOf(Object)
)
return "" + json;
const obj = {};
for (const key of Object.keys(json)) {
obj[key] = textFromJson(json[key]);
}
return Object.values(obj).join(" ");
}
function stripWhitespace(wordArray) {
return wordArray.split(/(\s+)/).filter((e) => e.trim().length > 0);
}
function excludeVariables(wordArray) {
const variableRegex = /{([^}]+)}/;
return wordArray.filter((w) => !w.match(variableRegex));
}
function getJsonWordCount(json) {
const wordArray = textFromJson(json);
const trimmedWordArray = stripWhitespace(wordArray);
const noVariableWordArray = excludeVariables(trimmedWordArray);
return noVariableWordArray.length;
}
function* walkSync(dir) {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
yield* walkSync(path.join(dir, file.name));
} else {
yield {
path: path.join(dir, file.name),
source: file,
extension: path.extname(file.name),
};
}
}
}
let totalCount = 0;
let fileCount = 0;
async function processFiles() {
for (const file of walkSync(DIRECTORY)) {
if (EXCLUDE_PATTERNS.some((pattern) => file.path.includes(pattern))) continue;
fileCount += 1;
if (file.extension === ".json") {
try {
const fileData = fs.readFileSync(file.path, "utf8");
const count = getJsonWordCount(JSON.parse(fileData));
totalCount += count;
console.log(file.path, `\t`, count);
} catch (e) {
console.log(e);
}
} else if (file.extension === ".yml") {
try {
const fileData = yaml.load(fs.readFileSync(file.path, "utf8"));
const count = getJsonWordCount(fileData);
totalCount += count;
console.log(file.path, `\t`, count);
} catch (e) {
console.log(e);
}
} else {
console.error(file.path, "Unsupported file type!");
}
}
}
async function run() {
if (!DIRECTORY) {
return console.error('Please provide a directory: npm start [directory] [exclude pattern [optional]]')
}
await processFiles();
console.log("Total words:", totalCount, `\nFiles:`, fileCount);
}
run();