Skip to content

Commit

Permalink
Merge 84e1851 into c460df4
Browse files Browse the repository at this point in the history
  • Loading branch information
ties-v committed Feb 1, 2019
2 parents c460df4 + 84e1851 commit 01ac87c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,21 @@ the above tree would generate the following routes:
/foo/thing
/bar/foo
```

**Multiple file extensions**
```javascript
// index.js
const { send } = require('micro')

// set up the config to both include .js and .ts files.
const config = {ext: ['.js', '.ts']}

// pass config to `fs-router` as optional second paramater
let match = require('fs-router')(__dirname + '/routes', config)

module.exports = async function(req, res) {
let matched = match(req)
if (matched) return await matched(req, res)
send(res, 404, { error: 'Not found' })
}
```
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ function addMatch (route) {
}

// recursively searches for all js files inside a directory tree, and returns their full paths
function findRoutes (dir) {
function findRoutes (dir, fileExtensions) {
// fileExtensions = (fileExtensions instanceof Array) ? fileExtensions : ['.js']
let files = fs.readdirSync(dir)
let resolve = f => path.join(dir, f)
let routes = files.filter(f => path.extname(f) === '.js').map(resolve)
let routes = files.filter(f => fileExtensions.indexOf(path.extname(f)) !== -1).map(resolve)
let dirs = files.filter(f => fs.statSync(path.join(dir, f)).isDirectory()).map(resolve)
return routes.concat(...dirs.map(findRoutes))
return routes.concat(...dirs.map(subdir => findRoutes(subdir, fileExtensions)))
}

const val = v => (typeof v === 'undefined' ? 0 : v)

module.exports = function router (routesDir, config) {

const routes = findRoutes(routesDir)
const fileExtensions = config && config.ext && config.ext instanceof Array ? config.ext : ['.js']
const routes = findRoutes(routesDir, fileExtensions)
// if filter function is set, filter routes
.filter(config && config.filter || function () { return true })
// require route files, then add a 'path' property to them
Expand Down
22 changes: 22 additions & 0 deletions test/extensions-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path')
const test = require('tape')
const router = require('..')

// use both ts and js scripts
const config = {ext: ['.js', '.ts']}

let match = router(path.join(__dirname, '/fixtures/extensions'), config)

test('includes the index.js', t => {
t.plan(2)
let req = { url: '/', method: 'GET' }
t.equal(typeof match(req), 'function', 'returns the route function')
t.equal(match(req)(), 'index', 'route function is callable')
})

test('includes the typescript file', t => {
t.plan(2)
let req = { url: '/typescript', method: 'GET' }
t.equal(typeof match(req), 'function', 'returns the route function')
t.equal(match(req)(), 'typescript', 'route function is callable')
})
1 change: 1 addition & 0 deletions test/fixtures/extensions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'index'
1 change: 1 addition & 0 deletions test/fixtures/extensions/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'typescript'

0 comments on commit 01ac87c

Please sign in to comment.