-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreatejson.js
63 lines (51 loc) · 1.67 KB
/
createjson.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
const fs = require('fs');
const path = require('path');
const codelabsPath = '_posts/codelabs/';
const indexJson = [];
const calculateDuration = wordCount => (wordCount < 100 ? 1 : Math.ceil(wordCount / 50));
const getDuration = course => {
const coursePath = `${codelabsPath}/${course.date}-${course.slug}`;
return [...Array(course.stepTitles.length)]
.map((_, index) => `${coursePath}/${index === 0 ? 'index' : `step${index}`}.md`)
.map(stepPath => fs.readFileSync(stepPath, 'utf8'))
.reduce((acc, md, index) => {
const words = md
.replace(/\n/g, ' ')
.split(' ')
.filter(word => Boolean(word) && /\w|\d|\b/g.test(word));
const stepDuration = calculateDuration(words.length);
return {
...acc,
[index + 1]: stepDuration,
total: acc.total + stepDuration,
};
}, {
total: 0,
});
};
function readDir(dirPath) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.log(err);
throw err;
}
files
.map((file) => {
if (fs.statSync(path.join(dirPath, file)).isDirectory()) {
readDir(path.join(dirPath, file));
}
return path.join(dirPath, file);
})
.filter((file) => fs.statSync(file).isFile())
.forEach((file) => {
if (path.extname(file) === '.json' && file !== '_posts/codelabs/index.json') {
const obj = JSON.parse(fs.readFileSync(file, 'utf8'));
obj.duration = getDuration(obj);
indexJson.push(obj);
const json = JSON.stringify(indexJson);
fs.writeFileSync(`${codelabsPath}/index.json`, json, 'utf8');
}
});
});
}
readDir(codelabsPath);