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 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

View file

@ -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": {

View file

@ -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) {

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 { 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 })
})
}

BIN
yarn.lock

Binary file not shown.