-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·75 lines (59 loc) · 1.67 KB
/
build.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
#!/usr/bin/env node
'use strict'
const fs = require('fs');
const nunjucks = require('nunjucks');
const sass = require('node-sass');
const packageImporter = require('node-sass-package-importer');
const yaml = require('js-yaml');
const sassConfig = {
file: 'src/scss/index.scss',
outFile: 'dist/styles.css',
importer: packageImporter()
};
const pagesConfig = [
{
input: 'src/index.njk',
output: 'dist/index.html'
}
]
sass.render(sassConfig, (error, result) => {
if (error) return console.log(error);
fs.writeFile(sassConfig.outFile, result.css, (error) => {
if (error) return console.log(error);
console.log(`✅ CSS file generated at ${sassConfig.outFile}`);
})
});
buildContent().then(content => {
render(content);
});
function buildContent() {
return new Promise((resolve, reject) => {
let content = {};
fs.readdir('src/components/', (error, components) => {
if (error) return console.log(error);
components.forEach(component => {
let contentFilePath = `src/components/${component}/content.yml`;
if (fs.existsSync(contentFilePath)) {
let key = `${component}_component_content`;
let value = yaml.load(fs.readFileSync(contentFilePath, 'utf8'));
content[key] = value;
}
});
console.log('✅ Content was compiled');
resolve(content);
});
});
}
function render(content) {
pagesConfig.forEach(page => {
nunjucks.render(
page.input,
content,
(error, result) => {
if (error) return console.log(error);
fs.writeFileSync(page.output, result);
console.log(`✅ HTML generated at ${page.output}`);
}
);
});
}