Skip to content
Permalink
Browse files

chore: add linting as part and parcel

  • Loading branch information...
remy committed Jul 30, 2019
1 parent 785da61 commit b8abdf515eb143647ca8f7c472eb77b04682dddc
Showing with 82 additions and 56 deletions.
  1. +9 −2 .eslintrc.json
  2. +3 −0 .prettierrc.yaml
  3. +1 −3 middleware/load-options.js
  4. +7 −3 middleware/load-user.js
  5. +11 −2 package.json
  6. +8 −7 routes/get.js
  7. +26 −26 routes/post.js
  8. +10 −6 server.js
  9. +3 −3 users/4232praveenpal.json
  10. +3 −3 users/praveenpal4232.json
  11. +1 −1 users/twitch.json
@@ -4,14 +4,21 @@
"es6": true,
"node": true
},
"plugins": ["node"],
"extends": ["eslint:recommended", "plugin:node/recommended"],
"plugins": [
"node"
],
"extends": [
"plugin:prettier/recommended",
"eslint:recommended",
"plugin:node/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"warnOnUnsupportedTypeScriptVersion": false
},
"rules": {
"prettier/prettier": "error",
"node/no-deprecated-api": 0,
"no-console": 0
}
@@ -0,0 +1,3 @@
# use the prettier defaults except for:
trailingComma: "es5"
singleQuote: true
@@ -37,9 +37,7 @@ module.exports = (req, res, next) => {
}

if (curr.startsWith('+')) {
acc.license = curr
.substr(1)
.toUpperCase();
acc.license = curr.substr(1).toUpperCase();
return acc;
}

