diff --git a/.eslintrc.json b/.eslintrc.json index cd57d612..7df192a5 100644 --- a/.eslintrc.json +++ b/.eslintrc.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 } diff --git a/.prettierrc.yaml b/.prettierrc.yaml new file mode 100644 index 00000000..8e1ecf74 --- /dev/null +++ b/.prettierrc.yaml @@ -0,0 +1,3 @@ +# use the prettier defaults except for: +trailingComma: "es5" +singleQuote: true diff --git a/middleware/load-options.js b/middleware/load-options.js index 505c70ac..08dfe04c 100644 --- a/middleware/load-options.js +++ b/middleware/load-options.js @@ -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; } diff --git a/middleware/load-user.js b/middleware/load-user.js index 4ea1ec9d..e9ffbaea 100644 --- a/middleware/load-user.js +++ b/middleware/load-user.js @@ -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; } } diff --git a/package.json b/package.json index de80a162..974ddc0b 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/routes/get.js b/routes/get.js index 70bacef4..f30b5269 100644 --- a/routes/get.js +++ b/routes/get.js @@ -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 = `${name}`; @@ -18,7 +21,7 @@ function getCopyrightHTML(user, plain) { if (user.email) { html += ` <${ plain ? user.email : escapeTags(user.email) - }>`; + }>`; } 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(', '); } } diff --git a/routes/post.js b/routes/post.js index 16d03f7c..852ecb46 100644 --- a/routes/post.js +++ b/routes/post.js @@ -1,8 +1,6 @@ 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'); @@ -10,14 +8,13 @@ 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` + ); } }; diff --git a/server.js b/server.js index 595a22fb..f8acdaae 100644 --- a/server.js +++ b/server.js @@ -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()); diff --git a/users/4232praveenpal.json b/users/4232praveenpal.json index 821a0b4b..3ea8818c 100644 --- a/users/4232praveenpal.json +++ b/users/4232praveenpal.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" } diff --git a/users/praveenpal4232.json b/users/praveenpal4232.json index 821a0b4b..3ea8818c 100644 --- a/users/praveenpal4232.json +++ b/users/praveenpal4232.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" } diff --git a/users/twitch.json b/users/twitch.json index a0144415..ce4ee259 100644 --- a/users/twitch.json +++ b/users/twitch.json @@ -3,4 +3,4 @@ "url": "https://remysharp.com", "email": "remy@remysharp.com", "gravatar": true -} \ No newline at end of file +}