-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.js
84 lines (67 loc) · 2.29 KB
/
utilities.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
const fs = require('fs');
const path = require('path');
// grab the configuration file
const siteConfig = require('./site-config.json');
/** ***************************************************************************\
* utility functions
\**************************************************************************** */
// stretch line in cli to the end
function lineStretchToEnd(msg, lineSep) {
const spaceNeeded = lineSep.length - msg.length;
let spacer = '';
// if space is needed in message
if (spaceNeeded > 0) {
const spaceArray = Array.from(Array(spaceNeeded).keys());
spacer = spaceArray.map(() => ' ').join('');
}
return `${msg}${spacer}`;
}
// format bytes to better display sizing
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
}
// parse file path and create helpful file info object
function parseFilePath(filePath) {
const fileExt = path.extname(filePath);
const fileName = path.basename(filePath, fileExt);
const pathNoExt = path.dirname(filePath);
const pathArrBase = pathNoExt.split('/src/html/');
pathArrBase.shift();
const pathArr = pathArrBase[0].split('/');
pathArr.shift();
const pathDepth = pathArr.join('/');
const subDirPath = pathArr[0] === undefined ? '' : `${pathDepth}/`;
const subPath = `/${subDirPath}${fileName}`;
return {
fileExt,
fileName,
fullPath: `${siteConfig.baseUrl}${subPath}.html`,
subPath
};
}
// create sitemap
// reference: https://www.sitemaps.org/protocol.html
function createSitemap(pages) {
let urls = '';
Object.keys(pages).forEach((url, index) => {
const isLast = index + 1 === pages.length;
const endOfLine = isLast ? '' : '\r\n';
urls += ` <url>\r\n <loc>${siteConfig.baseUrl}${url}.html</loc>\r\n </url>${endOfLine}`;
});
const siteMapContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
</urlset>`;
fs.writeFileSync('static_prod/sitemap.xml', siteMapContent);
}
module.exports = {
createSitemap,
formatBytes,
lineStretchToEnd,
parseFilePath
};