chore: convert everything to vue sfc

This commit is contained in:
2018-09-08 16:25:36 -07:00
parent 5e47f71f31
commit 9c4641dd0a
9 changed files with 495 additions and 516 deletions

View File

@@ -1,105 +1,33 @@
import http from 'http';
import fs from 'fs';
import ejs from 'ejs';
import Vue from 'vue';
import Router from 'vue-router';
const srcFile = 'src/index.ejs';
const destFile = 'dist/index.html';
Vue.use(Router);
function getData() {
return new Promise((resolve, reject) => {
fs.readFile('../scraper/db.json', (err, str) => {
if (err) reject(err);
else resolve(JSON.parse(str));
});
});
}
Vue.filter('capitalize', value => {
if (!value) return '';
const v = value.toString();
return v.charAt(0).toUpperCase() + v.slice(1);
});
async function build() {
const data = await getData();
const options = {};
Vue.filter('round', (value, digits = 2) => {
const v = parseFloat(value, 10);
return Math.round(v * (10 * digits)) / (10 * digits);
});
// calculate adjusted ratings
const totalMean =
data.strains.reduce((acc, strain) => acc + strain.rating + strain.rating_count, 0) /
data.strains.length;
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: () => import(/* webpackChunkName: "homeapp" */ './pages/Home.vue'),
},
],
});
data.strains = data.strains.map(strain => {
const minRatings = 10;
const { rating, rating_count: count } = strain;
return Object.assign(strain, {
rating_adjusted:
(((count / (count + minRatings)) * rating) / (minRatings / (count + minRatings))) *
totalMean,
});
});
const app = new Vue({
router,
render: h => h('div', { attrs: { id: 'app' } }, [h('router-view')]),
});
// order strains by rating
data.strains = data.strains.sort((n, strain) => {
if (strain.rating_adjusted === n.rating_adjusted) return 0;
return strain.rating_adjusted < n.rating_adjusted ? -1 : 1;
});
return new Promise((resolve, reject) => {
ejs.renderFile(srcFile, { data: JSON.stringify(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);
}
}
export default app;