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

Print file and line number for anonymous functions #5

Open
wants to merge 2 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ module.exports = (expressApp, filename) => {
return indentation.replace(/─/g, ' ').replace(/├/g, '│').replace(/└/g, ' ')
}

function getFileLine(handler) {
try {
handler()
} catch(e) {
return e.stack.split('\n')[1].split('at')[1].trim()
}
}

function printRoutes(layer, indentation) {

let path = ' '
Expand Down Expand Up @@ -51,7 +59,12 @@ module.exports = (expressApp, filename) => {
}
methods = methods.join(', ').toUpperCase()

text.push(`${ fillWithSpaces(indentation + layer.name, COLUMN_WIDTH_TREE) }${ fillWithSpaces(path, COLUMN_WIDTH_PATH) } ${ methods }`)
let name = layer.name
if(name == "<anonymous>") {
let file = getFileLine(layer.handle)
name = file.replace(process.cwd(), ".")
}
text.push(`${ fillWithSpaces(indentation + name, COLUMN_WIDTH_TREE) }${ fillWithSpaces(path, COLUMN_WIDTH_PATH) } ${ methods }`)

if (!layer.stack && !(layer.route && layer.route.stack)) {
if (layer.handle.stack) {
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/anonymous.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
router
├── query *
├── expressInit *
├── bound dispatch /users/:id GET
│ └── app.get (./test/spec/main.js:137:49) / GET
├── bound dispatch /posts/:id GET
│ └── ./test/spec/main.js:139:55 / GET
└── bound dispatch /images/:id GET
└── __images / GET

25 changes: 25 additions & 0 deletions test/spec/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,29 @@ describe('The express-print-routes middleware', () => {

})

it('should print anonymous functions', (done) => {

let app = express()

app.get('/users/:id', (req,res) => { res.status(200); res.end() })
// eslint-disable-next-line prefer-arrow-callback
app.get('/posts/:id', function (req,res) { res.status(200); res.end() })
app.get('/images/:id', function __images(req,res) { res.status(200); res.end() })

printRoutes(app, path.join(__dirname, '../results/anonymous.generated.txt'))

setTimeout(() => {

let expected = fs.readFileSync(path.join(__dirname, '../fixtures/anonymous.expected.txt'), 'utf8')
let generated = fs.readFileSync(path.join(__dirname, '../results/anonymous.generated.txt'), 'utf8')

expect(generated).to.eql(expected)

done()

}, 100)

})


})