« get me outta code hell

static-build.js « build-modes « write « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write/build-modes/static-build.js
blob: 3f829adaf177b67c441847d7eaad69e3e1c2035c (plain)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import * as path from 'node:path';

import {
  copyFile,
  cp,
  mkdir,
  stat,
  symlink,
  writeFile,
  unlink,
} from 'node:fs/promises';

import {rimraf} from 'rimraf';

import {quickLoadContentDependencies} from '#content-dependencies';
import {quickEvaluate} from '#content-function';
import * as html from '#html';
import * as pageSpecs from '#page-specs';
import {empty, queue, withEntries} from '#sugar';

import {
  fileIssue,
  logError,
  logInfo,
  logWarn,
  progressPromiseAll,
} from '#cli';

import {
  getPagePathname,
  getURLsFrom,
  getURLsFromRoot,
} from '#urls';

import {bindUtilities} from '../bind-utilities.js';
import {generateRedirectHTML, generateRandomLinkDataJSON} from '../common-templates.js';

const pageFlags = Object.keys(pageSpecs);

export const description = `Generates all page content in one build (according to the contents of data files at build time) and writes them to disk, preparing the output folder for upload and serving by any static web host\n\nIntended for any production or public-facing release of a wiki; serviceable for local development, but can be a bit unwieldy and time/CPU-expensive`;

export const config = {
  fileSizes: {
    default: 'perform',
  },

  languageReloading: {
    applicable: false,
  },

  mediaValidation: {
    default: 'perform',
  },

  search: {
    default: 'perform',
  },

  thumbs: {
    default: 'perform',
  },

  webRoutes: {
    required: true,
  },
};

export function getCLIOptions() {
  return {
    // This is the output directory. It's the one you'll upload online with
    // rsync or whatever when you're pushing an upd8, and also the one
    // you'd archive if you wanted to make a 8ackup of the whole dang
    // site. Just keep in mind that the gener8ted result will contain a
    // couple symlinked directories, so if you're uploading, you're pro8a8ly
    // gonna want to resolve those yourself.
    'out-path': {
      help: `Specify path to output directory, into which HTML page files and other output are written and other directories are linked\n\nAlways required alongside --static-build mode, but may be provided via the HSMUSIC_OUT environment variable instead`,
      type: 'value',
    },

    // Working without a dev server and just using file:// URLs in your we8
    // 8rowser? This will automatically append index.html to links across
    // the site. Not recommended for production, since it isn't guaranteed
    // 100% error-free (and index.html-style links are less pretty anyway).
    'append-index-html': {
      help: `Apply "index.html" to the end of page links, instead of just linking to the directory (ex. "/track/ng2yu/"); useful when no local server hosting option is available and browsing build output directly off the disk drive\n\nDefinitely not intended for production: this option isn't extensively tested and may include conspicuous oddities`,
      type: 'flag',
    },

    // Only want to 8uild one language during testing? This can chop down
    // 8uild times a pretty 8ig chunk! Just pass a single language code.
    'lang': {
      help: `Skip rest and build only pages for this locale language (specify a language code)`,
      type: 'value',
    },

    // NOT for neatly ena8ling or disa8ling specific features of the site!
    // This is only in charge of what general groups of files to write.
    // They're here to make development quicker when you're only working
    // on some particular area(s) of the site rather than making changes
    // across all of them.
    ...withEntries(pageSpecs, entries => entries.map(
      ([key, spec]) => [key, {
        help: spec.description &&
          `Skip rest and build only:\n${spec.description}`,
        type: 'flag',
      }])),
  };
}