@@ -20,10 +20,14 @@ module.exports = async (req, res, next) => {
path.join(__dirname, '..', 'users', `${id}.json`),
'utf8'
);
res.locals.user = {...res.locals.user, ...JSON.parse(data)};
} catch ({code, message}) {
res.locals.user = { ...res.locals.user, ...JSON.parse(data) };
} catch ({ code, message }) {
if (code !== 'ENOENT') {
res.code(500).send(`An internal error occurred - open an issue on https://github.com/remy/mit-license with the following information: ${message}`)
res
.code(500)
.send(
`An internal error occurred - open an issue on https://github.com/remy/mit-license with the following information: ${message}`
);
return;
}
}
@@ -12,6 +12,12 @@
"engines": {
"node": ">=10.x.x"
},
"husky": {
"hooks": {
"pre-commit": "npm run lint",
"pre-push": "npm test"
}
},
"scripts": {
"start": "node .",
"dev": "nodemon .",
@@ -30,7 +36,6 @@
"express": "^4.17.1",
"express-minify": "^1.0.0",
"md5": "^2.2.1",
"node-html-parser": "^1.1.15",
"postcss-middleware": "^1.1.4",
"postcss-preset-env": "^6.7.0",
"serve-favicon": "^2.5.0"
@@ -45,9 +50,13 @@
"@types/node": "^12.6.8",
"css": "^2.2.4",
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-prettier": "^3.1.0",
"has-flag": "^4.0.0",
"nodemon": "^1.19.1"
"husky": "^3.0.2",
"nodemon": "^1.19.1",
"prettier": "^1.18.2"
},
"resolutions": {
"postcss-middleware/vinyl-fs/glob-stream/micromatch/braces": "^3.0.2"
@@ -5,9 +5,12 @@ const { stripTags, escapeTags } = require('./utils');
function getCopyrightHTML(user, plain) {
let html = '';

const name = typeof user === "string" ? user
: plain ? user.name || user.copyright
: escapeTags(user.name || user.copyright);
const name =
typeof user === 'string'
? user
: plain
? user.name || user.copyright
: escapeTags(user.name || user.copyright);

if (user.url) {
html = `<a href="${stripTags(user.url)}">${name}</a>`;
@@ -18,7 +21,7 @@ function getCopyrightHTML(user, plain) {
if (user.email) {
html += ` &lt;<a href="mailto:${stripTags(user.email)}">${
plain ? user.email : escapeTags(user.email)
}</a>&gt;`;
}</a>&gt;`;
}

return html;
@@ -39,9 +42,7 @@ module.exports = (req, res) => {
.map(_ => (options.format !== 'html' ? _ : escapeTags(_)))
.join(', ');
} else {
name = user.copyright
.map(getCopyrightHTML)
.join(', ');
name = user.copyright.map(getCopyrightHTML).join(', ');
}
}

@@ -1,23 +1,20 @@
const fs = require('fs');
const path = require('path');
const {
promisify
} = require('util');
const { promisify } = require('util');
const access = promisify(fs.access);
const writeFile = promisify(fs.writeFile);
const btoa = require('btoa');
var github = require('@octokit/rest')({
// GitHub personal access token
auth: process.env.github_token,
// User agent with version from package.json
userAgent: 'mit-license v' + require(path.join(__dirname, "..", "package.json")).version,
userAgent:
'mit-license v' +
require(path.join(__dirname, '..', 'package.json')).version,
});
const { validDomainId } = require('./utils');

function getUserData({
query,
body
}) {
function getUserData({ query, body }) {
// If query parameters provided
if (Object.keys(query).length > 0) return query;
// If the data parsed as {'{data: "value"}': ''}
@@ -29,9 +26,7 @@ function getUserData({

// HTTP POST API
module.exports = async (req, res) => {
const {
hostname
} = req;
const { hostname } = req;
// Get different parts of hostname (example: remy.mit-license.org -> ['remy', 'mit-license', 'org'])
const params = hostname.split('.');

@@ -62,14 +57,18 @@ module.exports = async (req, res) => {
// Check if the user file exists in the users directory
await access(path.join(__dirname, '..', 'users', `${id}.json`));
res
.status(409)
.send(
'User already exists - to update values, please send a pull request on https://github.com/remy/mit-license'
)
.status(409)
.send(
'User already exists - to update values, please send a pull request on https://github.com/remy/mit-license'
);
return;
} catch ({code, message}) {
if (code !== "ENOENT") {
res.code(500).send(`An internal error occurred - open an issue on https://github.com/remy/mit-license with the following information: ${message}`)
} catch ({ code, message }) {
if (code !== 'ENOENT') {
res
.code(500)
.send(
`An internal error occurred - open an issue on https://github.com/remy/mit-license with the following information: ${message}`
);
return;
}
}
@@ -82,7 +81,7 @@ module.exports = async (req, res) => {
}

try {
const fileContent = JSON.stringify(userData, 0, 2)
const fileContent = JSON.stringify(userData, 0, 2);

await github.repos.createFile({
owner: 'remy',
@@ -94,15 +93,16 @@ module.exports = async (req, res) => {
name: 'MIT License Bot',
email: 'remy@leftlogic.com',
},
})
});

writeFile(
path.join(__dirname, "..", "users", `${id}.json`),
fileContent
);
writeFile(path.join(__dirname, '..', 'users', `${id}.json`), fileContent);

res.status(201).send(`MIT license page created: https://${hostname}`);
} catch(err) {
res.status(500).send(`Unable to create new user - please send a pull request on https://github.com/remy/mit-license`)
} catch (err) {
res
.status(500)
.send(
`Unable to create new user - please send a pull request on https://github.com/remy/mit-license`
);
}
};
@@ -17,9 +17,11 @@ var PORT = process.env.PORT || 8080;

// Prepare application
const app = express();
app.use(minify({
cache: tmpdir
}));
app.use(
minify({
cache: tmpdir,
})
);
app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.set('views', path.join(__dirname, '/licenses'));
app.set('view engine', 'ejs');
@@ -48,9 +50,11 @@ app.use(
// CORS
app.use(require('./middleware/cors'));
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded({
extended: true
}));
app.use(
express.urlencoded({
extended: true,
})
);
// Parse JSON bodies (as sent by API clients)
app.use(express.json());

@@ -1,5 +1,5 @@
{
"copyright": "Praveen Pal",
"url": "https://github.com/praveenpal4232",
"email": "praveenpal4232@gmail.com"
"copyright": "Praveen Pal",
"url": "https://github.com/praveenpal4232",
"email": "praveenpal4232@gmail.com"
}
@@ -1,5 +1,5 @@
{
"copyright": "Praveen Pal",
"url": "https://github.com/praveenpal4232",
"email": "praveenpal4232@gmail.com"
"copyright": "Praveen Pal",
"url": "https://github.com/praveenpal4232",
"email": "praveenpal4232@gmail.com"
}
@@ -3,4 +3,4 @@
"url": "https://remysharp.com",
"email": "remy@remysharp.com",
"gravatar": true
}
}

0 comments on commit b8abdf5

Please sign in to comment.
You can’t perform that action at this time.