Swap fs-extra for more specialised modules

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
This commit is contained in:
Richie Bendall 2020-08-08 00:15:33 +12:00
parent 28e0194337
commit 8b246867ef
No known key found for this signature in database
GPG key ID: 94AE1ACB662A2A6D
5 changed files with 17 additions and 17 deletions

View file

@ -1,5 +1,5 @@
const fs = require('fs-extra')
const path = require('path') const path = require('path')
const loadJsonFile = require('load-json-file')
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
const id = req.hostname.split('.')[0] const id = req.hostname.split('.')[0]
@ -15,11 +15,10 @@ module.exports = async (req, res, next) => {
} }
try { try {
const data = await fs.readFile( res.locals.user = {
path.join(__dirname, '..', 'users', `${id}.json`), ...res.locals.user,
'utf8' ...await loadJsonFile(path.join(__dirname, '..', 'users', `${id}.json`))
) }
res.locals.user = { ...res.locals.user, ...JSON.parse(data) }
} catch ({ code, message }) { } catch ({ code, message }) {
if (code !== 'ENOENT') { if (code !== 'ENOENT') {
res res

View file

@ -33,13 +33,15 @@
"escape-goat": "^3.0.0", "escape-goat": "^3.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-minify": "^1.0.0", "express-minify": "^1.0.0",
"fs-extra": "^9.0.1",
"html-text": "^1.0.1", "html-text": "^1.0.1",
"load-json-file": "^6.2.0",
"md5": "^2.3.0", "md5": "^2.3.0",
"path-exists": "^4.0.0",
"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", "temp-dir": "^2.0.0",
"write-json-file": "^4.3.0",
"yn": "^4.0.0" "yn": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,9 +1,10 @@
const fs = require('fs-extra')
const path = require('path') const path = require('path')
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 size = require('any-size') const size = require('any-size')
const { Octokit } = require('@octokit/rest') const { Octokit } = require('@octokit/rest')
const pathExists = require('path-exists')
const writeJsonFile = require('write-json-file')
const github = new Octokit({ const github = new Octokit({
// GitHub personal access token // GitHub personal access token
auth: process.env.github_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 // 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) { if (exists) {
res res
.status(409) .status(409)
@ -88,21 +89,19 @@ module.exports = async (req, res) => {
} }
try { try {
const fileContent = JSON.stringify(userData, 0, 2)
await github.repos.createOrUpdateFileContents({ await github.repos.createOrUpdateFileContents({
owner: 'remy', owner: 'remy',
repo: 'mit-license', repo: 'mit-license',
path: `users/${id}.json`, path: `users/${id}.json`,
message: `Automated creation of user ${id}.`, message: `Automated creation of user ${id}.`,
content: btoa(fileContent), content: btoa(JSON.stringify(userData, 0, 2)),
committer: { committer: {
name: 'MIT License Bot', name: 'MIT License Bot',
email: 'remy@leftlogic.com' 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}`) res.status(201).send(`MIT license page created: https://${hostname}`)
} catch (err) { } catch (err) {

View file

@ -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 CSS = require('css')
const { validDomainId } = require('./routes/utils') const { validDomainId } = require('./routes/utils')
const hasFlag = require('has-flag') const hasFlag = require('has-flag')
@ -18,7 +19,7 @@ async function report (content, fix) {
for (const user of users) { for (const user of users) {
if (getExtension(user) !== 'json') { if (getExtension(user) !== 'json') {
await report(`${user} is not a json file`, async () => { 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) { if (parsedData.version) {
await report(`Version tag found in ${user}`, async () => { await report(`Version tag found in ${user}`, async () => {
delete parsedData.version delete parsedData.version
const stringified = `${JSON.stringify(parsedData, 0, 2)}\n` await writeJsonFile(path.join('users', user), parsedData, { indent: 2 })
await fs.writeFile(path.join('users', user), stringified)
}) })
} }

BIN
yarn.lock

Binary file not shown.