jhan.com.au/build.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-05-21 11:59:27 +00:00
const fs = require('fs');
const readline = require('readline')
2023-05-21 11:59:27 +00:00
const files = ["src/index.html",
2023-05-25 10:30:42 +00:00
"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({
2023-05-25 10:30:42 +00:00
input: fs.createReadStream(file),
output: process.stdout,
terminal: false
});
2023-05-25 10:30:42 +00:00
const re_include = new RegExp('{% include \'*(.*)\' %}');
const re_picture = new RegExp('{% picture \'*(.*)\' %}');
rl.on('line', line => {
2023-05-25 10:30:42 +00:00
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', () => {
2023-05-25 10:30:42 +00:00
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');
});
2023-05-21 11:59:27 +00:00
});
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)) {
2023-05-25 10:30:42 +00:00
return true;
}
ensureDirectoryExists(dirname);
fs.mkdirSync(dirname);
}