-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (117 loc) · 3.82 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const fs = require('fs');
const path = require('path');
const mustache = require('mustache');
const marked = require('marked');
const Server = require('dev-file-server');
module.exports = class Minimatic {
constructor(config) {
this.config = config;
this.src = `${process.cwd()}/${this.config.src}`;
this.output = `${process.cwd()}/${this.config.output}`;
this.cache = new Map();
}
importMap(map) {
return Object.entries(map).reduce((out, [key, filePath]) => {
const isDirectory = fs.lstatSync(`${this.src}/${filePath}`).isDirectory();
out[key] = isDirectory ? this.importDirectory(filePath) : this.importFile(filePath);
return out;
}, {});
}
parseMarkdown(markdown) {
const regex = /(\{[^\)]+\})/gm;
const fm = JSON.parse(markdown.match(regex)[0]);
return { ...fm, body: marked(markdown.replace(regex, '')) };
}
parseFile(filePath, content) {
if (path.extname(filePath) === '.md') {
return this.parseMarkdown(content);
} else if (path.extname(filePath) === '.json') {
return JSON.parse(content);
} else {
return content;
}
}
readOrRequire(filePath) {
if (path.extname(filePath) === '.js') {
return require(filePath);
}
return fs.readFileSync(filePath, 'utf8');
}
importFile(file) {
const filePath = `${this.src}/${file}`;
if (this.cache.has(filePath)) {
return this.cache.get(filePath);
}
const content = this.readOrRequire(filePath);
const parsedFile = this.parseFile(filePath, content);
this.cache.set(filePath, parsedFile);
return parsedFile;
}
importDirectory(dir) {
return fs.readdirSync(`${this.src}/${dir}`).map(file => this.importFile(`${dir}/${file}`));
}
async renderPage(collection, page) {
const data = {
...this.importMap(this.config.imports || {}),
...this.config,
...this.importMap(collection.imports || {}),
...collection,
...this.importMap(page.imports || {}),
...page
};
const partials = this.importMap({
...(this.config.partials || {}),
...(collection.partials || {}),
...(page.partials || {})
});
const template = this.importFile(collection.template);
fs.mkdirSync(path.dirname(`${this.output}/${page.output}`), { recursive: true });
fs.writeFileSync(`${this.output}/${page.output}`, mustache.render(template, data, partials));
if (typeof this.config.onPageRendered === 'function') {
await this.config.onPageRendered(data);
}
}
renderCollectionsPages() {
for (const collectionPath in this.config.collections) {
this.importDirectory(collectionPath).forEach(page => {
try {
this.renderPage(this.config.collections[collectionPath], page);
} catch (error) {
console.log('\x1b[31m%s\x1b[0m', `Error during processing ${this.output}/${page.output}: ${error.message}`);
}
});
}
}
async build(watch, serve) {
const start = Date.now();
if (typeof this.config.preBuild === 'function') {
await this.config.preBuild();
}
this.renderCollectionsPages();
if (typeof this.config.postBuild === 'function') {
await this.config.postBuild();
}
console.log('\x1b[32m%s\x1b[0m', `Build successful in ${Date.now() - start}ms`);
if (watch) {
this.watch();
}
if (serve) {
const server = new Server(this.output);
server.listen();
}
}
clearFileCache(filePath) {
this.cache.delete(filePath);
if (path.extname(filePath) === '.js') {
delete require.cache[filePath];
}
}
watch() {
fs.watch(this.config.src, { recursive: true }, (type, file) => {
const filePath = `${this.src}/${file}`;
console.log('\x1b[36m%s\x1b[0m', `Changes detected in: ${filePath}`);
this.clearFileCache(filePath);
this.build();
});
}
}