Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add ESLint to prepublish/commit hook to add some enforcement #1438

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .husky/pre-commit
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname $0)/_/husky.sh"

npm test
npm test
npm run lint
4 changes: 2 additions & 2 deletions __tests__/cli/index.js
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions __tests__/server/mixins.js
Expand Up @@ -33,7 +33,7 @@ describe('mixins', () => {

assert.deepStrictEqual(
_.getRemovable(db, { foreignKeySuffix: 'Id' }),
expected,
expected
)
})

Expand All @@ -45,7 +45,7 @@ describe('mixins', () => {

assert.deepStrictEqual(
_.getRemovable(db, { foreignKeySuffix: 'Id' }),
expected,
expected
)
})
})
Expand Down
4 changes: 2 additions & 2 deletions 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",
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions public/script.js
Expand Up @@ -15,7 +15,7 @@ function ResourceList({ db }) {
ResourceItem({
name,
length: Array.isArray(db[name]) && db[name].length,
}),
})
)
.join('')}
</ul>
Expand All @@ -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 }) {
Expand All @@ -56,7 +56,7 @@ function CustomRoutesBlock({ customRoutes }) {
`<tr>
<td>${rule}</td>
<td><code>⇢</code> ${customRoutes[rule]}</td>
</tr>`,
</tr>`
)
.join('')}
</table>
Expand All @@ -72,5 +72,5 @@ window
(customRoutes) =>
(document.getElementById('custom-routes').innerHTML = CustomRoutesBlock({
customRoutes,
})),
}))
)
16 changes: 8 additions & 8 deletions src/cli/run.js
Expand Up @@ -42,7 +42,7 @@ function createApp(db, routes, middlewares, argv) {

const router = jsonServer.router(
db,
foreignKeySuffix ? { foreignKeySuffix } : undefined,
foreignKeySuffix ? { foreignKeySuffix } : undefined
)

const defaultsOpts = {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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`
)
}
})
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/utils/load.js
Expand Up @@ -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.'
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/defaults.js
Expand Up @@ -40,7 +40,7 @@ module.exports = function (opts) {
logger('dev', {
skip: (req) =>
process.env.NODE_ENV === 'test' || req.path === '/favicon.ico',
}),
})
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/mixins.js
Expand Up @@ -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]) {
Expand Down
10 changes: 5 additions & 5 deletions src/server/router/plural.js
Expand Up @@ -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' : ''}`
)
}

Expand All @@ -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}`
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/router/validate-data.js
Expand Up @@ -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.`
)
}
}