export async function go({
  cliOptions,
  mediaPath,
  queueSize,

  defaultLanguage,
  languages,
  missingImagePaths,
  srcRootPath,
  thumbsCache,
  urls,
  webRoutes,
  wikiData,

  developersComment: _developersComment,
  getSizeOfAdditionalFile,
  getSizeOfImagePath,
  niceShowAggregate,
}) {
  const outputPath = cliOptions['out-path'] || process.env.HSMUSIC_OUT;
  const appendIndexHTML = cliOptions['append-index-html'] ?? false;
  const writeOneLanguage = cliOptions['lang'] ?? null;

  if (!outputPath) {
    logError`Expected ${'--out-path'} option or ${'HSMUSIC_OUT'} to be set`;
    return false;
  }

  if (appendIndexHTML) {
    logWarn`Appending index.html to link hrefs. (Note: not recommended for production release!)`;
  }

  if (writeOneLanguage && !(writeOneLanguage in languages)) {
    logError`Specified to write only ${writeOneLanguage}, but there is no strings file with this language code!`;
    return false;
  } else if (writeOneLanguage) {
    logInfo`Writing only language ${writeOneLanguage} this run.`;
  } else {
    logInfo`Writing all languages.`;
  }

  const selectedPageFlags = Object.keys(cliOptions)
    .filter(key => pageFlags.includes(key));

  const writeAll = empty(selectedPageFlags) || selectedPageFlags.includes('all');
  logInfo`Writing site pages: ${writeAll ? 'all' : selectedPageFlags.join(', ')}`;

  await mkdir(outputPath, {recursive: true});

  await writeWebRouteSymlinks({
    outputPath,
    webRoutes,
  });

  await writeWebRouteCopies({
    outputPath,
    webRoutes,
  });

  if (writeAll) {
    await writeFavicon({
      mediaPath,
      outputPath,
    });

    await writeSharedFilesAndPages({
      outputPath,
      randomLinkDataJSON: generateRandomLinkDataJSON({wikiData}),
    });
  } else {
    logInfo`Skipping favicon and shared files (not writing all site pages).`
  }

  const buildSteps = writeAll
    ? Object.entries(pageSpecs)
    : Object.entries(pageSpecs)
        .filter(([flag]) => selectedPageFlags.includes(flag));

  let writes;
  {
    let error = false;

    // TODO: Port this to aggregate error
    writes = buildSteps
      .map(([flag, pageSpec]) => {
        if (pageSpec.condition && !pageSpec.condition({wikiData})) {
          return null;
        }

        const paths = [];

        if (pageSpec.pathsTargetless) {
          const result = pageSpec.pathsTargetless({wikiData});
          if (Array.isArray(result)) {
            paths.push(...result);
          } else {
            paths.push(result);
          }
        }

        if (pageSpec.targets) {
          if (!pageSpec.pathsForTarget) {
            logError`${flag + '.targets'} is specified, but ${flag + '.pathsForTarget'} is missing!`;
            error = true;
            return null;
          }

          const targets = pageSpec.targets({wikiData});

          if (!Array.isArray(targets)) {
            logError`${flag + '.targets'} was called, but it didn't return an array! (${targets})`;
            error = true;
            return null;
          }

          paths.push(...targets.flatMap(target => pageSpec.pathsForTarget(target)));
          // TODO: Validate each pathsForTargets entry
        }

        return paths;
      })
      .filter(Boolean)
      .flat();

    if (error) {
      return false;
    }
  }

  const pageWrites = writes.filter(({type}) => type === 'page');
  const dataWrites = writes.filter(({type}) => type === 'data');
  const redirectWrites = writes.filter(({type}) => type === 'redirect');

  if (writes.length) {
    logInfo`Total of ${writes.length} writes returned. (${pageWrites.length} page, ${dataWrites.length} data [currently skipped], ${redirectWrites.length} redirect)`;
  } else {
    logWarn`No writes returned at all, so exiting early. This is probably a bug!`;
    return false;
  }

  /*
  await progressPromiseAll(`Writing data files shared across languages.`, queue(
    dataWrites.map(({path, data}) => () => {
      const bound = {};

      bound.serializeLink = bindOpts(serializeLink, {});

      bound.serializeContribs = bindOpts(serializeContribs, {});

      bound.serializeImagePaths = bindOpts(serializeImagePaths, {
        thumb
      });

      bound.serializeCover = bindOpts(serializeCover, {
        [bindOpts.bindIndex]: 2,
        serializeImagePaths: bound.serializeImagePaths,
        urls
      });

      bound.serializeGroupsForAlbum = bindOpts(serializeGroupsForAlbum, {
        serializeLink
      });

      bound.serializeGroupsForTrack = bindOpts(serializeGroupsForTrack, {
        serializeLink
      });

      // TODO: This only supports one <>-style argument.
      return writeData(path[0], path[1], data({...bound}));
    }),
    queueSize
  ));
  */

  let errored = false;

  const contentDependencies = await quickLoadContentDependencies({
    showAggregate: niceShowAggregate,
  });

  const commonUtilities = {...universalUtilities};

  const perLanguageFn = async (language, i, entries) => {
    const baseDirectory =
      language === defaultLanguage ? '' : language.code;

    console.log(`\x1b[34;1m${`[${i + 1}/${entries.length}] ${language.code} (-> /${baseDirectory}) `.padEnd(60, '-')}\x1b[0m`);

    await progressPromiseAll(`Writing ${language.code}`, queue([
      ...pageWrites.map(page => () => {
        const pagePath = page.path;

        const pathname = getPagePathname({
          baseDirectory,
          pagePath,
          urls,
        });

        const to = getURLsFrom({
          baseDirectory,
          pagePath,
          urls,
        });

        const absoluteTo = getURLsFromRoot({
          baseDirectory,
          urls,
        });

        const bound = bindUtilities({
          ...commonUtilities,

          absoluteTo,
          language,
          pagePath,
          pagePathStringFromRoot: pathname,
          to,
        });

        let pageHTML, oEmbedJSON;
        try {
          const topLevelResult =
            quickEvaluate({
              contentDependencies,
              extraDependencies: {...bound, appendIndexHTML},

              name: page.contentFunction.name,
              args: page.contentFunction.args ?? [],
            });

          ({pageHTML, oEmbedJSON} = html.resolve(topLevelResult));
        } catch (error) {
          logError`\rError generating page: ${pathname}`;
          niceShowAggregate(error);
          errored = true;
          return;
        }

        return writePage({
          pageHTML,
          oEmbedJSON,
          outputDirectory: path.join(outputPath, getPagePathname({
            baseDirectory,
            device: true,
            pagePath,
            urls,
          })),
        });
      }),

      ...redirectWrites.map(({fromPath, toPath, title, getTitle}) => () => {
        title ??= getTitle?.({language});

        const to = getURLsFrom({
          baseDirectory,
          pagePath: fromPath,
          urls,
        });

        const target = to('localized.' + toPath[0], ...toPath.slice(1));
        const pageHTML = generateRedirectHTML(title, target, {language});

        return writePage({
          pageHTML,
          outputDirectory: path.join(outputPath, getPagePathname({
            baseDirectory,
            device: true,
            pagePath: fromPath,
            urls,
          })),
        });
      }),
    ], queueSize));
  };

  await wrapLanguages(perLanguageFn, {
    languages,
    writeOneLanguage,
  });

  // The single most important step.
  logInfo`Written!`;

  if (errored) {
    logWarn`The code generating content for some pages ended up erroring.`;
    logWarn`These pages were skipped, so if you ran a build previously and`;
    logWarn`they didn't error that time, then the old version is still`;
    logWarn`available - albeit possibly outdated! Please scroll up and send`;
    logWarn`the HSMusic developers a copy of the errors:`;
    fileIssue({topMessage: null});

    return false;
  }

  return true;
}

