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>
<html id="home" lang="en">
<% include components/header %>
<%- include('components/header') %>
<body>
<article>
<%- 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
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.</p>
</article>
<% include components/footer %>
<%- include('components/footer') %>
</body>
</html>

View file

@ -1,6 +1,6 @@
<!DOCTYPE html>
<html id="home" lang="en">
<% include components/header %>
<%- include('components/header') %>
<body>
<article>
<%- 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
THE SOFTWARE.</p>
</article>
<% include components/footer %>
<%- include('components/footer') %>
</body>
</html>

View file

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

View file

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

View file

@ -10,7 +10,7 @@
"url": "git@github.com:remy/mit-license.git"
},
"engines": {
"node": ">=10.x.x"
"node": ">=10"
},
"husky": {
"hooks": {
@ -30,34 +30,28 @@
},
"license": "MIT",
"dependencies": {
"@octokit/rest": "^16.34.1",
"@octokit/rest": "^16.36.0",
"btoa": "^1.2.1",
"ejs": "^2.7.1",
"ejs": "^3.0.1",
"express": "^4.17.1",
"express-minify": "^1.0.0",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15",
"md5": "^2.2.1",
"postcss-middleware": "^1.1.4",
"postcss-preset-env": "^6.7.0",
"serve-favicon": "^2.5.0"
"serve-favicon": "^2.5.0",
"temp-dir": "^2.0.0"
},
"devDependencies": {
"@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",
"css": "^2.2.4",
"eslint": "^6.6.0",
"eslint-plugin-node": "^10.0.0",
"eslint": "^6.8.0",
"eslint-plugin-node": "^11.0.0",
"has-flag": "^4.0.0",
"husky": "^3.0.9",
"nodemon": "^1.19.4"
"husky": "^3.1.0",
"nodemon": "^2.0.2"
},
"resolutions": {
"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 { promisify } = require('util');
const access = promisify(fs.access);
const writeFile = promisify(fs.writeFile);
const btoa = require('btoa');
const { version } = require(path.join(__dirname, '..', 'package.json'));
const _ = require('lodash');
@ -18,8 +15,7 @@ function getUserData({ query, body }) {
// If query parameters provided
if (_.size(query) > 0) return query;
// If the data parsed as {'{data: "value"}': ''}
if (_.size(body) === 1 && !_.first(_.values(body)))
return JSON.parse(_.first(_.keys(body)));
if (_.size(body) === 1 && !_.first(_.values(body))) return JSON.parse(_.first(_.keys(body)));
// Fallback
return body;
}
@ -53,24 +49,15 @@ module.exports = async (req, res) => {
return;
}
try {
// Check if the user file exists in the users directory
await access(path.join(__dirname, '..', 'users', `${id}.json`));
// Check if the user file exists in the users directory
const exists = await fs.pathExists(path.join(__dirname, '..', 'users', `${id}.json`));
if (exists) {
res
.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}`
);
return;
}
}
// 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}`);
} catch (err) {

View file

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

26
test.js
View file

@ -1,33 +1,28 @@
const path = require('path');
const fs = require('fs');
const fs = require('fs-extra');
const CSS = require('css');
const { validDomainId } = require('./routes/utils');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
const readdir = promisify(fs.readdir);
const hasFlag = require('has-flag');
let errored = false;
function report(content, fix) {
errored = true;
console.error(content);
if (fix && hasFlag('--fix')) fix();
process.exitCode = 1;
}
(async () => {
const users = await readdir('users');
await users.forEach(async user => {
const users = await fs.readdir('users');
users.forEach(async user => {
if (!user.endsWith('json')) {
report(`${user} is not a json file`, () =>
fs.unlink(path.join('users', user), () => {})
fs.unlink(path.join('users', user), () => { })
);
}
if (!validDomainId(user.replace('.json', ''))) {
report(`${user} is not a valid domain id.`);
}
try {
const data = await readFile(path.join('users', user), 'utf8');
const data = await fs.readFile(path.join('users', user), 'utf8');
try {
const u = JSON.parse(data);
if (!u.locked && !u.copyright) {
@ -37,14 +32,14 @@ function report(content, fix) {
report(`Version tag found in ${user}`, () => {
delete u.version;
const stringified = `${JSON.stringify(u, 0, 2)}\n`;
fs.writeFile(path.join('users', user), stringified, () => {});
fs.writeFile(path.join('users', user), stringified, () => { });
});
}
if (typeof u.gravatar === 'string') {
report(`Gravatar boolean encoded as string found in ${user}`, () => {
u.gravatar = u.gravatar === 'true';
const stringified = `${JSON.stringify(u, 0, 2)}\n`;
fs.writeFile(path.join('users', user), stringified, () => {});
fs.writeFile(path.join('users', user), stringified, () => { });
});
}
} catch ({ message }) {
@ -55,11 +50,11 @@ function report(content, fix) {
}
});
const themes = await readdir('themes');
const themes = await fs.readdir('themes');
await themes.forEach(async theme => {
if (theme.endsWith('css')) {
try {
const data = await readFile(path.join('themes', theme), 'utf8');
const data = await fs.readFile(path.join('themes', theme), 'utf8');
try {
CSS.parse(data);
} catch ({ message }) {
@ -70,5 +65,4 @@ function report(content, fix) {
}
}
});
if (errored) process.exit(1);
})();

View file

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

View file

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

View file

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

View file

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

View file

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

BIN
yarn.lock

Binary file not shown.