jhan.com.au/build.js

56 lines
1.4 KiB
JavaScript

const fs = require('fs');
// const path = require('path');
const readline = require('readline')
const files = ["src/index.html",
"src/projects/2016-XXXX.html"];
files.forEach( file => {
// console.log(file + "...");
// console.log(file.split('/').slice(1).join('/') + "\n"); //remove /src
var file_buffer = "";
const rl = readline.createInterface({
input: fs.createReadStream(file),
output: process.stdout,
terminal: false
});
const re = new RegExp('{% include \'*(.*)\' %}');
rl.on('line', line => {
const match = line.match(re);
if (match) {
file_buffer += fs.readFileSync(match[1], 'utf8');
} else {
file_buffer += line + "\n";
}
});
rl.on('close', () => {
//fs.mkdirSync('build/' + file.split('/').slice(1,-1).join('/')); // create dirs
ensureDirectoryExists('build/' + file.split('/').slice(1).join('/'));
fs.writeFileSync('build/' + file.split('/').slice(1).join('/'), file_buffer, 'utf8');
});
});
// fs.cp('./src/projects','./build/projects', { recursive: true }, (err) => {
// if (err) throw err;
// });
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);
}