forked from mdn/yari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
449 lines (406 loc) · 12.8 KB
/
cli.ts
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import zlib from "zlib";
import chalk from "chalk";
import cliProgress from "cli-progress";
const program = require("@caporal/core").default;
import { prompt } from "inquirer";
import { Document, slugToFolder, translationsOf } from "../content";
import { CONTENT_ROOT, CONTENT_TRANSLATED_ROOT } from "../libs/env";
import { VALID_LOCALES } from "../libs/constants";
// eslint-disable-next-line node/no-missing-require
import { renderHTML } from "../ssr/dist/main";
import options from "./build-options";
import { buildDocument, BuiltDocument, renderContributorsTxt } from ".";
import { Flaws } from "../libs/types";
import * as bcd from "@mdn/browser-compat-data/types";
import SearchIndex from "./search-index";
import { BUILD_OUT_ROOT } from "../libs/env";
import { makeSitemapXML, makeSitemapIndexXML } from "./sitemaps";
import { humanFileSize } from "./utils";
export type DocumentBuild = SkippedDocumentBuild | InteractiveDocumentBuild;
export interface SkippedDocumentBuild {
doc: {};
skip: true;
}
export interface InteractiveDocumentBuild {
document: any;
doc: BuiltDocument;
skip: false;
}
async function buildDocumentInteractive(
documentPath,
interactive,
invalidate = false
): Promise<SkippedDocumentBuild | InteractiveDocumentBuild> {
try {
const document = invalidate
? Document.read(documentPath, Document.MEMOIZE_INVALIDATE)
: Document.read(documentPath);
if (!document) {
throw new Error(`${documentPath} could not be read`);
}
if (!interactive) {
const translations = translationsOf(document.metadata);
if (translations && translations.length > 0) {
document.translations = translations;
} else {
document.translations = [];
}
}
return { document, doc: await buildDocument(document), skip: false };
} catch (e) {
if (!interactive) {
throw e;
}
console.error(e);
const { action } = await prompt([
{
type: "list",
message: "What to do?",
name: "action",
choices: [
{ name: "re-run", value: "r" },
{ name: "skip", value: "s" },
{ name: "quit", value: "q" },
],
default: "r",
},
]);
if (action === "r") {
return await buildDocumentInteractive(documentPath, interactive, true);
}
if (action === "s") {
return { doc: {}, skip: true };
}
throw e;
}
}
export interface BuiltDocuments {
slugPerLocale: Record<
string,
{
slug: string;
modified: string;
}[]
>;
peakHeapBytes: number;
totalFlaws: any;
}
async function buildDocuments(
files: string[] = null,
quiet = false,
interactive = false,
noHTML = false,
locales: Map<string, string> = new Map()
): Promise<BuiltDocuments> {
// If a list of files was set, it came from the CLI.
// Override whatever was in the build options.
const findAllOptions = {
...options,
locales,
};
if (files) {
findAllOptions.files = new Set(files);
}
const documents = Document.findAll(findAllOptions);
const progressBar = new cliProgress.SingleBar(
{},
cliProgress.Presets.shades_grey
);
const docPerLocale = {};
const searchIndex = new SearchIndex();
if (!documents.count) {
throw new Error("No documents to build found");
}
let peakHeapBytes = 0;
// For keeping track of the total counts of flaws
const totalFlaws = new Map<string, number>();
function appendTotalFlaws(flaws: Flaws) {
for (const [key, actualFlaws] of Object.entries(flaws)) {
const count = actualFlaws.length;
if (!totalFlaws.has(key)) {
totalFlaws.set(key, 0);
}
totalFlaws.set(key, (totalFlaws.get(key) as number) + count);
}
}
if (!options.noProgressbar) {
progressBar.start(documents.count);
}
for (const documentPath of documents.iter({
pathOnly: true,
}) as Iterable<string>) {
const result = await buildDocumentInteractive(documentPath, interactive);
const isSkippedDocumentBuild = (result): result is SkippedDocumentBuild =>
result.skip !== false;
if (isSkippedDocumentBuild(result)) {
continue;
}
const {
doc: { doc: builtDocument, liveSamples, fileAttachments, bcdData },
document,
} = result;
const outPath = path.join(BUILD_OUT_ROOT, slugToFolder(document.url));
fs.mkdirSync(outPath, { recursive: true });
if (builtDocument.flaws) {
appendTotalFlaws(builtDocument.flaws);
}
if (!noHTML) {
fs.writeFileSync(
path.join(outPath, "index.html"),
renderHTML(document.url, { doc: builtDocument })
);
}
fs.writeFileSync(
path.join(outPath, "index.json"),
// This is exploiting the fact that renderHTML has the side-effect of
// mutating the built document which makes this not great and refactor-worthy.
JSON.stringify({ doc: builtDocument })
);
fs.writeFileSync(
path.join(outPath, "contributors.txt"),
renderContributorsTxt(
document.metadata.contributors,
builtDocument.source.github_url.replace("/blob/", "/commits/")
)
);
for (const { url, data } of bcdData) {
fs.writeFileSync(
path.join(outPath, path.basename(url)),
JSON.stringify(data, (key, value) => {
// The BCD data object contains a bunch of data we don't need in the
// React component that loads the `bcd.json` file and displays it.
// The `.releases` block contains information about browsers (e.g
// release dates) and that part has already been extracted and put
// next to each version number where appropriate.
// Therefore, we strip out all "retired" releases.
if (key === "releases") {
return Object.fromEntries(
Object.entries(value as bcd.ReleaseStatement).filter(
([, v]) => v.status !== "retired"
)
);
}
return value;
})
);
}
for (const { id, html } of liveSamples) {
const liveSamplePath = path.join(outPath, `_sample_.${id}.html`);
fs.writeFileSync(liveSamplePath, html);
}
for (const filePath of fileAttachments) {
// We *could* use symlinks instead. But, there's no point :)
// Yes, a symlink is less disk I/O but it's nominal.
fs.copyFileSync(filePath, path.join(outPath, path.basename(filePath)));
}
// Collect active documents' slugs to be used in sitemap building and
// search index building.
if (!builtDocument.noIndexing) {
const { locale, slug } = document.metadata;
if (!docPerLocale[locale]) {
docPerLocale[locale] = [];
}
docPerLocale[locale].push({
slug,
modified: document.metadata.modified,
});
searchIndex.add(document);
}
if (!options.noProgressbar) {
progressBar.increment();
} else if (!quiet) {
console.log(outPath);
}
const heapBytes = process.memoryUsage().heapUsed;
if (heapBytes > peakHeapBytes) {
peakHeapBytes = heapBytes;
}
}
if (!options.noProgressbar) {
progressBar.stop();
}
const sitemapsBuilt: string[] = [];
for (const [locale, docs] of Object.entries(docPerLocale)) {
const sitemapDir = path.join(
BUILD_OUT_ROOT,
"sitemaps",
locale.toLowerCase()
);
fs.mkdirSync(sitemapDir, { recursive: true });
const sitemapFilePath = path.join(sitemapDir, "sitemap.xml.gz");
fs.writeFileSync(
sitemapFilePath,
zlib.gzipSync(makeSitemapXML(locale, docs))
);
}
searchIndex.sort();
for (const [locale, items] of Object.entries(searchIndex.getItems())) {
fs.writeFileSync(
path.join(BUILD_OUT_ROOT, locale.toLowerCase(), "search-index.json"),
JSON.stringify(items)
);
}
return { slugPerLocale: docPerLocale, peakHeapBytes, totalFlaws };
}
function formatTotalFlaws(flawsCountMap, header = "Total_Flaws_Count") {
if (!flawsCountMap.size) {
return "";
}
const keys = [...flawsCountMap.keys()];
const longestKey = Math.max(...keys.map((k) => k.length));
const out = ["\n"];
out.push(header);
for (const key of keys.sort()) {
out.push(
`${key.padEnd(longestKey + 1)} ${flawsCountMap.get(key).toLocaleString()}`
);
}
out.push("\n");
return out.join("\n");
}
program
.name("build")
.option("-i, --interactive", "Ask what to do when encountering flaws", {
default: false,
})
.option("-n, --nohtml", "Do not build index.html", {
default: false,
})
.option("-l, --locale <locale...>", "Filtered specific locales", {
default: [],
validator: [...VALID_LOCALES.keys()],
})
.option("--not-locale <locale...>", "Exclude specific locales", {
default: [],
validator: [...VALID_LOCALES.keys()],
})
.option("--sitemap-index", "Build a sitemap index file", {
default: false,
})
.argument("[files...]", "specific files to build")
.action(async ({ args, options }) => {
try {
if (!options.quiet) {
const roots = [
["CONTENT_ROOT", CONTENT_ROOT],
["CONTENT_TRANSLATED_ROOT", CONTENT_TRANSLATED_ROOT],
];
for (const [key, value] of roots) {
console.log(
`${chalk.grey((key + ":").padEnd(25, " "))}${
value ? chalk.white(value) : chalk.grey("not set")
}`
);
}
}
if (options.sitemapIndex) {
if (!options.quiet) {
console.log(chalk.yellow("Building sitemap index file..."));
}
const sitemapsBuilt = [];
const locales = [];
for (const locale of VALID_LOCALES.keys()) {
const sitemapFilePath = path.join(
BUILD_OUT_ROOT,
"sitemaps",
locale,
"sitemap.xml.gz"
);
if (fs.existsSync(sitemapFilePath)) {
sitemapsBuilt.push(sitemapFilePath);
locales.push(locale);
}
}
const sitemapIndexFilePath = path.join(BUILD_OUT_ROOT, "sitemap.xml");
fs.writeFileSync(
sitemapIndexFilePath,
makeSitemapIndexXML(
sitemapsBuilt.map((fp) => fp.replace(BUILD_OUT_ROOT, ""))
)
);
if (!options.quiet) {
console.log(
chalk.green(
`Sitemap index file built with locales: ${locales.join(", ")}.`
)
);
}
return;
}
const { files } = args;
let locales = new Map();
if (options.notLocale && options.notLocale.length > 0) {
if (options.locale && options.locale.length) {
throw new Error(
"Can't use --not-locale and --locale at the same time"
);
}
const notLocales = Array.isArray(options.notLocale)
? options.notLocale
: [options.notLocale];
locales = new Map(
[...VALID_LOCALES.keys()]
.filter((locale) => !notLocales.includes(locale))
.map((locale) => [locale, true])
);
} else {
// 'true' means we include this locale and all others get excluded.
// Some day we might make it an option to set `--not-locale` to
// filter out specific locales.
locales = new Map(
// The `options.locale` is either an empty array (e.g. no --locale used),
// a string (e.g. one single --locale) or an array of strings
// (e.g. multiple --locale options).
(Array.isArray(options.locale)
? options.locale
: [options.locale]
).map((locale) => [locale, true])
);
}
const t0 = new Date();
const { slugPerLocale, peakHeapBytes, totalFlaws } = await buildDocuments(
files,
Boolean(options.quiet),
Boolean(options.interactive),
Boolean(options.nohtml),
locales
);
const t1 = new Date();
const count = Object.values(slugPerLocale).reduce(
(a, b) => a + b.length,
0
);
const seconds = (t1.getTime() - t0.getTime()) / 1000;
const took =
seconds > 60
? `${(seconds / 60).toFixed(1)} minutes`
: `${seconds.toFixed(1)} seconds`;
if (!options.quiet) {
console.log(
chalk.green(
`Built ${count.toLocaleString()} pages in ${took}, at a rate of ${(
count / seconds
).toFixed(1)} documents per second.`
)
);
if (locales.size) {
console.log(
chalk.yellow(
`(only building locales: ${[...locales.keys()].join(", ")})`
)
);
}
console.log(`Peak heap memory usage: ${humanFileSize(peakHeapBytes)}`);
console.log(formatTotalFlaws(totalFlaws));
}
} catch (error) {
// So you get a stacktrace in the CLI output
console.error(error);
// So that the CLI ultimately fails
throw error;
}
});
program.run();