forked from rosano/hyperdraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic-export.js
More file actions
232 lines (189 loc) · 6.97 KB
/
Copy pathstatic-export.js
File metadata and controls
232 lines (189 loc) · 6.97 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node
// Crawls every public route this app's Express server renders and writes
// the responses to `.static/`, alongside a full copy of the built `os-app`
// asset tree (which Express serves as-is), producing a self-contained
// static site suitable for GitHub Pages.
const fs = require('fs');
const path = require('path');
const http = require('http');
const { spawn } = require('child_process');
const kRootDirectory = __dirname;
const kOutputDirectory = path.join(kRootDirectory, '.static');
const kPort = process.env.PORT || '3000';
const kBaseURL = `http://localhost:${ kPort }`;
const OLSKStaticExportDiscoverRoutes = function () {
const controllerFiles = [];
(function walk(directory) {
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
if (entry.name.startsWith('__') || entry.name === 'node_modules') {
continue;
}
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
walk(fullPath);
continue;
}
if (entry.name === 'controller.js') {
controllerFiles.push(fullPath);
}
}
})(path.join(kRootDirectory, 'os-app'));
return controllerFiles.reduce(function (coll, filePath) {
const controllerModule = require(filePath);
if (typeof controllerModule.OLSKControllerRoutes !== 'function') {
return coll;
}
const routesResult = controllerModule.OLSKControllerRoutes();
const routesArray = Array.isArray(routesResult) ? routesResult : Object.values(routesResult);
return coll.concat(routesArray.filter(function (route) {
return (route.OLSKRouteMethod || 'get') === 'get'
&& !route.OLSKRouteRedirect
&& !route.OLSKRouteIsHidden
&& !route.OLSKRoutePath.startsWith('/stub/');
}));
}, []);
};
const OLSKStaticExportURLsForRoute = function (route) {
const basePath = route.OLSKRoutePath;
const languageCodes = Array.isArray(route.OLSKRouteLanguageCodes) ? route.OLSKRouteLanguageCodes : [];
// Pages link to explicit `/en/...` alternates (hreflang) even though the
// unprefixed path is the canonical default, so both must be crawled.
return [basePath].concat(languageCodes.map(function (languageCode) {
return basePath === '/' ? `/${ languageCode }` : `/${ languageCode }${ basePath }`;
}));
};
const OLSKStaticExportDestinationForURL = function (urlPath) {
if (urlPath === '/') {
return path.join(kOutputDirectory, 'index.html');
}
if (/\.[a-zA-Z0-9]+$/.test(urlPath)) {
return path.join(kOutputDirectory, urlPath);
}
return path.join(kOutputDirectory, urlPath, 'index.html');
};
const OLSKStaticExportFetch = function (urlPath) {
return new Promise(function (resolve, reject) {
http.get(`${ kBaseURL }${ urlPath }`, function (response) {
if (response.statusCode >= 300 && response.statusCode < 400) {
response.resume();
return resolve(null);
}
if (response.statusCode !== 200) {
response.resume();
return reject(new Error(`GET ${ urlPath } -> ${ response.statusCode }`));
}
const chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
});
response.on('end', function () {
resolve(Buffer.concat(chunks));
});
}).on('error', reject);
});
};
const OLSKStaticExportWaitForServer = async function () {
for (let attempt = 0; attempt < 120; attempt++) {
try {
await OLSKStaticExportFetch('/robots.txt');
return;
} catch (error) {
await new Promise(function (resolve) {
setTimeout(resolve, 500);
});
}
}
throw new Error('Server did not become ready in time');
};
const OLSKStaticExportFindRollupModuleDirectories = function () {
const moduleDirectories = [];
(function walk(directory) {
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
if (entry.name.startsWith('__') || entry.name === 'node_modules') {
continue;
}
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
walk(fullPath);
continue;
}
if (entry.name === 'rollup-start.js') {
moduleDirectories.push(directory);
}
}
})(path.join(kRootDirectory, 'os-app'));
return moduleDirectories;
};
const OLSKStaticExportCopyBuiltAssets = async function () {
// olsk-rollup has been observed to log "created ... __compiled/..." and
// exit for its last-built module before that module's output is fully
// flushed to disk, so a copy taken immediately after `npm run build`
// can silently miss it. Retry a few times before giving up loudly —
// better to fail the deploy than ship a page missing its script/style.
for (let attempt = 1; attempt <= 5; attempt++) {
fs.rmSync(kOutputDirectory, { recursive: true, force: true });
fs.cpSync(path.join(kRootDirectory, 'os-app'), kOutputDirectory, {
recursive: true,
});
const missing = OLSKStaticExportFindRollupModuleDirectories().filter(function (moduleDirectory) {
const relativePath = path.relative(path.join(kRootDirectory, 'os-app'), moduleDirectory);
return !fs.existsSync(path.join(kOutputDirectory, relativePath, '__compiled'));
});
if (!missing.length) {
break;
}
if (attempt === 5) {
throw new Error(`__compiled output missing after copy for: ${ missing.join(', ') }`);
}
console.warn(`__compiled missing for ${ missing.join(', ') }, retrying copy (attempt ${ attempt })`);
await new Promise(function (resolve) {
setTimeout(resolve, 1000);
});
}
// Without this, GitHub Pages treats folders like `_shared` and
// `__compiled` as Jekyll-reserved and silently excludes them from
// serving, 404ing every asset underneath.
fs.writeFileSync(path.join(kOutputDirectory, '.nojekyll'), '');
};
const OLSKStaticExportMain = async function () {
await OLSKStaticExportCopyBuiltAssets();
const urls = Array.from(OLSKStaticExportDiscoverRoutes().reduce(function (coll, route) {
OLSKStaticExportURLsForRoute(route).forEach(function (url) {
coll.add(url);
});
return coll;
}, new Set(['/robots.txt', '/manifest.json', '/sw.js'])));
const server = spawn('npm', ['start'], {
cwd: kRootDirectory,
// NODE_ENV=production disables the dev-only livereload script
// (OLSKExpress/main.js), which would otherwise get baked into
// every crawled page and trip mixed-content blocking once served
// over HTTPS.
env: Object.assign({}, process.env, { PORT: kPort, NODE_ENV: 'production' }),
stdio: 'inherit',
detached: true,
});
try {
await OLSKStaticExportWaitForServer();
for (const urlPath of urls) {
const body = await OLSKStaticExportFetch(urlPath);
if (body === null) {
console.warn(`skipped (redirect) ${ urlPath }`);
continue;
}
const destinationPath = OLSKStaticExportDestinationForURL(urlPath);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(destinationPath, body);
console.log(`saved ${ urlPath } -> ${ path.relative(kRootDirectory, destinationPath) }`);
}
} finally {
// `npm start` forks its own child process, so kill the whole group.
process.kill(-server.pid, 'SIGTERM');
}
};
OLSKStaticExportMain().then(function () {
process.exit(0);
}, function (error) {
console.error(error);
process.exit(1);
});