-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgetfiles-es6.js
79 lines (72 loc) · 2 KB
/
getfiles-es6.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
const fs = require('fs');
class GetPosts {
constructor(dir, prefix, target) {
this.processed = 0;
this.postCount = 0;
this.posts = [];
this.directory = dir;
this.pathPrefix = prefix;
this.postsArray = [];
this.src = target;
}
getFiles() {
this.posts = fs
.readdirSync(this.directory)
.filter(name => name.indexOf('.md') !== -1)
.sort((a, b) => {
return (
fs.statSync(this.directory + a).mtime.getTime() -
fs.statSync(this.directory + b).mtime.getTime()
);
});
this.postCount = this.posts.length;
}
writeJson(arr) {
const str = JSON.stringify(this.postsArray);
const jsonContent = `export const postsArray = ${str}`;
fs.writeFile(this.src, jsonContent, err => {
if (err) {
return console.log(err);
}
});
}
/**
*
* @param {boolean} save only for testing purpos.
*/
processPosts(save) {
this.posts.forEach(post => {
let postObj = {
url: '',
name: '',
title: '',
preview: '',
timestamp: ''
};
postObj.url = `${this.pathPrefix}${post}`;
postObj.name = post.replace('.md', '');
postObj.timestamp = fs.statSync(this.directory + post).mtime.getTime();
fs.readFile(`${this.directory}${post}`, 'utf8', (err, postcontent) => {
this.processed++;
if (err) {
return console.log(err);
}
postObj.preview = postcontent.substr(0, 200);
let title = postcontent
.substr(0, postcontent.indexOf('</strong>'))
.replace('<strong>', '');
postObj.title = title;
this.postsArray.push(postObj);
if (this.postCount === this.processed && save) {
this.writeJson(this.postsArray);
}
});
});
}
}
const dir = './src/assets/posts/';
const prefix = 'assets/posts/';
const src = './src/app/blog/dashboard/posts.ts';
const postlist = new GetPosts(dir, prefix, src);
postlist.getFiles();
postlist.processPosts(true);