diff --git a/licenses/ISC.ejs b/licenses/ISC.ejs index 44fc3094..4f433020 100644 --- a/licenses/ISC.ejs +++ b/licenses/ISC.ejs @@ -1,6 +1,6 @@ -<% include components/header %> +<%- include('components/header') %>
<%- 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.

-<% include components/footer %> +<%- include('components/footer') %> diff --git a/licenses/MIT.ejs b/licenses/MIT.ejs index cb50611c..9823d05f 100644 --- a/licenses/MIT.ejs +++ b/licenses/MIT.ejs @@ -1,6 +1,6 @@ -<% include components/header %> +<%- include('components/header') %>
<%- 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.

-<% include components/footer %> +<%- include('components/footer') %> diff --git a/middleware/load-options.js b/middleware/load-options.js index 08dfe04c..5879c25e 100644 --- a/middleware/load-options.js +++ b/middleware/load-options.js @@ -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, } ); diff --git a/middleware/load-user.js b/middleware/load-user.js index e9ffbaea..eb4f9424 100644 --- a/middleware/load-user.js +++ b/middleware/load-user.js @@ -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' ); diff --git a/package.json b/package.json index eb7dbf11..c51cd738 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/routes/post.js b/routes/post.js index 986cd408..dae92717 100644 --- a/routes/post.js +++ b/routes/post.js @@ -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) { diff --git a/server.js b/server.js index 25432b3b..8f4c5c1d 100644 --- a/server.js +++ b/server.js @@ -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 diff --git a/test.js b/test.js index b0360c3d..a741e2ec 100644 --- a/test.js +++ b/test.js @@ -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); })(); diff --git a/users/deneme-deneme.json b/users/deneme-deneme.json index 0cd51ecb..9cd460f5 100644 --- a/users/deneme-deneme.json +++ b/users/deneme-deneme.json @@ -5,5 +5,5 @@ "format": "html", "license": "mit", "theme": "default", - "gravatar": "true" -} \ No newline at end of file + "gravatar": true +} diff --git a/users/deneme-denemk.json b/users/deneme-denemk.json index f13ddd31..c7c644ab 100644 --- a/users/deneme-denemk.json +++ b/users/deneme-denemk.json @@ -5,5 +5,5 @@ "format": "html", "license": "isc", "theme": "eula-modern", - "gravatar": "true" -} \ No newline at end of file + "gravatar": true +} diff --git a/users/deneme-denemkh.json b/users/deneme-denemkh.json index ec05090e..8c21a673 100644 --- a/users/deneme-denemkh.json +++ b/users/deneme-denemkh.json @@ -5,5 +5,5 @@ "format": "html", "license": "mit", "theme": "friendly", - "gravatar": "true" -} \ No newline at end of file + "gravatar": true +} diff --git a/users/deneme.json b/users/deneme.json index 9ca5102f..5e4074e7 100644 --- a/users/deneme.json +++ b/users/deneme.json @@ -5,5 +5,5 @@ "format": "html", "license": "mit", "theme": "default", - "gravatar": "true" -} \ No newline at end of file + "gravatar": true +} diff --git a/users/emre-atal.json b/users/emre-atal.json index 18d366b0..4e24c0a4 100644 --- a/users/emre-atal.json +++ b/users/emre-atal.json @@ -5,5 +5,5 @@ "format": "html", "license": "mit", "theme": "default", - "gravatar": "false" -} \ No newline at end of file + "gravatar": false +} diff --git a/yarn.lock b/yarn.lock index 53a3dd64..43984aa3 100644 Binary files a/yarn.lock and b/yarn.lock differ