From 8b246867ef56da1c023ec14c08244960a2dbbfd6 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sat, 8 Aug 2020 00:15:33 +1200 Subject: [PATCH] Swap fs-extra for more specialised modules Signed-off-by: Richie Bendall --- middleware/load-user.js | 11 +++++------ package.json | 4 +++- routes/post.js | 11 +++++------ test.js | 8 ++++---- yarn.lock | Bin 194834 -> 195731 bytes 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/middleware/load-user.js b/middleware/load-user.js index 0f93d492..483c2345 100644 --- a/middleware/load-user.js +++ b/middleware/load-user.js @@ -1,5 +1,5 @@ -const fs = require('fs-extra') const path = require('path') +const loadJsonFile = require('load-json-file') module.exports = async (req, res, next) => { const id = req.hostname.split('.')[0] @@ -15,11 +15,10 @@ module.exports = async (req, res, next) => { } try { - const data = await fs.readFile( - path.join(__dirname, '..', 'users', `${id}.json`), - 'utf8' - ) - res.locals.user = { ...res.locals.user, ...JSON.parse(data) } + res.locals.user = { + ...res.locals.user, + ...await loadJsonFile(path.join(__dirname, '..', 'users', `${id}.json`)) + } } catch ({ code, message }) { if (code !== 'ENOENT') { res diff --git a/package.json b/package.json index 3d442fa6..723f0d9d 100644 --- a/package.json +++ b/package.json @@ -33,13 +33,15 @@ "escape-goat": "^3.0.0", "express": "^4.17.1", "express-minify": "^1.0.0", - "fs-extra": "^9.0.1", "html-text": "^1.0.1", + "load-json-file": "^6.2.0", "md5": "^2.3.0", + "path-exists": "^4.0.0", "postcss-middleware": "^1.1.4", "postcss-preset-env": "^6.7.0", "serve-favicon": "^2.5.0", "temp-dir": "^2.0.0", + "write-json-file": "^4.3.0", "yn": "^4.0.0" }, "devDependencies": { diff --git a/routes/post.js b/routes/post.js index 14e2f9a6..c803e56b 100644 --- a/routes/post.js +++ b/routes/post.js @@ -1,9 +1,10 @@ -const fs = require('fs-extra') const path = require('path') const btoa = require('btoa') const { version } = require(path.join(__dirname, '..', 'package.json')) const size = require('any-size') const { Octokit } = require('@octokit/rest') +const pathExists = require('path-exists') +const writeJsonFile = require('write-json-file') const github = new Octokit({ // GitHub personal access token auth: process.env.github_token, @@ -57,7 +58,7 @@ module.exports = async (req, res) => { } // Check if the user file exists in the users directory - const exists = await fs.pathExists(path.join(__dirname, '..', 'users', `${id}.json`)) + const exists = await pathExists(path.join(__dirname, '..', 'users', `${id}.json`)) if (exists) { res .status(409) @@ -88,21 +89,19 @@ module.exports = async (req, res) => { } try { - const fileContent = JSON.stringify(userData, 0, 2) - await github.repos.createOrUpdateFileContents({ owner: 'remy', repo: 'mit-license', path: `users/${id}.json`, message: `Automated creation of user ${id}.`, - content: btoa(fileContent), + content: btoa(JSON.stringify(userData, 0, 2)), committer: { name: 'MIT License Bot', email: 'remy@leftlogic.com' } }) - await fs.writeFile(path.join(__dirname, '..', 'users', `${id}.json`), fileContent) + await writeJsonFile(path.join(__dirname, '..', 'users', `${id}.json`), userData, { indent: undefined }) res.status(201).send(`MIT license page created: https://${hostname}`) } catch (err) { diff --git a/test.js b/test.js index ceb01390..1eee5192 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,5 @@ -const fs = require('fs-extra') +const { promises: fs } = require('fs') +const writeJsonFile = require('write-json-file') const CSS = require('css') const { validDomainId } = require('./routes/utils') const hasFlag = require('has-flag') @@ -18,7 +19,7 @@ async function report (content, fix) { for (const user of users) { if (getExtension(user) !== 'json') { await report(`${user} is not a json file`, async () => { - await fs.remove(user) + await fs.unlink(user) }) } @@ -39,8 +40,7 @@ async function report (content, fix) { if (parsedData.version) { await report(`Version tag found in ${user}`, async () => { delete parsedData.version - const stringified = `${JSON.stringify(parsedData, 0, 2)}\n` - await fs.writeFile(path.join('users', user), stringified) + await writeJsonFile(path.join('users', user), parsedData, { indent: 2 }) }) } diff --git a/yarn.lock b/yarn.lock index 357734c3699a7ff024736c132b2f6eeb03102ccc..6e95cec52a96448449a718aff53767ca1564c9c8 100644 GIT binary patch delta 1467 zcmZ9M+lw1j9LJfc3wA}kHMSKryHZeMHZy1D%$yMlo7--(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