feat: simple site build and dev server

This commit is contained in:
2018-08-30 19:52:58 -07:00
parent 37dcabde5e
commit 699a03c7c0
9 changed files with 106 additions and 9 deletions

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Strain Search</title>
</head>
<body>
<noscript><h1>You're going to want to enable JavaScript</h1></noscript>
<h1>Hosted Stuff</h1>
<script src="https://unpkg.com/lunr@2.3.1/lunr.js"></script>
</body>
</html>

View File

@@ -1 +1,75 @@
// package code goes here
import http from 'http';
import fs from 'fs';
import ejs from 'ejs';
const srcFile = 'src/index.html';
const destFile = 'dist/index.html';
function build() {
const data = {};
const options = {};
return new Promise((resolve, reject) => {
ejs.renderFile(srcFile, data, options, (err, str) => {
if (err) reject(err);
else {
fs.writeFile(destFile, str, er => {
if (er) reject(er);
else {
console.log(`Site built: ${destFile}`);
resolve();
}
});
}
});
});
}
async function serve() {
const PORT = '3000';
await build();
http
.createServer((req, res) => {
fs.readFile(destFile, (err, str) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Failed :(');
console.error(err);
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(str);
});
})
.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
}
export default async function() {
const cmds = ['build', 'serve'];
const cmd = process.argv.splice(2)[0];
try {
switch (cmd) {
case 'build': {
await build();
break;
}
case 'serve': {
await serve();
break;
}
default: {
const msg = `Please use one of ${cmds.map(c => `"${c}"`).join(', ')}`;
if (cmd.length) console.error(`Unknown command "${cmd}". ${msg}`);
else console.error(`No command provided. ${msg}`);
}
}
} catch (err) {
console.error(err);
}
}