// Wrapper function for running a function once for all languages.
async function wrapLanguages(fn, {
  languages,
  writeOneLanguage = null,
}) {
  const k = writeOneLanguage;
  const languagesToRun = k ? {[k]: languages[k]} : languages;

  const entries = Object.entries(languagesToRun).filter(
    ([key]) => key !== 'default'
  );

  for (let i = 0; i < entries.length; i++) {
    const [_key, language] = entries[i];

    await fn(language, i, entries);
  }
}

async function writePage({
  pageHTML,
  oEmbedJSON = '',
  outputDirectory,
}) {
  await mkdir(outputDirectory, {recursive: true});

  await Promise.all([
    writeFile(path.join(outputDirectory, 'index.html'), pageHTML),

    oEmbedJSON &&
      writeFile(path.join(outputDirectory, 'oembed.json'), oEmbedJSON),
  ].filter(Boolean));
}

function writeWebRouteSymlinks({
  outputPath,
  webRoutes,
}) {
  const symlinkRoutes =
    webRoutes.filter(route => route.statically === 'symlink');

  const promises =
    symlinkRoutes.map(async route => {
      const parts = route.to.split('/');
      const parentDirectoryParts = parts.slice(0, -1);
      const symlinkNamePart = parts.at(-1);

      const parentDirectory = path.join(outputPath, ...parentDirectoryParts);
      const symlinkPath = path.join(parentDirectory, symlinkNamePart);

      try {
        await unlink(symlinkPath);
      } catch (error) {
        if (error.code !== 'ENOENT') {
          throw error;
        }
      }

      await mkdir(parentDirectory, {recursive: true});

      try {
        await symlink(route.from, symlinkPath);
      } catch (error) {
        if (error.code === 'EPERM') {
          await symlink(route.from, symlinkPath, 'junction');
        } else {
          throw error;
        }
      }
    });

  return progressPromiseAll(`Writing web route symlinks.`, promises);
}

