mirror of
https://github.com/samsonjs/mit-license.git
synced 2026-04-27 15:07:42 +00:00
chore: add linting as part and parcel
This commit is contained in:
parent
785da61044
commit
b8abdf515e
11 changed files with 82 additions and 56 deletions
|
|
@ -4,14 +4,21 @@
|
||||||
"es6": true,
|
"es6": true,
|
||||||
"node": true
|
"node": true
|
||||||
},
|
},
|
||||||
"plugins": ["node"],
|
"plugins": [
|
||||||
"extends": ["eslint:recommended", "plugin:node/recommended"],
|
"node"
|
||||||
|
],
|
||||||
|
"extends": [
|
||||||
|
"plugin:prettier/recommended",
|
||||||
|
"eslint:recommended",
|
||||||
|
"plugin:node/recommended"
|
||||||
|
],
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"ecmaVersion": 2018,
|
"ecmaVersion": 2018,
|
||||||
"sourceType": "module",
|
"sourceType": "module",
|
||||||
"warnOnUnsupportedTypeScriptVersion": false
|
"warnOnUnsupportedTypeScriptVersion": false
|
||||||
},
|
},
|
||||||
"rules": {
|
"rules": {
|
||||||
|
"prettier/prettier": "error",
|
||||||
"node/no-deprecated-api": 0,
|
"node/no-deprecated-api": 0,
|
||||||
"no-console": 0
|
"no-console": 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
3
.prettierrc.yaml
Normal file
3
.prettierrc.yaml
Normal file
|
|
@ -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('+')) {
|
if (curr.startsWith('+')) {
|
||||||
acc.license = curr
|
acc.license = curr.substr(1).toUpperCase();
|
||||||
.substr(1)
|
|
||||||
.toUpperCase();
|
|
||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,14 @@ module.exports = async (req, res, next) => {
|
||||||
path.join(__dirname, '..', 'users', `${id}.json`),
|
path.join(__dirname, '..', 'users', `${id}.json`),
|
||||||
'utf8'
|
'utf8'
|
||||||
);
|
);
|
||||||
res.locals.user = {...res.locals.user, ...JSON.parse(data)};
|
res.locals.user = { ...res.locals.user, ...JSON.parse(data) };
|
||||||
} catch ({code, message}) {
|
} catch ({ code, message }) {
|
||||||
if (code !== 'ENOENT') {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
package.json
13
package.json
|
|
@ -12,6 +12,12 @@
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.x.x"
|
"node": ">=10.x.x"
|
||||||
},
|
},
|
||||||
|
"husky": {
|
||||||
|
"hooks": {
|
||||||
|
"pre-commit": "npm run lint",
|
||||||
|
"pre-push": "npm test"
|
||||||
|
}
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node .",
|
"start": "node .",
|
||||||
"dev": "nodemon .",
|
"dev": "nodemon .",
|
||||||
|
|
@ -30,7 +36,6 @@
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"express-minify": "^1.0.0",
|
"express-minify": "^1.0.0",
|
||||||
"md5": "^2.2.1",
|
"md5": "^2.2.1",
|
||||||
"node-html-parser": "^1.1.15",
|
|
||||||
"postcss-middleware": "^1.1.4",
|
"postcss-middleware": "^1.1.4",
|
||||||
"postcss-preset-env": "^6.7.0",
|
"postcss-preset-env": "^6.7.0",
|
||||||
"serve-favicon": "^2.5.0"
|
"serve-favicon": "^2.5.0"
|
||||||
|
|
@ -45,9 +50,13 @@
|
||||||
"@types/node": "^12.6.8",
|
"@types/node": "^12.6.8",
|
||||||
"css": "^2.2.4",
|
"css": "^2.2.4",
|
||||||
"eslint": "^6.1.0",
|
"eslint": "^6.1.0",
|
||||||
|
"eslint-config-prettier": "^6.0.0",
|
||||||
"eslint-plugin-node": "^9.1.0",
|
"eslint-plugin-node": "^9.1.0",
|
||||||
|
"eslint-plugin-prettier": "^3.1.0",
|
||||||
"has-flag": "^4.0.0",
|
"has-flag": "^4.0.0",
|
||||||
"nodemon": "^1.19.1"
|
"husky": "^3.0.2",
|
||||||
|
"nodemon": "^1.19.1",
|
||||||
|
"prettier": "^1.18.2"
|
||||||
},
|
},
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
"postcss-middleware/vinyl-fs/glob-stream/micromatch/braces": "^3.0.2"
|
"postcss-middleware/vinyl-fs/glob-stream/micromatch/braces": "^3.0.2"
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,12 @@ const { stripTags, escapeTags } = require('./utils');
|
||||||
function getCopyrightHTML(user, plain) {
|
function getCopyrightHTML(user, plain) {
|
||||||
let html = '';
|
let html = '';
|
||||||
|
|
||||||
const name = typeof user === "string" ? user
|
const name =
|
||||||
: plain ? user.name || user.copyright
|
typeof user === 'string'
|
||||||
: escapeTags(user.name || user.copyright);
|
? user
|
||||||
|
: plain
|
||||||
|
? user.name || user.copyright
|
||||||
|
: escapeTags(user.name || user.copyright);
|
||||||
|
|
||||||
if (user.url) {
|
if (user.url) {
|
||||||
html = `<a href="${stripTags(user.url)}">${name}</a>`;
|
html = `<a href="${stripTags(user.url)}">${name}</a>`;
|
||||||
|
|
@ -18,7 +21,7 @@ function getCopyrightHTML(user, plain) {
|
||||||
if (user.email) {
|
if (user.email) {
|
||||||
html += ` <<a href="mailto:${stripTags(user.email)}">${
|
html += ` <<a href="mailto:${stripTags(user.email)}">${
|
||||||
plain ? user.email : escapeTags(user.email)
|
plain ? user.email : escapeTags(user.email)
|
||||||
}</a>>`;
|
}</a>>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return html;
|
return html;
|
||||||
|
|
@ -39,9 +42,7 @@ module.exports = (req, res) => {
|
||||||
.map(_ => (options.format !== 'html' ? _ : escapeTags(_)))
|
.map(_ => (options.format !== 'html' ? _ : escapeTags(_)))
|
||||||
.join(', ');
|
.join(', ');
|
||||||
} else {
|
} else {
|
||||||
name = user.copyright
|
name = user.copyright.map(getCopyrightHTML).join(', ');
|
||||||
.map(getCopyrightHTML)
|
|
||||||
.join(', ');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {
|
const { promisify } = require('util');
|
||||||
promisify
|
|
||||||
} = require('util');
|
|
||||||
const access = promisify(fs.access);
|
const access = promisify(fs.access);
|
||||||
const writeFile = promisify(fs.writeFile);
|
const writeFile = promisify(fs.writeFile);
|
||||||
const btoa = require('btoa');
|
const btoa = require('btoa');
|
||||||
|
|
@ -10,14 +8,13 @@ var github = require('@octokit/rest')({
|
||||||
// GitHub personal access token
|
// GitHub personal access token
|
||||||
auth: process.env.github_token,
|
auth: process.env.github_token,
|
||||||
// User agent with version from package.json
|
// 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');
|
const { validDomainId } = require('./utils');
|
||||||
|
|
||||||
function getUserData({
|
function getUserData({ query, body }) {
|
||||||
query,
|
|
||||||
body
|
|
||||||
}) {
|
|
||||||
// If query parameters provided
|
// If query parameters provided
|
||||||
if (Object.keys(query).length > 0) return query;
|
if (Object.keys(query).length > 0) return query;
|
||||||
// If the data parsed as {'{data: "value"}': ''}
|
// If the data parsed as {'{data: "value"}': ''}
|
||||||
|
|
@ -29,9 +26,7 @@ function getUserData({
|
||||||
|
|
||||||
// HTTP POST API
|
// HTTP POST API
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const {
|
const { hostname } = req;
|
||||||
hostname
|
|
||||||
} = req;
|
|
||||||
// Get different parts of hostname (example: remy.mit-license.org -> ['remy', 'mit-license', 'org'])
|
// Get different parts of hostname (example: remy.mit-license.org -> ['remy', 'mit-license', 'org'])
|
||||||
const params = hostname.split('.');
|
const params = hostname.split('.');
|
||||||
|
|
||||||
|
|
@ -62,14 +57,18 @@ module.exports = async (req, res) => {
|
||||||
// Check if the user file exists in the users directory
|
// Check if the user file exists in the users directory
|
||||||
await access(path.join(__dirname, '..', 'users', `${id}.json`));
|
await access(path.join(__dirname, '..', 'users', `${id}.json`));
|
||||||
res
|
res
|
||||||
.status(409)
|
.status(409)
|
||||||
.send(
|
.send(
|
||||||
'User already exists - to update values, please send a pull request on https://github.com/remy/mit-license'
|
'User already exists - to update values, please send a pull request on https://github.com/remy/mit-license'
|
||||||
)
|
);
|
||||||
return;
|
return;
|
||||||
} catch ({code, message}) {
|
} catch ({ code, message }) {
|
||||||
if (code !== "ENOENT") {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -82,7 +81,7 @@ module.exports = async (req, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const fileContent = JSON.stringify(userData, 0, 2)
|
const fileContent = JSON.stringify(userData, 0, 2);
|
||||||
|
|
||||||
await github.repos.createFile({
|
await github.repos.createFile({
|
||||||
owner: 'remy',
|
owner: 'remy',
|
||||||
|
|
@ -94,15 +93,16 @@ module.exports = async (req, res) => {
|
||||||
name: 'MIT License Bot',
|
name: 'MIT License Bot',
|
||||||
email: 'remy@leftlogic.com',
|
email: 'remy@leftlogic.com',
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
writeFile(
|
writeFile(path.join(__dirname, '..', 'users', `${id}.json`), fileContent);
|
||||||
path.join(__dirname, "..", "users", `${id}.json`),
|
|
||||||
fileContent
|
|
||||||
);
|
|
||||||
|
|
||||||
res.status(201).send(`MIT license page created: https://${hostname}`);
|
res.status(201).send(`MIT license page created: https://${hostname}`);
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
res.status(500).send(`Unable to create new user - please send a pull request on https://github.com/remy/mit-license`)
|
res
|
||||||
|
.status(500)
|
||||||
|
.send(
|
||||||
|
`Unable to create new user - please send a pull request on https://github.com/remy/mit-license`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
16
server.js
16
server.js
|
|
@ -17,9 +17,11 @@ var PORT = process.env.PORT || 8080;
|
||||||
|
|
||||||
// Prepare application
|
// Prepare application
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(minify({
|
app.use(
|
||||||
cache: tmpdir
|
minify({
|
||||||
}));
|
cache: tmpdir,
|
||||||
|
})
|
||||||
|
);
|
||||||
app.use(favicon(path.join(__dirname, 'favicon.ico')));
|
app.use(favicon(path.join(__dirname, 'favicon.ico')));
|
||||||
app.set('views', path.join(__dirname, '/licenses'));
|
app.set('views', path.join(__dirname, '/licenses'));
|
||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs');
|
||||||
|
|
@ -48,9 +50,11 @@ app.use(
|
||||||
// CORS
|
// CORS
|
||||||
app.use(require('./middleware/cors'));
|
app.use(require('./middleware/cors'));
|
||||||
// Parse URL-encoded bodies (as sent by HTML forms)
|
// Parse URL-encoded bodies (as sent by HTML forms)
|
||||||
app.use(express.urlencoded({
|
app.use(
|
||||||
extended: true
|
express.urlencoded({
|
||||||
}));
|
extended: true,
|
||||||
|
})
|
||||||
|
);
|
||||||
// Parse JSON bodies (as sent by API clients)
|
// Parse JSON bodies (as sent by API clients)
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"copyright": "Praveen Pal",
|
"copyright": "Praveen Pal",
|
||||||
"url": "https://github.com/praveenpal4232",
|
"url": "https://github.com/praveenpal4232",
|
||||||
"email": "praveenpal4232@gmail.com"
|
"email": "praveenpal4232@gmail.com"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"copyright": "Praveen Pal",
|
"copyright": "Praveen Pal",
|
||||||
"url": "https://github.com/praveenpal4232",
|
"url": "https://github.com/praveenpal4232",
|
||||||
"email": "praveenpal4232@gmail.com"
|
"email": "praveenpal4232@gmail.com"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,4 @@
|
||||||
"url": "https://remysharp.com",
|
"url": "https://remysharp.com",
|
||||||
"email": "remy@remysharp.com",
|
"email": "remy@remysharp.com",
|
||||||
"gravatar": true
|
"gravatar": true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue