From 8adae767ca4259ec091d176424ce6727070465f6 Mon Sep 17 00:00:00 2001 From: Marcin Date: Fri, 14 Jul 2023 13:01:40 -0700 Subject: [PATCH 1/2] Enforcement + Fix --- __tests__/cli/index.js | 4 ++-- __tests__/server/mixins.js | 4 ++-- package.json | 4 ++-- public/script.js | 8 ++++---- src/cli/run.js | 16 ++++++++-------- src/cli/utils/load.js | 2 +- src/server/defaults.js | 2 +- src/server/mixins.js | 2 +- src/server/router/plural.js | 10 +++++----- src/server/router/validate-data.js | 2 +- 10 files changed, 27 insertions(+), 27 deletions(-) diff --git a/__tests__/cli/index.js b/__tests__/cli/index.js index 0414b9c40..a7f54cc7f 100644 --- a/__tests__/cli/index.js +++ b/__tests__/cli/index.js @@ -38,12 +38,12 @@ describe('cli', () => { posts: [{ id: 1 }, { _id: 2 }], comments: [{ id: 1, post_id: 1 }], }), - 'db.json', + 'db.json' ) routesFile = tempWrite.sync( JSON.stringify({ '/blog/*': '/$1' }), - 'routes.json', + 'routes.json' ) ++PORT diff --git a/__tests__/server/mixins.js b/__tests__/server/mixins.js index 0fe2ab834..694ed0b03 100644 --- a/__tests__/server/mixins.js +++ b/__tests__/server/mixins.js @@ -33,7 +33,7 @@ describe('mixins', () => { assert.deepStrictEqual( _.getRemovable(db, { foreignKeySuffix: 'Id' }), - expected, + expected ) }) @@ -45,7 +45,7 @@ describe('mixins', () => { assert.deepStrictEqual( _.getRemovable(db, { foreignKeySuffix: 'Id' }), - expected, + expected ) }) }) diff --git a/package.json b/package.json index 24d8ab672..07669d932 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-server", - "version": "0.17.3", + "version": "0.17.4", "description": "Get a full fake REST API with zero coding in less than 30 seconds", "main": "./lib/server/index.js", "bin": "./lib/cli/bin.js", @@ -17,7 +17,7 @@ "build": "babel src -d lib", "toc": "markdown-toc -i README.md", "postversion": "git push && git push --tags", - "prepublish": "npm test && npm run build" + "prepublish": "npm lint & npm test && npm run build" }, "dependencies": { "body-parser": "^1.19.0", diff --git a/public/script.js b/public/script.js index 9506a8e40..53da7f797 100644 --- a/public/script.js +++ b/public/script.js @@ -15,7 +15,7 @@ function ResourceList({ db }) { ResourceItem({ name, length: Array.isArray(db[name]) && db[name].length, - }), + }) ) .join('')} @@ -40,7 +40,7 @@ window .then((response) => response.json()) .then( (db) => - (document.getElementById('resources').innerHTML = ResourcesBlock({ db })), + (document.getElementById('resources').innerHTML = ResourcesBlock({ db })) ) function CustomRoutesBlock({ customRoutes }) { @@ -56,7 +56,7 @@ function CustomRoutesBlock({ customRoutes }) { ` ${rule} ${customRoutes[rule]} - `, + ` ) .join('')} @@ -72,5 +72,5 @@ window (customRoutes) => (document.getElementById('custom-routes').innerHTML = CustomRoutesBlock({ customRoutes, - })), + })) ) diff --git a/src/cli/run.js b/src/cli/run.js index c4238f473..2e45b8f84 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -42,7 +42,7 @@ function createApp(db, routes, middlewares, argv) { const router = jsonServer.router( db, - foreignKeySuffix ? { foreignKeySuffix } : undefined, + foreignKeySuffix ? { foreignKeySuffix } : undefined ) const defaultsOpts = { @@ -141,8 +141,8 @@ module.exports = function (argv) { if (error.errno === 'EADDRINUSE') console.log( chalk.red( - `Cannot bind to the port ${error.port}. Please specify another port number either through --port argument or through the json-server.json configuration file`, - ), + `Cannot bind to the port ${error.port}. Please specify another port number either through --port argument or through the json-server.json configuration file` + ) ) else console.log('Some error occurred', error) process.exit(1) @@ -156,8 +156,8 @@ module.exports = function (argv) { // Snapshot console.log( chalk.gray( - ' Type s + enter at any time to create a snapshot of the database', - ), + ' Type s + enter at any time to create a snapshot of the database' + ) ) // Support nohup @@ -174,7 +174,7 @@ module.exports = function (argv) { const state = app.db.getState() fs.writeFileSync(file, JSON.stringify(state, null, 2), 'utf-8') console.log( - ` Saved snapshot to ${path.relative(process.cwd(), file)}\n`, + ` Saved snapshot to ${path.relative(process.cwd(), file)}\n` ) } }) @@ -217,7 +217,7 @@ module.exports = function (argv) { const isDatabaseDifferent = !_.isEqual(obj, app.db.getState()) if (isDatabaseDifferent) { console.log( - chalk.gray(` ${source} has changed, reloading...`), + chalk.gray(` ${source} has changed, reloading...`) ) server && server.destroy(() => start()) } @@ -234,7 +234,7 @@ module.exports = function (argv) { const watchedFile = path.resolve(watchedDir, file) if (watchedFile === path.resolve(argv.routes)) { console.log( - chalk.gray(` ${argv.routes} has changed, reloading...`), + chalk.gray(` ${argv.routes} has changed, reloading...`) ) server && server.destroy(() => start()) } diff --git a/src/cli/utils/load.js b/src/cli/utils/load.js index 2c095c8c8..d1f8ec877 100644 --- a/src/cli/utils/load.js +++ b/src/cli/utils/load.js @@ -53,7 +53,7 @@ module.exports = function (source) { if (typeof dataFn !== 'function') { throw new Error( - 'The database is a JavaScript file but the export is not a function.', + 'The database is a JavaScript file but the export is not a function.' ) } diff --git a/src/server/defaults.js b/src/server/defaults.js index d4cd55df5..a0a8b7a7a 100644 --- a/src/server/defaults.js +++ b/src/server/defaults.js @@ -40,7 +40,7 @@ module.exports = function (opts) { logger('dev', { skip: (req) => process.env.NODE_ENV === 'test' || req.path === '/favicon.ico', - }), + }) ) } diff --git a/src/server/mixins.js b/src/server/mixins.js index e99ce2884..482c12319 100644 --- a/src/server/mixins.js +++ b/src/server/mixins.js @@ -19,7 +19,7 @@ function getRemovable(db, opts) { // Remove foreign key suffix and pluralize it // Example postId -> posts const refName = pluralize.plural( - key.replace(new RegExp(`${opts.foreignKeySuffix}$`), ''), + key.replace(new RegExp(`${opts.foreignKeySuffix}$`), '') ) // Test if table exists if (db[refName]) { diff --git a/src/server/router/plural.js b/src/server/router/plural.js index 0e73dc399..b3695d9ee 100644 --- a/src/server/router/plural.js +++ b/src/server/router/plural.js @@ -165,7 +165,7 @@ module.exports = (db, name, opts) => { res.setHeader('X-Total-Count', chain.size()) res.setHeader( 'Access-Control-Expose-Headers', - `X-Total-Count${_page ? ', Link' : ''}`, + `X-Total-Count${_page ? ', Link' : ''}` ) } @@ -180,28 +180,28 @@ module.exports = (db, name, opts) => { if (page.first) { links.first = fullURL.replace( `page=${page.current}`, - `page=${page.first}`, + `page=${page.first}` ) } if (page.prev) { links.prev = fullURL.replace( `page=${page.current}`, - `page=${page.prev}`, + `page=${page.prev}` ) } if (page.next) { links.next = fullURL.replace( `page=${page.current}`, - `page=${page.next}`, + `page=${page.next}` ) } if (page.last) { links.last = fullURL.replace( `page=${page.current}`, - `page=${page.last}`, + `page=${page.last}` ) } diff --git a/src/server/router/validate-data.js b/src/server/router/validate-data.js index b4f112a32..5fc4bfeba 100644 --- a/src/server/router/validate-data.js +++ b/src/server/router/validate-data.js @@ -20,7 +20,7 @@ module.exports = (obj) => { `Data must be an object. Found ${ Array.isArray(obj) ? 'array' : typeof obj }. - 'See https://github.com/typicode/json-server for example.`, + 'See https://github.com/typicode/json-server for example.` ) } } From 55e65eab9e4f726650707ed510b953b56b4167af Mon Sep 17 00:00:00 2001 From: Marcin Date: Fri, 14 Jul 2023 13:02:39 -0700 Subject: [PATCH 2/2] Add lint hook --- .husky/pre-commit | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index e6feac5b5..74a0ba519 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,5 @@ #!/bin/sh . "$(dirname $0)/_/husky.sh" -npm test \ No newline at end of file +npm test +npm run lint \ No newline at end of file