Here is the solution. I'm sure it could use some refactoring but it suits my needs for now.
Note: The "replace-in-file" npm package needs to be installed but the "fs" package doesn't. From what I gather, this package is now part of Node.js by default. Correct me if i'm wrong.
const fs = require("fs");
const replace = require("replace-in-file");
// create dist directory
if (!fs.existsSync("dist")) {
fs.mkdirSync("dist", (error) => {
console.log("==> directory created");
if (error) {
console.log(error);
}
});
} else {
console.log("==> Directory already exists");
}
// create the dist/index.file if not already there
if (!fs.existsSync("dist/index.html")) {
fs.writeFileSync("dist/index.html", "blank file", (error) => {
console.log("==> index.html created");
if (error) {
console.log(error);
}
});
} else {
console.log("==> index.html already exists");
}
// copy the source index.html to the dist folder
fs.copyFile("src/index.html", "dist/index.html", (err) => {
if (err) {
console.log("Error Found:", err);
}
console.log("==> Index file contents copied");
});
// get list of files in src directory that start with "_"(underscore) which are the partial files
fs.readdir("./src/", (err, files) => {
if (err) {
console.log(err);
} else {
files.forEach((file) => {
if (file.startsWith("_")) {
// get the contents of the partial file and put it in a variable
fs.readFile("./src/" + file, (err, data) => {
if (err) throw err;
textData = data.toString();
// replace the placeholder text in the index.html file with the content from partial file
const options = {
files: "dist/index.html",
from: file,
to: textData,
};
try {
const results = replace.sync(options);
console.log("==> Replacement results:", results);
} catch (error) {
console.error("==> Error occurred:", error);
}
});
}
});
}
});
This is my directory structure for the test. Its basic.
project-name/src/index.html
project-name/src/_navigation.html
project-name/src/_cards.html
project-name/src/_footer.html
The index.html looks like this.
<!DOCTYPE html>
<html lang="en">
<head>
<title>PageTitle</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
</head>
<body>
<div class="container">
<h1 class="mt-5">Basic Template</h1>
<p class="lead">Simple template to start your design with.</p>
</div>
_navigation.html
_cards.html
_footer.html
</body>
</html>
I run it from the package.json with this script. I keep my node.js scripts in a utils folder in the root of the project.
"compile:html": "node utils/compile-html.js"
It basically creates the dist folder and an index.html file in that folder if not already there. Then it copies the contents of the src/index.html and pastes it into the dist/index.html. After that the script makes the changes to the dist/index.html and leaves the src/index.html alone. The script takes the contents of the partial files (e.g. _navigation.html) and replaces the file name text in the index.html (e.g. the _navigation.html line in the index.html file) with the content copied from the partial file.