-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.js
130 lines (108 loc) · 4.16 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
const fs = require('fs');
const recursive = require('recursive-readdir');
const highlightJs = require('highlight.js');
const camelCase = require('camelcase');
const Mustache = require('mustache');
var argv = require('minimist')(process.argv.slice(2), {
boolean: ['dev'],
alias: {
d: 'dev'
}
});
const capitalize = (string) => {
return camelCase([string], {pascalCase: true});
}
// Our script to add our livereload
const livereloadHtmlScript = `
<script>
document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] +
':35729/livereload.js?snipver=1"></' + 'script>')
</script>
`;
// Get all of the aesthetic css
const vaporCss = fs.readFileSync('./dist/index.css', 'utf8').toString();
// Log out the size of the CSS
console.log(`
A E S T H E T I C CSS Size: ${Buffer.byteLength(vaporCss, 'utf8') / 1024 }KB
`);
// Our mustace data object
const mustacheData = {
vaporCss: vaporCss,
bootAnimation: argv.dev ? 'animation: boot-animation 0s;' : 'animation: boot-animation 7s;',
highlightJsCss: fs.readFileSync('./demo/highlightJs.css', 'utf8').toString(),
liveReload: argv.dev ? livereloadHtmlScript : '',
demoCss: fs.readFileSync('./demo/index.css', 'utf8').toString(),
}
// Start a table of contents as json
const tableOfContents = {};
// Find all HTML Files within the demo directory, that are not our index or daniel
recursive('./demo', ['index.html', 'daniel.html', '*.css']).then((files) => {
// Create an object for each file that we found, and assign a filepath and name
files.forEach(filePath => {
// Read the file path
const fileText = fs.readFileSync(filePath, 'utf8').toString();
// Get our filePath as camel case. Replace / and - with _
// Using Split and join to replace all
const replacedPath = filePath.split('demo/').join('').split('/').join('_').split('.html').join('');
const camelCaseFilePath = camelCase(replacedPath);
// Add the path to our table of contents
// Re-assigning currentTableDirectoryObject to a key in the
// Table of contents, until we get the level deep that we need.
let currentTableDirectoryObject = tableOfContents;
const splitPath = replacedPath.split('_');
splitPath.forEach((pathElement, index) => {
if (index === splitPath.length - 1) {
currentTableDirectoryObject[pathElement] = camelCaseFilePath;
return;
} else if (!currentTableDirectoryObject[pathElement]) {
currentTableDirectoryObject[pathElement] = {};
}
currentTableDirectoryObject = currentTableDirectoryObject[pathElement];
});
// Highlight the HTML and append it to the fileText
const highlightedFileText = highlightJs.fixMarkup(highlightJs.highlight('html', fileText).value);
const hashId = `${camelCaseFilePath}`;
const snippet = `<div id="${hashId}"></div>
${fileText}
<div class="vapor-windows-95-container margin-top">
<div class="snippet-container">
<pre>
<code class="vapor-font">${highlightedFileText.trim()}</code>
</pre>
</div>
</div>`;
mustacheData[camelCaseFilePath] = snippet;
});
const getContentsLink = (contents) => {
let contentsList = `<ul>
`;
Object.keys(contents).forEach(contentsKey => {
if (typeof contents[contentsKey] === 'string') {
contentsList += `
<li>
<a href="#${contents[contentsKey]}">${capitalize(contentsKey)}</a>
</li>
`
} else {
contentsList += `<h1>${capitalize(contentsKey)}</h1>`
contentsList += getContentsLink(contents[contentsKey])
}
});
contentsList += '</ul>';
return contentsList
}
// Let's build our table of contents
const tableOfContentsSnippet = `
<h2>Elements</h2>
${getContentsLink(tableOfContents['elements'])}
<h2>Effects</h2>
${getContentsLink(tableOfContents['effects'])}
<h2>Colors</h2>
${getContentsLink(tableOfContents['colors'])}
<h2>Fonts</h2>
${getContentsLink(tableOfContents['fonts'])}
`;
mustacheData['tableOfContents'] = tableOfContentsSnippet;
const demoWithCss = Mustache.render(fs.readFileSync('./demo/index.html', 'utf8'), mustacheData);
fs.writeFileSync('./dist/index.html', demoWithCss);
});