-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic.build.posts.js
More file actions
44 lines (39 loc) · 1.08 KB
/
static.build.posts.js
File metadata and controls
44 lines (39 loc) · 1.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
const fs = require('fs');
const { reactStaticCustom:{ postsLocation } } = require('./package.json');
const getPost = (data) => {
const regex = /{(.|\n)*?},(.|\n)*?}/;
const post = data.match(regex);
return post[0];
};
const buildPost = async (file) => {
try {
const data = await fs.readFileSync(`./src/posts/${file}`, 'utf8');
return getPost(data);
}
catch (e) {
console.log('Build Post Error: ', e);
}
};
const buildPosts = async (path) => {
try {
console.info(`=> Building Posts from ${path}`);
const files = await fs
.readdirSync(path)
.filter(file => file !=='index.js');
return await Promise.all(files.map(buildPost));
}
catch (e) {
console.log('Build Post List Error: ', e);
}
};
const writePostList = async (posts) => {
try {
const output = `const posts = [${posts}];\nexport default posts;`;
console.info(`=> Writing Posts to ${postsLocation}`);
await fs.writeFileSync('./src/posts/index.js', output);
}
catch (e) {
console.log('Write Post List Error: ', e);
}
};
buildPosts(postsLocation).then(writePostList);