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

2
.gitignore vendored
View File

@@ -8,5 +8,5 @@ yarn-error.log
coverage
.nyc_output
coverage.lcov
/lib
dist/
db.json

View File

@@ -29,7 +29,7 @@
},
"dependencies": {
"axios": "^0.18.0",
"esm": "^3.0.17",
"esm": "^3.0.81",
"lodash": "^4.17.10",
"lowdb": "^1.0.0"
},

0
packages/search-site/dist/.empty vendored Normal file
View File

View File

@@ -2,4 +2,4 @@
require = require('esm')(module);
const mod = require('./src/index.mjs').default;
module.exports = mod;
mod();

View File

@@ -1,3 +1,3 @@
import mod from './src/index.mjs';
export default mod;
mod();

View File

@@ -6,8 +6,9 @@
"module": "index.mjs",
"description": "strain search static website",
"scripts": {
"start": "node .",
"build": "echo NO BUILD YET"
"start": "node . serve",
"build": "node . build",
"dev": "nodemon -i dist/ -w src -e mjs,ejs -x 'node . build'"
},
"author": "joe fleming (https://github.com/w33ble)",
"license": "MIT",
@@ -15,6 +16,10 @@
"cjs": true
},
"dependencies": {
"ejs": "^2.6.1",
"esm": "^3.0.81"
},
"devDependencies": {
"nodemon": "^1.18.4"
}
}

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);
}
}

View File

@@ -653,6 +653,10 @@ duplexer@~0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1"
ejs@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.6.1.tgz#498ec0d495655abc6f23cd61868d926464071aa0"
elegant-spinner@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/elegant-spinner/-/elegant-spinner-1.0.1.tgz#db043521c95d7e303fd8f345bedc3349cfb0729e"
@@ -824,7 +828,7 @@ eslint@^4.9.0:
table "4.0.2"
text-table "~0.2.0"
esm@^3.0.17, esm@^3.0.81:
esm@^3.0.81:
version "3.0.81"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.81.tgz#3d78df013960b6d30bb5a5adfafe5d9da654b9d2"