-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
191 lines (156 loc) · 4.14 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const juice = require('juice');
const fm = require('front-matter');
const util = require('util');
const getStylesheetList = require('list-stylesheets');
//
// config
//
// Convert fs.readFile into Promise version of same
const readFilePromise = util.promisify(fs.readFile);
const destination = process.env.DESTINATION || path.join(__dirname, 'build');
const sourceTemplates = process.env.SOURCE || path.join(__dirname, 'templates', '/**/!(_*).html');
const sourceLayouts = process.env.LAYOUTS || path.join(__dirname, 'layouts');
const defaultLayout = 'default';
const replacement = /<hr id="replacement" \/>/;
//
// checks
//
let countExists = 0;
let processed = 0;
//
// remove old build and start the new one
//
function startBuild()
{
fs.remove(destination, err =>
{
if (err){
console.error(err);
}
dist();
});
}
//
// find templates, do inline-css and save to public folder
//
function dist()
{
// array with absolute paths to templates
const diskPages = glob.sync(sourceTemplates);
countExists = diskPages.length;
if(countExists > 0) {
console.info('Found templates:', countExists);
}
else {
console.error("No templates found!");
return;
}
const diskPagesRelativePaths = diskPages.reduce((acc, item) =>
{
const relativePath = item.split('/templates').slice(-1)[0];
fs.readFile(item, 'utf8', function(err, data)
{
if (err){
console.error(err);
}
pageLayoutCheck(data, destination + relativePath);
});
++processed;
}, []);
//
// final message
//
if(countExists === processed) {
console.info('✔ Processed all ' + processed + ' from ' + countExists + ' templates');
} else {
console.error('🛑️ Processed only ' + processed + ' from ' + countExists + ' templates!');
}
}
//
// load defined layout in template or fallback to default
//
function pageLayoutCheck(pageHtml, filePath)
{
let page = fm(pageHtml);
let layout = page.attributes.layout || defaultLayout;
let subject = page.attributes.subject || null;
loadLayoutData(layout, subject, page.body, filePath);
}
//
// merge template with layout and do inline-css
//
function loadLayoutData(layoutName, subject, pageHtml, filePath)
{
let layoutPath = path.join(sourceLayouts, layoutName + '.html');
if (!fs.existsSync(layoutPath)) {
console.error("Layout not found at path: " + layoutPath);
return;
}
fs.readFile(layoutPath, 'utf8', (err, layoutHtml) =>
{
if (err){
console.error(err);
}
if(null !== subject){
layoutHtml = layoutHtml.replace("<title>", "<title>" + subject + " ");
}
pageHtml = layoutHtml.replace(replacement, pageHtml);
inlineCSS(pageHtml, filePath);
});
}
//
// convert <style> to inline css on each element
//
async function inlineCSS(data, filePath) {
const fileParser = getStylesheetList(data, {
applyLinkTags: true,
removeLinkTags: true
});
const allCss = await getCssContent(fileParser.hrefs);
const finalHtml = juice.inlineContent(fileParser.html, allCss);
saveDist(filePath, finalHtml);
}
//
// read css files content and merge them to one string
//
async function getCssContent(filesArray) {
let output = "";
for (const file of filesArray) {
output += await readFile(file);
output += "\n\n"
}
return output;
}
//
// async file reading handler
//
const readFile = async (path) => {
return await readFilePromise(path);
};
//
// create folder structure and save dist
//
function saveDist(filePath, data)
{
ensureDirectoryExistence(filePath);
// finally save
fs.writeFileSync(filePath, data);
}
//
// file system helper
//
function ensureDirectoryExistence(filePath) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
//
// start process
//
startBuild();