refactor: Update deps, proper temp-dir retrieval, simplify filesystem actions with fs-extra and fix user files

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
This commit is contained in:
Richie Bendall 2020-01-03 16:12:18 +13:00
parent 43919a9774
commit bd3ecf4917
No known key found for this signature in database
GPG key ID: 1C6A99DFA9D306FC
14 changed files with 45 additions and 71 deletions

View file

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html id="home" lang="en"> <html id="home" lang="en">
<% include components/header %> <%- include('components/header') %>
<body> <body>
<article> <article>
<%- gravatar %> <%- gravatar %>
@ -20,7 +20,7 @@ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.</p> OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.</p>
</article> </article>
<% include components/footer %> <%- include('components/footer') %>
</body> </body>
</html> </html>

View file

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html id="home" lang="en"> <html id="home" lang="en">
<% include components/header %> <%- include('components/header') %>
<body> <body>
<article> <article>
<%- gravatar %> <%- gravatar %>
@ -26,7 +26,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.</p> THE SOFTWARE.</p>
</article> </article>
<% include components/footer %> <%- include('components/footer') %>
</body> </body>
</html> </html>

View file

@ -1,4 +1,4 @@
const thisYear = new Date().getFullYear(); const currentYear = new Date().getFullYear();
module.exports = (req, res, next) => { module.exports = (req, res, next) => {
const parts = req.url.split('/'); const parts = req.url.split('/');
@ -47,7 +47,7 @@ module.exports = (req, res, next) => {
{ {
format: 'html', format: 'html',
startYear: null, startYear: null,
endYear: thisYear, endYear: currentYear,
sha: null, sha: null,
} }
); );

View file

@ -1,5 +1,4 @@
const { promisify } = require('util'); const fs = require('fs-extra');
const readFile = promisify(require('fs').readFile);
const path = require('path'); const path = require('path');
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
@ -16,7 +15,7 @@ module.exports = async (req, res, next) => {
}; };
try { try {
const data = await readFile( const data = await fs.readFile(
path.join(__dirname, '..', 'users', `${id}.json`), path.join(__dirname, '..', 'users', `${id}.json`),
'utf8' 'utf8'
); );

View file

@ -10,7 +10,7 @@
"url": "git@github.com:remy/mit-license.git" "url": "git@github.com:remy/mit-license.git"
}, },
"engines": { "engines": {
"node": ">=10.x.x" "node": ">=10"
}, },
"husky": { "husky": {
"hooks": { "hooks": {
@ -30,34 +30,28 @@
}, },
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@octokit/rest": "^16.34.1", "@octokit/rest": "^16.36.0",
"btoa": "^1.2.1", "btoa": "^1.2.1",
"ejs": "^2.7.1", "ejs": "^3.0.1",
"express": "^4.17.1", "express": "^4.17.1",
"express-minify": "^1.0.0", "express-minify": "^1.0.0",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"md5": "^2.2.1", "md5": "^2.2.1",
"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",
"temp-dir": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"@remy/eslint": "^3.2.2", "@remy/eslint": "^3.2.2",
"@types/btoa": "^1.2.3",
"@types/css": "^0.0.31",
"@types/ejs": "^2.6.3",
"@types/express": "^4.17.2",
"@types/express-minify": "^0.1.34",
"@types/lodash": "^4.14.144",
"@types/md5": "^2.1.33",
"@types/node": "^12.12.6",
"babel-eslint": "^10.0.3", "babel-eslint": "^10.0.3",
"css": "^2.2.4", "css": "^2.2.4",
"eslint": "^6.6.0", "eslint": "^6.8.0",
"eslint-plugin-node": "^10.0.0", "eslint-plugin-node": "^11.0.0",
"has-flag": "^4.0.0", "has-flag": "^4.0.0",
"husky": "^3.0.9", "husky": "^3.1.0",
"nodemon": "^1.19.4" "nodemon": "^2.0.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"

View file

@ -1,8 +1,5 @@
const fs = require('fs'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const { promisify } = require('util');
const access = promisify(fs.access);
const writeFile = promisify(fs.writeFile);
const btoa = require('btoa'); const btoa = require('btoa');
const { version } = require(path.join(__dirname, '..', 'package.json')); const { version } = require(path.join(__dirname, '..', 'package.json'));
const _ = require('lodash'); const _ = require('lodash');
@ -18,8 +15,7 @@ function getUserData({ query, body }) {
// If query parameters provided // If query parameters provided
if (_.size(query) > 0) return query; if (_.size(query) > 0) return query;
// If the data parsed as {'{data: "value"}': ''} // If the data parsed as {'{data: "value"}': ''}
if (_.size(body) === 1 && !_.first(_.values(body))) if (_.size(body) === 1 && !_.first(_.values(body))) return JSON.parse(_.first(_.keys(body)));
return JSON.parse(_.first(_.keys(body)));
// Fallback // Fallback
return body; return body;
} }
@ -53,24 +49,15 @@ module.exports = async (req, res) => {
return; return;
} }
try {
// 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`)); const exists = await fs.pathExists(path.join(__dirname, '..', 'users', `${id}.json`));
if (exists) {
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 }) {
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;
}
} }
// File doesn't exist // File doesn't exist
@ -95,7 +82,7 @@ module.exports = async (req, res) => {
}, },
}); });
writeFile(path.join(__dirname, '..', 'users', `${id}.json`), fileContent); await fs.writeFile(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) {

View file

@ -9,7 +9,7 @@ const express = require('express');
const minify = require('express-minify'); const minify = require('express-minify');
const favicon = require('serve-favicon'); const favicon = require('serve-favicon');
const postcssMiddleware = require('postcss-middleware'); const postcssMiddleware = require('postcss-middleware');
const tmpdir = require('os').tmpdir(); const tmpdir = require('temp-dir');
const path = require('path'); const path = require('path');
// Server // Server

20
test.js
View file

@ -1,23 +1,18 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs-extra');
const CSS = require('css'); const CSS = require('css');
const { validDomainId } = require('./routes/utils'); const { validDomainId } = require('./routes/utils');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
const readdir = promisify(fs.readdir);
const hasFlag = require('has-flag'); const hasFlag = require('has-flag');
let errored = false;
function report(content, fix) { function report(content, fix) {
errored = true;
console.error(content); console.error(content);
if (fix && hasFlag('--fix')) fix(); if (fix && hasFlag('--fix')) fix();
process.exitCode = 1;
} }
(async () => { (async () => {
const users = await readdir('users'); const users = await fs.readdir('users');
await users.forEach(async user => { users.forEach(async user => {
if (!user.endsWith('json')) { if (!user.endsWith('json')) {
report(`${user} is not a json file`, () => report(`${user} is not a json file`, () =>
fs.unlink(path.join('users', user), () => { }) fs.unlink(path.join('users', user), () => { })
@ -27,7 +22,7 @@ function report(content, fix) {
report(`${user} is not a valid domain id.`); report(`${user} is not a valid domain id.`);
} }
try { try {
const data = await readFile(path.join('users', user), 'utf8'); const data = await fs.readFile(path.join('users', user), 'utf8');
try { try {
const u = JSON.parse(data); const u = JSON.parse(data);
if (!u.locked && !u.copyright) { if (!u.locked && !u.copyright) {
@ -55,11 +50,11 @@ function report(content, fix) {
} }
}); });
const themes = await readdir('themes'); const themes = await fs.readdir('themes');
await themes.forEach(async theme => { await themes.forEach(async theme => {
if (theme.endsWith('css')) { if (theme.endsWith('css')) {
try { try {
const data = await readFile(path.join('themes', theme), 'utf8'); const data = await fs.readFile(path.join('themes', theme), 'utf8');
try { try {
CSS.parse(data); CSS.parse(data);
} catch ({ message }) { } catch ({ message }) {
@ -70,5 +65,4 @@ function report(content, fix) {
} }
} }
}); });
if (errored) process.exit(1);
})(); })();

View file

@ -5,5 +5,5 @@
"format": "html", "format": "html",
"license": "mit", "license": "mit",
"theme": "default", "theme": "default",
"gravatar": "true" "gravatar": true
} }

View file

@ -5,5 +5,5 @@
"format": "html", "format": "html",
"license": "isc", "license": "isc",
"theme": "eula-modern", "theme": "eula-modern",
"gravatar": "true" "gravatar": true
} }

View file

@ -5,5 +5,5 @@
"format": "html", "format": "html",
"license": "mit", "license": "mit",
"theme": "friendly", "theme": "friendly",
"gravatar": "true" "gravatar": true
} }

View file

@ -5,5 +5,5 @@
"format": "html", "format": "html",
"license": "mit", "license": "mit",
"theme": "default", "theme": "default",
"gravatar": "true" "gravatar": true
} }

View file

@ -5,5 +5,5 @@
"format": "html", "format": "html",
"license": "mit", "license": "mit",
"theme": "default", "theme": "default",
"gravatar": "false" "gravatar": false
} }

BIN
yarn.lock

Binary file not shown.