-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
166 lines (136 loc) · 5.23 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
import fs from "node:fs";
import chalk from "chalk";
import { config } from "./_config.js";
import { getPost } from "./data/post.js";
import { getSourceByURL,
getError404HTML } from "./get-source/by-url.js";
import { copyFolder } from "./helpers/copy.js";
const GENERATED_FILES_FOLDER = `./${config.buildFolder}`;
async function createFile({ pageURL, filename, output, mkdirp }) {
const writePath = GENERATED_FILES_FOLDER + pageURL;
try {
const folder = await mkdirp(writePath);
fs.writeFileSync(`${writePath}/${filename ? filename : "index.html"}`, output, 'utf8', (err) => {
if (err) {
console.log(err);
}
});
} catch(err) {
console.error(err);
}
}
function removeFile({ pageURL, filename }) {
const writePath = GENERATED_FILES_FOLDER + pageURL;
const path = `${writePath}/${filename ? filename : "index.html"}`;
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
}
async function buildStaticFiles({ mkdirp }) {
console.log(`📂 Preparing static files`);
for (let folder of config.staticFolders) {
const folderWithoutLeadingUnderscore = folder.replace(/^_/, "");
const source = `./${folder}`;
const destination = `${GENERATED_FILES_FOLDER}/${folderWithoutLeadingUnderscore}`;
await copyFolder({ source, destination, mkdirp }).catch(err => { throw err; });
}
// _public is a general folder for any static file to be served from “/”
await copyFolder({
source: `./_public`,
destination: GENERATED_FILES_FOLDER,
mkdirp
}).catch(err => { throw err; });
// _pictures is a special case, to emulate WordPress URLs
await copyFolder({
source: `./_pictures`,
destination: `${GENERATED_FILES_FOLDER}/wp-content/uploads/`,
mkdirp
}).catch(err => { throw err; });
}
function buildPages({ urls, askSearchEnginesNotToIndex, mkdirp, DOMParser }) {
console.log(`📗 Preparing pages`);
for (let url of urls) {
getSourceByURL({ url, askSearchEnginesNotToIndex, DOMParser })
.then(html => createFile({
pageURL: url,
output: html,
mkdirp
}))
.catch(err => console.error(err));
}
}
async function buildRobotsText({ askSearchEnginesNotToIndex, mkdirp, DOMParser }) {
console.log(`🤖 Preparing robots.txt`);
getSourceByURL({ url: "/robots.txt", askSearchEnginesNotToIndex, DOMParser })
.then(text => createFile({
pageURL: "/",
filename: "robots.txt",
output: text,
mkdirp
}))
.catch(err => console.error(err));
}
function buildSiteMap({ askSearchEnginesNotToIndex, mkdirp, DOMParser }) {
console.log(`🗺 Preparing sitemap.xml`);
getSourceByURL({ url: "/sitemap.xml", askSearchEnginesNotToIndex, DOMParser })
.then(xml => createFile({
pageURL: "/",
filename: "sitemap.xml",
output: xml,
mkdirp
}))
.catch(err => console.error(err));
}
function buildRedirectsFile({ askSearchEnginesNotToIndex, mkdirp, DOMParser, urls }) {
console.log(`🗺 Preparing _redirects`);
const postURLs = urls.filter(url => getPost(url) != null);
getSourceByURL({ url: "/_redirects", askSearchEnginesNotToIndex, DOMParser, postURLs })
.then(text => createFile({
pageURL: "/",
filename: "_redirects",
output: text,
mkdirp
}))
.catch(err => console.error(err));
}
function buildError404Page({ askSearchEnginesNotToIndex, mkdirp, DOMParser }) {
console.log(`🚥 Preparing 404 "not found" page`);
createFile({
pageURL: "/",
filename: "404.html",
output: getError404HTML({ askSearchEnginesNotToIndex, DOMParser }),
mkdirp
});
}
async function build({ urls, env, mkdirp, DOMParser }) {
console.log(urls);
const askSearchEnginesNotToIndex = env.ASK_SEARCH_ENGINES_NOT_TO_INDEX;
buildPages({ urls, askSearchEnginesNotToIndex, mkdirp, DOMParser });
await buildStaticFiles({ mkdirp });
if (askSearchEnginesNotToIndex != 1 && config.host) {
buildRobotsText({ askSearchEnginesNotToIndex, mkdirp, DOMParser });
buildSiteMap({ askSearchEnginesNotToIndex, mkdirp, DOMParser });
} else {
if (askSearchEnginesNotToIndex == 1) console.log("⚠️ ", chalk.italic("askSearchEnginesNotToIndex"), "is set to", chalk.italic(true), "in environment");
if (!config.host) console.log("⚠️ ", chalk.italic("host"), "is not set in", chalk.italic("_config.js"));
console.log("⚠️ Skipping sitemap.xml");
removeFile({ pageURL: "/", filename: "sitemap.xml" });
console.log("⚠️ Skipping robots.txt");
removeFile({ pageURL: "/", filename: "robots.txt" });
}
buildRedirectsFile({ askSearchEnginesNotToIndex, mkdirp, DOMParser, urls });
buildError404Page({ askSearchEnginesNotToIndex, mkdirp, DOMParser });
console.log("");
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("🏁", chalk.cyan(`Build files saved to`), chalk.italic(config.buildFolder));
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("");
}
console.log("");
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("⏱️ ", chalk.cyan("Starting build"));
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("");
export {
build,
}