-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
214 lines (191 loc) · 5.54 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const fs = require('fs');
const path = require('path');
const minify = require('html-minifier').minify;
const minOps = {collapseWhitespace: true};
const filters = ['node_modules'];
let puts = [];
const defaultPuts = [{in:'./', out:'./'}];
function testPut(put, label){
if (put.substr(put.length - 1) !== '/') {
throw new Error('Mini Site Generator: ' +label+ ' should end with a "/" Got:' +put);
}
return put;
}
for (let j = 0; j < process.argv.length; j++) {
//if we find an -io, the next 2 args should be for us! input output!
if (process.argv[j] == '-io') {
var input = makePathAbsolute(testPut(process.argv[j + 1], 'input'));
var output = makePathAbsolute(testPut(process.argv[j + 2], 'output'));
puts.push({
in: input,
out: output
});
j+=2;
}
}
if (puts.length == 0) {
puts = defaultPuts;
}
function errorHandler(err){
if (err) { console.log('err', err); }
}
/**
* Takes an address
* Returns true if that address points to a folder
*/
function isDirectory(sourceAddres){
return fs.lstatSync(sourceAddres).isDirectory();
}
/**
* Takes an array of addresses (files / folders / anything!)
* Returns an array of page addresses
*/
function pages(addresses){
var pages = [];
addresses.forEach(function(address){
if (address.indexOf('.page.js') !== -1) {
pages.push(address);
}
});
return pages;
}
/**
* Takes an array of addresses (files / folders / anything!)
* Returns an array of folder addresses
*/
function folders(addresses){
var folders = [];
addresses.forEach(function(address){
if (isDirectory(address)){
folders.push(address + '/');
}
});
return folders;
}
/**
* Takes an address of a folder
* Returns an array of the items within that folder
*/
function contents(dir){
return fs.readdirSync(dir);;
}
/**
* Takes an array of items (strings)
* Returns all those that do not match filters
*/
function filterContents(items, filters){
var filteredItems = [];
items.forEach(function(item){
var didPass = true;
filters.forEach(function(filter){
if (filter === item) {
didPass = false;
}
});
if (didPass) {
filteredItems.push(item);
}
});
return filteredItems;
}
/**
* Takes an address (dir) and an array of items (files / folders)
* Returns an array of addresses (for each item)
*/
function contentAddresses(dir, items){
var returnAdds = [];
items.forEach(function(item){
returnAdds.push(path.resolve(dir, item));
});
return returnAdds;
}
/**
* Takes a page filename
* Returns the dist filename
*/
function srcToDistName(srcFileName){
return srcFileName.replace('.page.js', '.html');
}
/**
* Takes a source address (meant for pages)
* Returns the dist address
*/
function srcToDistAddress(srcAddress, siteConfig){
return srcAddress.replace(siteConfig.in, siteConfig.out);
}
/**
* Takes the address of a page src
* Returns the markup generated by said page src
*/
function generateMarkup(pageAddress){
var generator = require(pageAddress);
return minify(generator(), minOps);
}
function makePathAbsolute(relativePath){
const absPath = path.resolve(relativePath);
return absPath;
}
//=================================================== Leaving pure functions behind now
//=================================================== Moving into the land of flow control
/**
* Takes an array of src page addresses and the site configuration object
* Creates the dist address, generates the dist markup, saves the markup into the dist address
*/
function buildPages(pageAddressList, siteConfig){
pageAddressList.forEach(function(srcPageAddress){
var distFilename = srcToDistName(srcPageAddress);
var distFileAddress = srcToDistAddress(distFilename, siteConfig);
fs.writeFile(distFileAddress, generateMarkup(srcPageAddress), errorHandler);
});
}
/**
* Takes a folder address
* Makes it if it doesn't exist already
*/
function ensureFolderAddress(folderAddress){
if (!fs.existsSync(folderAddress)) {
fs.mkdirSync(folderAddress);
}
return folderAddress;
}
/**
* Recursivly steps through all the child folders
* Takes a folder address (dir)
* Gets the pages in the given dir and runs the page builder on them
* Gets the folders within the given dir
* Passes each folder to itself.
*/
function stepIntoDir(dir, siteConfig){
var dirContents = filterContents(contents(dir), filters);
var pageList = pages(dirContents);
if (pageList.length > 0) {
ensureFolderAddress(srcToDistAddress(dir, siteConfig));
}
var pageAddressList = contentAddresses(dir, pageList);
buildPages(pageAddressList, siteConfig);
var fullAddressList = contentAddresses(dir, dirContents);
var folderAddressList = folders(fullAddressList);
folderAddressList.forEach(function(folderAddress){
stepIntoDir(folderAddress, siteConfig);
});
};
//stop the string template literal html tag from throwing an undefined
if (typeof html == 'undefined') {
html = function(strings, ...keys){
let returnString = '';
strings.forEach(function(str, i){
returnString += str;
if (typeof keys[i] !== 'undefined') {
returnString += keys[i];
}
});
return returnString;
}
}
/**
* Iterate over every given input / output!
* This is the beginning :)
*/
puts.forEach(function(put){
stepIntoDir(put.in, put);
});