async function writeWebRouteCopies({
  outputPath,
  webRoutes,
}) {
  const copyRoutes =
    webRoutes.filter(route => route.statically === 'copy');

  const promises =
    copyRoutes.map(async route => {
      const permissionName = '__hsmusic-ok-for-deletion.txt';

      const parts = route.to.split('/');
      const parentDirectoryParts = parts.slice(0, -1);
      const copyNamePart = parts.at(-1);

      const parentDirectory = path.join(outputPath, ...parentDirectoryParts);
      const copyPath = path.join(parentDirectory, copyNamePart);

      // We're going to do a rimraf call! This is freaking terrifying,
      // so nope out on a couple important conditions.

      let needsDelete;
      try {
        await stat(copyPath);
        needsDelete = true;
      } catch (error) {
        if (error.code === 'ENOENT') {
          needsDelete = false;
        } else {
          throw error;
        }
      }

      if (needsDelete) {
        // First remove it directly, in case it's a symlink.
        try {
          await unlink(copyPath);
          needsDelete = false;
        } catch (error) {
          // EPERM is POSIX, but libuv may or may not flat-out just raise
          // the system error (which is ostensibly EISDIR on Linux).
          // https://github.com/nodejs/node-v0.x-archive/issues/5791
          // https://man7.org/linux/man-pages/man2/unlink.2.html
          //
          // Both of these indidcate "a directory, probably" and we'll
          // still check for the deletion permission file where we expect
          // it before actually touching anything.
          if (error.code !== 'EPERM' && error.code !== 'EISDIR') {
            throw error;
          }
        }
      }

      if (needsDelete) {
        // Then check that the deletion permission file exists
        // where we expect it.
        try {
          await stat(path.join(copyPath, permissionName));
        } catch (error) {
          if (error.code === 'ENOENT') {
            throw new Error(`Couldn't find ${permissionName} in ${copyPath} - please delete or move away this folder manually`);
          } else {
            throw error;
          }
        }

        // And *then* actually delete that directory.
        await rimraf(copyPath);
      }

      // Actually copy the source path where it's wanted.
      await cp(route.from, copyPath, {recursive: true});

      // And certify that it's OK to delete this path, next time around.
      await writeFile(path.join(copyPath, permissionName),
        `The presence of this file (by its name, not its contents)\n` +
        `indicates hsmusic may delete everything contained in this\n` +
        `directory (the one which directly contains this file, *not*\n` +
        `any further-up parent directories).\n` +
        `\n` +
        `If you make edits, or add any files, they will be deleted or\n` +
        `overwritten the next time you run the build.\n` +
        `\n` +
        `If you delete *this* file, hsmusic will error during the next\n` +
        `build, and will ask that you delete the containing directory\n` +
        `yourself.\n`);
    });

  const results =
    await Promise.allSettled(promises);

  const errors =
    results
      .filter(({status}) => status === 'rejected')
      .map(({reason}) => reason)
      .map(err =>
        (err.message.startsWith(`Couldn't find`)
          ? err.message
          : err));

  if (empty(errors)) {
    logInfo`Wrote web route copies.`;
  } else {
    throw new AggregateError(errors, `Errors copying internal files ("web routes")`);
  }
}

async function writeFavicon({
  mediaPath,
  outputPath,
}) {
  const faviconFile = 'favicon.ico';

  try {
    await stat(path.join(mediaPath, faviconFile));
  } catch (error) {
    return;
  }

  try {
    await copyFile(
      path.join(mediaPath, faviconFile),
      path.join(outputPath, faviconFile));
  } catch (error) {
    logWarn`Failed to copy favicon! ${error.message}`;
    return;
  }

  logInfo`Copied favicon to site root.`;
}

async function writeSharedFilesAndPages({
  outputPath,
  randomLinkDataJSON,
}) {
  return progressPromiseAll(`Writing files & pages shared across languages.`, [
    randomLinkDataJSON &&
      writeFile(
        path.join(outputPath, 'random-link-data.json'),
        randomLinkDataJSON),
  ].filter(Boolean));
}