-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
50 lines (47 loc) · 1.56 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
const fs = require('fs');
const path = require('path');
const pug = require('pug');
const LAYOUTS = path.resolve(__dirname, 'src', 'views');
console.log(LAYOUTS);
const buildLayouts = async () => {
return new Promise(resolve => {
fs.readdir(LAYOUTS, (err, files) => {
if (err) throw err;
resolve(files.filter(file => file.includes('.pug')));
});
})
.then(views => views.map(view => view.split('.pug')[0]))
.then(views =>
views.map(view => {
const fileName = path.resolve(LAYOUTS, `${view}.pug`);
const doctype = view === 'sitemap' ? 'xml' : 'html';
return {
name: view,
content: pug.renderFile(fileName, {
pretty: true,
doctype
}),
extension: doctype
};
})
)
.then(pages =>
Promise.all(
pages.map(({ name, content, extension }) => {
const fileName = path.resolve('dist', `${name}.${extension}`);
return new Promise(resolve =>
fs.writeFile(fileName, content, err => {
if (err) throw err;
resolve(name);
})
);
})
)
);
};
buildLayouts()
.then(files => console.log('Copied files', files))
.catch((error) => {
console.error(error)
process.exit(1)
});