-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.js
90 lines (78 loc) · 3.32 KB
/
compile.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
const {
execSync
} = require('child_process');
const fs = require("fs");
const main = async () => {
try {
const config = require('./config.json')
await compileAll(config);
}
catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
console.error("Error loading config.json")
throw e;
}
else {
console.error(e)
}
}
}
const compileAll = async (config) => {
/*
Re-compiles all the files.
?Can change to compile only changed files
*/
const output = execSync('npm run docs', {
encoding: 'utf-8'
});
let compiledContents = require('./_docs/contents.json');
compiledContents = compiledContents.contents;
const date = new Date(); //? Use the same date for all files compiled in the same batch
for (let i = 0; i < compiledContents.length; i++) {
//get proper md file. we will need it's meta info
let currentPostText = (fs.readFileSync(`./docs/${compiledContents[i].path}`));
currentPostText = currentPostText.toString();
let objToAppend = {}
if (config.posts && config.posts[i]) { // Already have some post information
objToAppend.filename = compiledContents[i].url;
objToAppend.id = compiledContents[i].id;
objToAppend.date = {}
objToAppend.date.UTCDate = (config.posts[i].date.UTCDate ? config.posts[i].date.UTCDate : Date.now());
objToAppend.date.dateString = (config.posts[i].date.dateString ? config.posts[i].date.dateString : Date);
objToAppend.date.monthDayYear = (config.posts[i].date.monthDayYear ? config.posts[i].date.monthDayYear : [date.getMonth(), date.getDate(), date.getFullYear()]);
} else { // we have to add the post from 0
objToAppend.filename = compiledContents[i].url;
objToAppend.id = compiledContents[i].id;
objToAppend.date = {}
objToAppend.date.UTCDate = date.getUTCDate();
objToAppend.date.dateString = date.toString();
objToAppend.date.monthDayYear = [date.getMonth(), date.getDate(), date.getFullYear()];
}
// add the user-decided meta data
try {
const postMetaInfo = await JSON.parse(currentPostText.substring(currentPostText.indexOf('{'), currentPostText.indexOf('}') + 1)) // cut out the JSON_text meta information
objToAppend.title = postMetaInfo.title;
objToAppend.author = postMetaInfo.author;
objToAppend.tags = postMetaInfo.tags;
} catch (error) {
console.error('It is likely that the meta information for ' + objToAppend.filename + 'has not been properly formatted, please look at our templates/metaTagtemplate.md and try again!');
console.error(error);
objToAppend.title = "None"
objToAppend.author = "None"
objToAppend.tags = []
}
// add or replace the current data to config
if (config.posts && config.posts[i]) {
config.posts[i] = objToAppend;
} else {
config.posts.push(objToAppend);
}
}
// console.log(config);
// save to file
fs.writeFile('config.json', JSON.stringify(config), (err) => {
if (err) console.error(err);
console.log('Successfully compiled!');
});
}
main();