Skip to content

Commit

Permalink
Merge pull request #27 from GiovanniCardamone/dev
Browse files Browse the repository at this point in the history
Fix ignore directory if starts with '.' or '_'
  • Loading branch information
GiovanniCardamone authored Aug 12, 2020
2 parents f06cab6 + 219b059 commit c074dd9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-autoroutes",
"version": "1.0.10",
"version": "1.0.11",
"description": "Map directory structure to routes",
"main": "dist/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function scan(

function isAcceptableFile(file: string, stat: fs.Stats): boolean {
return (
!file.startsWith('.') &&
!file.startsWith('_') &&
!path.basename(file).startsWith('.') &&
!path.basename(file).startsWith('_') &&
!file.endsWith('.test.js') &&
!file.endsWith('.test.ts') &&
stat.isFile()
Expand Down
77 changes: 77 additions & 0 deletions tests/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const tap = require('tap')

const fastify = require('fastify')
const autoroutes = require('../dist')
const errorLabel = autoroutes.errorLabel

const exampleGetRoute = `module.exports = function (server) {
return {
Expand Down Expand Up @@ -268,3 +269,79 @@ tap.test('example es6 exports default module', { saveFixture: false }, (t) => {
}
)
})

tap.test(
'skip routes with starting . charater',
{ saveFixture: false },
(t) => {
const server = fastify()

const dir = t.testdir({
'.hello.js': exampleGetRouteDefaultModule,
})

server.register(autoroutes, {
dir: dir,
})

server.inject(
{
method: 'GET',
url: '/hello',
},
(err, res) => {
t.is(res.statusCode, 404)

server.inject(
{
method: 'GET',
url: '/.hello',
},
(err, res) => {
t.is(res.statusCode, 404)

t.end()
}
)
}
)
}
)

tap.test(
'skip routes with starting _ charater',
{ saveFixture: false },
(t) => {
const server = fastify()

const dir = t.testdir({
'_hello.js': exampleGetRouteDefaultModule,
})

server.register(autoroutes, {
dir: dir,
log: true,
})

server.inject(
{
method: 'GET',
url: '/hello',
},
(err, res) => {
t.is(res.statusCode, 404)

server.inject(
{
method: 'GET',
url: '/_hello',
},
(err, res) => {
t.is(res.statusCode, 404)
t.end()
}
)
}
)
}
)

0 comments on commit c074dd9

Please sign in to comment.