angular/aio/scripts/build-404-page.mjs
Paul Gschwendtner 13d3039c7d refactor: convert AIO tooling scripts used in Bazel to ESM (#48521)
Since the Bazel setup in this repo will now always use ESM,
the tooling scripts/binaries in AIO need to be switched to ESM
too. Most of the scripts are already ESM, but a few had to be converted.

Note that the Dgeni generation does not use ESM because it's unaffected
and the Dgeni CLI is used. In the future we could also update the Dgeni
setup to ESM but there is no need currently.

PR Close #48521
2022-12-19 19:50:44 +00:00

34 lines
940 B
JavaScript

#!/usr/bin/env node
// Imports
import {readFileSync, writeFileSync} from 'fs';
import {join, resolve} from '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);
}