2018-04-05 11:24:37 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
// Imports
|
2022-12-14 09:59:42 +00:00
|
|
|
import {readFileSync, writeFileSync} from 'fs';
|
|
|
|
|
import {join, resolve} from 'path';
|
2018-04-05 11:24:37 +00:00
|
|
|
|
|
|
|
|
// Constants
|
2022-08-16 04:44:19 +00:00
|
|
|
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]);
|
2018-04-05 11:24:37 +00:00
|
|
|
|
|
|
|
|
// Run
|
2019-01-19 13:29:42 +00:00
|
|
|
_main();
|
2018-04-05 11:24:37 +00:00
|
|
|
|
|
|
|
|
// Functions - Definitions
|
|
|
|
|
function _main() {
|
2022-08-16 04:44:19 +00:00
|
|
|
const srcIndexPath = join(BUILD_OUTPUT_DIR, 'index.html');
|
2018-04-05 11:24:37 +00:00
|
|
|
|
|
|
|
|
const srcIndexContent = readFileSync(srcIndexPath, 'utf8');
|
2022-08-16 04:44:19 +00:00
|
|
|
const src404BodyContent = readFileSync(SOURCE_404_BODY_PATH, 'utf8').trim();
|
2022-12-14 09:59:42 +00:00
|
|
|
const dst404PageContent = srcIndexContent.replace(
|
|
|
|
|
/(<body>)[\s\S]+(<\/body>)/,
|
|
|
|
|
`$1\n ${src404BodyContent}\n$2`
|
|
|
|
|
);
|
2018-04-05 11:24:37 +00:00
|
|
|
|
|
|
|
|
if (dst404PageContent === srcIndexContent) {
|
|
|
|
|
throw new Error(
|
2022-12-14 09:59:42 +00:00
|
|
|
"Failed to generate '404.html'. " +
|
|
|
|
|
"The content of 'index.html' does not match the expected pattern."
|
|
|
|
|
);
|
2018-04-05 11:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-16 04:44:19 +00:00
|
|
|
writeFileSync(DEST_404_PAGE_PATH, dst404PageContent);
|
2018-04-05 11:24:37 +00:00
|
|
|
}
|