jhan.com.au/build.js

57 lines
1.7 KiB
JavaScript

const fs = require('fs');
const readline = require('readline')
const files = ["src/index.html",
"src/about.html",
"src/projects/2016-XXXX.html",
"src/projects/2023-Pepperstone.html"]; // this list should be generated.
files.forEach( file => {
var file_buffer = "";
const rl = readline.createInterface({
input: fs.createReadStream(file),
output: process.stdout,
terminal: false
});
const re_include = new RegExp('{% include \'*(.*)\' %}');
const re_picture = new RegExp('{% picture \'*(.*)\' %}');
rl.on('line', line => {
const match_include = line.match(re_include);
const match_picture = line.match(re_picture);
if (match_include) {
line = line.replace(re_include, fs.readFileSync(match_include[1], 'utf8'));
}
if (match_picture) {
const picture_src = match_picture[1].split('/').slice(1).join('/');
ensureDirectoryExists('build/' + picture_src);
fs.cp(match_picture[1], 'build/' + picture_src, { recursive: false }, (err) => {
if (err) throw err;
});
line = line.replace(re_picture, "<img src=\'/" + picture_src + "\' \>");
}
file_buffer += line;
});
rl.on('close', () => {
ensureDirectoryExists('build/' + file.split('/').slice(1).join('/')); // all this quatch is to remove the /src/.
fs.writeFileSync('build/' + file.split('/').slice(1).join('/'), file_buffer, 'utf8');
});
});
fs.cp('./src/styles','./build/styles', { recursive: true }, (err) => {
if (err) throw err;
});
function ensureDirectoryExists(filePath) {
var dirname = filePath.split('/').slice(0,-1).join('/');
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExists(dirname);
fs.mkdirSync(dirname);
}