Added linting to the project

This commit is contained in:
Robert Webb 2019-02-10 16:07:09 -08:00
parent ed33f010d5
commit 3f8e8479d0
16 changed files with 3248 additions and 122 deletions

View File

@ -1,42 +1,43 @@
const router = require('express').Router() const router = require('express').Router();
const { Article } = require('../db/models') const { Article } = require('../db/models');
const buildPage = require('./buildPage') const buildPage = require('./buildPage');
module.exports = router
module.exports = router;
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
const articles = await Article.findAll() const articles = await Article.findAll();
const tbl = articles const tbl = articles
.map( .map(
article => `<tr><td>${article.title}</td><td>${article.link}</td></tr>` article => `<tr><td>${article.title}</td><td>${article.link}</td></tr>`
) )
.join() .join();
const page = buildPage(tbl) const page = buildPage(tbl);
console.log(page) console.log(page);
res.status(201).send(page) res.status(201).send(page);
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });
router.get('/:id', async (req, res, next) => { router.get('/:id', async (req, res, next) => {
try { try {
const article = await Article.findById(req.params.id) const article = await Article.findById(req.params.id);
console.log(article.title) console.log(article.title);
console.log('by: ' + article.author) console.log(`by: ${article.author}`);
console.log(article.text) console.log(article.text);
res.status(201).send(article) res.status(201).send(article);
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });
router.post('/', async (req, res, next) => { router.post('/', async (req, res, next) => {
const body = req.body const body = req.body;
try { try {
const article = await Article.create(body) const article = await Article.create(body);
res.redirect(article.id) res.redirect(article.id);
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });

View File

@ -19,4 +19,4 @@ module.exports = listString =>
</body> </body>
</html> </html>
` `;

View File

@ -1,19 +1,20 @@
const router = require('express').Router() const router = require('express').Router();
module.exports = router
router.use('/items', require('./items')) module.exports = router;
router.use('/articles', require('./articles'))
router.use('/items', require('./items'));
router.use('/articles', require('./articles'));
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
res.send('/n-------/nHello from Express!/n--------/n') res.send('/n-------/nHello from Express!/n--------/n');
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });
router.use((req, res, next) => { router.use((req, res, next) => {
const error = new Error('Not Found!!!!!!!') const error = new Error('Not Found!!!!!!!');
error.status = 404 error.status = 404;
next(error) next(error);
}) });

View File

@ -1,23 +1,24 @@
const router = require('express').Router() const router = require('express').Router();
const { Item } = require('../db/models') const { Item } = require('../db/models');
module.exports = router
module.exports = router;
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
const items = await Item.findAll() const items = await Item.findAll();
res.status(201).send(items) res.status(201).send(items);
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });
router.post('/', async (req, res, next) => { router.post('/', async (req, res, next) => {
try { try {
const item = await Item.create(req.body) const item = await Item.create(req.body);
res.status(201).json(item) res.status(201).json(item);
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });

View File

@ -1,12 +1,13 @@
const router = require('express').Router() const router = require('express').Router();
module.exports = router
module.exports = router;
// what you will hit at /api/robots // what you will hit at /api/robots
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
console.log('NSA was here') console.log('NSA was here');
res.status(201).send('FUCK YOU NSA\n') res.status(201).send('FUCK YOU NSA\n');
} catch (err) { } catch (err) {
next(err) next(err);
} }
}) });

View File

@ -21,6 +21,6 @@ const ascii = String.raw`
____^/\___^--____/\____O______________/\/\---/\___________-- ____^/\___^--____/\____O______________/\/\---/\___________--
/\^ ^ ^ ^ ^^ ^ '\ ^ ^ /\^ ^ ^ ^ ^^ ^ '\ ^ ^
-- - -- - - --- __ ^ -- - -- - - --- __ ^
-- __ ___-- ^ ^` -- __ ___-- ^ ^`;
module.exports = ascii module.exports = ascii;

View File

@ -1,7 +1,8 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize');
const pkg = require('../package.json') const pkg = require('../package.json');
const databaseName = pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '') const databaseName =
pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '');
const createDB = () => { const createDB = () => {
const db = new Sequelize( const db = new Sequelize(
@ -10,16 +11,16 @@ const createDB = () => {
logging: false, logging: false,
operatorsAliases: false operatorsAliases: false
} }
) );
return db return db;
} };
const db = createDB() const db = createDB();
module.exports = db module.exports = db;
// This is a global Mocha hook used for resource cleanup. // This is a global Mocha hook used for resource cleanup.
// Otherwise, Mocha v4+ does not exit after tests. // Otherwise, Mocha v4+ does not exit after tests.
if (process.env.NODE_ENV === 'test') { if (process.env.NODE_ENV === 'test') {
after('close database connection', () => db.close()) after('close database connection', () => db.close());
} }

View File

@ -1,6 +1,6 @@
const db = require('./db') const db = require('./db');
// register models // register models
require('./models') require('./models');
module.exports = db module.exports = db;

View File

@ -1,5 +1,5 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize');
const db = require('../db') const db = require('../db');
const Article = db.define('articles', { const Article = db.define('articles', {
title: { title: {
@ -9,6 +9,6 @@ const Article = db.define('articles', {
link: { link: {
type: Sequelize.STRING type: Sequelize.STRING
} }
}) });
module.exports = Article module.exports = Article;

View File

@ -1,4 +1,4 @@
const Item = require('./item') const Item = require('./item');
const Article = require('./article') const Article = require('./article');
module.exports = { Item, Article } module.exports = { Item, Article };

View File

@ -1,11 +1,11 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize');
const db = require('../db') const db = require('../db');
const Item = db.define('items', { const Item = db.define('items', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
allowNull: false allowNull: false
} }
}) });
module.exports = Item module.exports = Item;

View File

@ -1,28 +1,27 @@
const db = require('../db') const db = require('../db');
const fs = require('fs') const { Article } = require('./models');
const { Item, Article } = require('./models')
const testArticle = { const testArticle = {
title: 'read desert', title: 'read desert',
link: 'https://readdesert.org' link: 'https://readdesert.org'
} };
console.log(Article) console.log(Article);
async function runSeed() { async function runSeed() {
await db.sync({ force: true }) await db.sync({ force: true });
console.log('db synced!') console.log('db synced!');
console.log('seeding...') console.log('seeding...');
try { try {
await Article.create(testArticle) await Article.create(testArticle);
console.log('seeded successfully') console.log('seeded successfully');
} catch (err) { } catch (err) {
console.error(err) console.error(err);
process.exitCode = 1 process.exitCode = 1;
} finally { } finally {
console.log('closing db connection') console.log('closing db connection');
await db.close() await db.close();
console.log('db connection closed') console.log('db connection closed');
} }
} }
runSeed() runSeed();

View File

@ -1,20 +1,20 @@
const fetch = require("node-fetch"); const fetch = require('node-fetch');
// implemented from: https://github.com/HackerNews/API // implemented from: https://github.com/HackerNews/API
const HN_PREFIX = "https://hacker-news.firebaseio.com/v0/"; const HN_PREFIX = 'https://hacker-news.firebaseio.com/v0/';
const TOP_STORIES = "topstories"; const TOP_STORIES = 'topstories';
const ITEM = "item"; const ITEM = 'item';
function hnFetch(type, id = "") { function hnFetch(type, id = '') {
const url = id const url = id
? `${HN_PREFIX}${type}/${id}.json` ? `${HN_PREFIX}${type}/${id}.json`
: `${HN_PREFIX}${type}.json`; : `${HN_PREFIX}${type}.json`;
return fetch(url, { return fetch(url, {
method: "GET", method: 'GET',
headers: { headers: {
"Content-Type": "application/json" 'Content-Type': 'application/json'
} }
}) })
.then(res => { .then(res => {
@ -36,7 +36,12 @@ async function main() {
storyIds.slice(0, 20).map(storyId => hnFetch(ITEM, storyId)) storyIds.slice(0, 20).map(storyId => hnFetch(ITEM, storyId))
); );
console.log(stories.map(story => { delete story.kids; return story; })); console.log(
stories.map(story => {
delete story.kids;
return story;
})
);
} }
main(); main();

View File

@ -1,31 +1,41 @@
const express = require('express') const express = require('express');
const path = require('path') const path = require('path');
const app = express() const morgan = require('morgan');
const morgan = require('morgan') const ascii = require('./ascii');
const ascii = require('./ascii') const db = require('./db');
const port = process.env.PORT || 1337
app.use(morgan('tiny')) const app = express();
const port = process.env.PORT || 1337;
db.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
app.use(morgan('tiny'));
// body parsing middleware // body parsing middleware
app.use(express.json()) app.use(express.json());
app.use(express.urlencoded({ extended: true })) app.use(express.urlencoded({ extended: true }));
app.use(require('body-parser').text()) app.use(require('body-parser').text());
app.use('/api', require('./api')) app.use('/api', require('./api'));
app.get('*', (req, res) => app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, 'public', 'articles.html')) res.sendFile(path.resolve(__dirname, 'public', 'articles.html'))
) );
// error handling endware // error handling endware
app.use((err, req, res, next) => { app.use((err, req, res, next) => {
console.error(err) console.error(err);
console.error(err.stack) console.error(err.stack);
res.status(err.status || 500).send(err.message || 'Internal server error.') res.status(err.status || 500).send(err.message || 'Internal server error.');
next() next();
}) });
app.listen(port, () => { app.listen(port, () => {
console.log('\n' + ascii + '\n') console.log(`\n${ascii}\n`);
console.log(`Doin' haxor stuff on port ${port}`) console.log(`Doin' haxor stuff on port ${port}`);
}) });

3083
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,15 +18,39 @@
"seed": "node db/seed.js", "seed": "node db/seed.js",
"start": "nodemon index" "start": "nodemon index"
}, },
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
"dependencies": { "dependencies": {
"node-fetch": "^2.3.0",
"axios": "^0.18.0", "axios": "^0.18.0",
"body-parser": "^1.18.3",
"concurrently": "^4.0.1", "concurrently": "^4.0.1",
"express": "^4.16.4", "express": "^4.16.4",
"http-proxy-middleware": "^0.19.0", "http-proxy-middleware": "^0.19.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"node-fetch": "^2.3.0",
"nodemon": "^1.18.9", "nodemon": "^1.18.9",
"pg": "^7.5.0", "pg": "^7.5.0",
"sequelize": "^4.39.1" "sequelize": "^4.39.1"
},
"devDependencies": {
"eslint": "^5.3.0",
"eslint-config-prettier": "^4.0.0",
"eslint-config-recommended": "^4.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.12.4",
"husky": "^1.3.1",
"lint-staged": "^8.1.3",
"prettier": "^1.16.4"
} }
} }