angular/aio/scripts/build-404-page.js
Derek Cormier d9f90b55f6 build(bazel): build aio 404 page under bazel
The legacy build injected this file into the dist folder, however that's
not possible under bazel so files are copied to a new directory.
2022-11-22 13:51:16 -07:00

31 lines
946 B
JavaScript

#!/usr/bin/env node
// Imports
const {readFileSync, writeFileSync} = require('fs');
const {join, resolve} = require('path');
// Constants
const SOURCE_404_BODY_PATH = resolve(process.argv[2]);
const BUILD_OUTPUT_DIR = resolve(process.argv[3]);
const DEST_404_PAGE_PATH = resolve(process.argv[4]);
// Run
_main();
// Functions - Definitions
function _main() {
const srcIndexPath = join(BUILD_OUTPUT_DIR, 'index.html');
const srcIndexContent = readFileSync(srcIndexPath, 'utf8');
const src404BodyContent = readFileSync(SOURCE_404_BODY_PATH, 'utf8').trim();
const dst404PageContent = srcIndexContent
.replace(/(<body>)[\s\S]+(<\/body>)/, `$1\n ${src404BodyContent}\n$2`);
if (dst404PageContent === srcIndexContent) {
throw new Error(
'Failed to generate \'404.html\'. ' +
'The content of \'index.html\' does not match the expected pattern.');
}
writeFileSync(DEST_404_PAGE_PATH, dst404PageContent);
}