jhan.com.au/build.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-05-21 11:59:27 +00:00
const fs = require('fs');
2023-05-25 10:40:32 +00:00
const readline = require('readline');
2023-05-26 02:17:34 +00:00
const src_dir = 'src';
2023-05-25 10:40:32 +00:00
const build_dir = 'build';
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 => {
2023-05-26 02:17:34 +00:00
var file_buffer = "";
2023-05-26 02:17:34 +00:00
const rl = readline.createInterface({
2023-05-25 10:30:42 +00:00
input: fs.createReadStream(file),
output: process.stdout,
terminal: false
2023-05-26 02:17:34 +00:00
});
2023-05-26 02:17:34 +00:00
const re_include = new RegExp('{% include \'*(.*)\' %}');
const re_picture = new RegExp('{% picture.*%}');
2023-05-26 02:17:34 +00:00
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) {
2023-05-26 02:17:34 +00:00
const re_src = new RegExp("src=[\'\"](.*?)[\'\"]");
const picture_src = match_picture[0].match(re_src)[1].split('/').slice(1).join('/');
const re_alt = new RegExp("alt=[\'\"](.*?)[\'\"]");
const alt = match_picture[0].match(re_alt)[1];
2023-05-25 10:40:32 +00:00
ensureDirectoryExists(build_dir + '/' + picture_src);
2023-05-26 02:17:34 +00:00
fs.cp(src_dir + '/' + picture_src, build_dir + '/' + picture_src, { recursive: false }, (err) => {
2023-05-25 10:30:42 +00:00
if (err) throw err;
});
2023-05-26 02:17:34 +00:00
line = line.replace(re_picture, "<img src=\'/" + picture_src + "\' alt=\'" + alt + "\' \>");
2023-05-25 10:30:42 +00:00
}
file_buffer += line;
2023-05-26 02:17:34 +00:00
});
rl.on('close', () => {
ensureDirectoryExists(build_dir + '/' + file.split('/').slice(1).join('/'));
2023-05-25 10:40:32 +00:00
fs.writeFileSync(build_dir + '/' + 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)) {
return true;
}
ensureDirectoryExists(dirname);
fs.mkdirSync(dirname);
}