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 357734c3..6e95cec5 100644 Binary files a/yarn.lock and b/yarn.lock differ