-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordCount.js
100 lines (81 loc) · 3.05 KB
/
wordCount.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
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const readingTime = require('reading-time');
const execa = require('execa');
const { format, zonedTimeToUtc, utcToZonedTime } = require('date-fns-tz');
const projectRoot = path.join(__dirname);
const logFilePath = path.join(projectRoot, 'logfile.json');
glob(
'src/posts/**/*.md',
{ ignore: ['**/node_modules/**', '**/README.md'] },
function (err, files) {
if (err) {
throw new Error(err.message);
}
// Guess timezone of user location
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
const date = new Date();
const localDate = utcToZonedTime(date, tz);
const dateWithTimezone = format(localDate, 'yyyy-MM-dd HH:mm:ssXXX', {
timeZone: tz,
});
files.forEach((file) => {
const logFileContent = fs.readFileSync(`${logFilePath}`, 'utf8');
const markdownFileContent = fs.readFileSync(`${file}`, 'utf8');
const initialWordCount = readingTime(markdownFileContent).words;
const parsedFile = JSON.parse(logFileContent);
let fileInParts = file.split('/');
const fileName = file
.split('/')
[fileInParts.length - 1].replace('.md', '');
const existingFileName = parsedFile[fileName];
if (!existingFileName) {
console.info(
`'${fileName}' seems to be a new file, adding to wordCount.js log file...`,
);
const newFileTimestamp = dateWithTimezone;
const newContent = {
...parsedFile,
[fileName]: {
[newFileTimestamp]: {
diff: initialWordCount,
currentWords: initialWordCount,
},
},
};
fs.writeFileSync(logFilePath, JSON.stringify(newContent, null, 2));
} else {
const fileObjKeys = Object.keys(existingFileName);
const latestTimestamp = fileObjKeys[fileObjKeys.length - 1];
const currentWords = existingFileName[latestTimestamp].currentWords;
const wordCountNew = readingTime(markdownFileContent).words;
const diffTimestamp = dateWithTimezone;
const diffInWords = wordCountNew - currentWords;
if (diffInWords !== 0) {
console.info(`Changes found in '${fileName}', creating diff...`);
const updatingFileWithDiffContent = {
...parsedFile,
[fileName]: {
...existingFileName,
[diffTimestamp]: {
diff: diffInWords,
currentWords: wordCountNew,
},
},
};
fs.writeFileSync(
logFilePath,
JSON.stringify(updatingFileWithDiffContent, null, 2),
);
}
}
});
/**
* Lint-staged precommit hook doesn't stage file changes that happened through precommit hook itself
* Therefore we need to manually run git add -A to ensure that logfile.json gets staged and committed,
* otherwise it would be left behind as changed file in git
*/
execa('git', ['add', '-A'], { cwd: __dirname });
},
);