Merge pull request #1546 from sharelatex/sk-add-routes-script

Add a script to print all routes in the project

GitOrigin-RevId: 47e55bb24fb16864296222b7a87b21901c56f084
This commit is contained in:
Hugh O'Brien
2019-02-28 10:57:35 +00:00
committed by James Allen
parent 0d4c92054b
commit 72933bdd81
4 changed files with 226 additions and 14 deletions

View File

@@ -83,6 +83,12 @@ Unit test status
[![Unit test status](https://travis-ci.org/sharelatex/web-sharelatex.png?branch=master)](https://travis-ci.org/sharelatex/web-sharelatex)
Routes
------
Run `bin/routes` to print out all routes in the project.
License and Credits
-------------------

129
services/web/bin/routes Executable file
View File

@@ -0,0 +1,129 @@
#! /usr/bin/env node
const acorn = require('acorn')
const acornWalk = require('acorn-walk')
const fs = require('fs')
const _ = require('lodash')
const glob = require('glob')
print = console.log
const Methods = new Set([
'get',
'head',
'post',
'put',
'delete',
'connect',
'options',
'trace',
'patch'
])
const isMethod = str => {
return Methods.has(str)
}
// Check if the expression is a call on a router, return data about it, or null
const routerCall = callExpression => {
const callee = callExpression.callee
const property = callee.property
const args = callExpression.arguments
if (!callee.object || !callee.object.name) {
return false
}
const routerName = callee.object.name
if ( // Match known names for the Express routers: app, webRouter, whateverRouter, etc...
isMethod(property.name) &&
(routerName === 'app' || routerName.match('^.*[rR]outer$'))
) {
return {
routerName: routerName,
method: property.name,
args: args
}
} else {
return null
}
}
const formatMethodCall = expression => {
if (!expression.object || !expression.property) {
return '????'
}
return `${expression.object.name}.${expression.property.name}`
}
const parseAndPrintRoutesSync = path => {
const content = fs.readFileSync(path)
// Walk the AST (Abstract Syntax Tree)
acornWalk.simple(acorn.parse(content), {
// We only care about call expression ( like `a.b()` )
CallExpression(node) {
const call = routerCall(node)
if (call) {
const firstArg = _.first(call.args)
const lastArg = _.last(call.args)
try {
print(
` ${formatRouterName(call.routerName)}\t .${call.method} \t: ${
firstArg.value
} => ${formatMethodCall(lastArg)}`
)
} catch (e) {
print('>> Error')
print(e)
print(JSON.stringify(call))
process.exit(1)
}
}
}
})
}
const routerNameMapping = {
'privateApiRouter': 'privateApi',
'publicApiRouter': 'publicApi'
}
const formatRouterName = (name) => {
return routerNameMapping[name] || name
}
const main = () => {
// Take an optional filter to apply to file names
const filter = process.argv[2] || null
if (filter && (filter === '--help' || filter == 'help')) {
print('')
print(' Usage: bin/routes [filter]')
print(' Examples:')
print(' bin/routes')
print(' bin/routes GitBridge')
print('')
process.exit(0)
}
// Find all routers
glob('*[rR]outer.js', { matchBase: true }, (err, files) => {
for (file of files) {
if (file.match('^node_modules.*$') || file.match('.*/public/.*')) {
continue
}
// Restrict to the filter (if filter is present)
if (filter && !file.match(`.*${filter}.*`)) {
continue
}
print(`[${file}]`)
try {
parseAndPrintRoutesSync(file)
} catch (_e) {
print('>> Error parsing file')
continue
}
}
process.exit(0)
})
}
if (require.main === module) {
main()
}

View File

@@ -340,9 +340,10 @@
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.2.13.tgz"
},
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.1.0 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz"
"version": "6.1.0",
"from": "acorn@6.1.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-6.1.0.tgz",
"dev": true
},
"acorn-dynamic-import": {
"version": "2.0.2",
@@ -374,6 +375,20 @@
"version": "3.0.1",
"from": "acorn-jsx@>=3.0.0 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz",
"dev": true,
"dependencies": {
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.0.4 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz",
"dev": true
}
}
},
"acorn-walk": {
"version": "6.1.1",
"from": "acorn-walk@6.1.1",
"resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-6.1.1.tgz",
"dev": true
},
"after": {
@@ -3925,6 +3940,18 @@
"from": "file-utils@>=0.1.5 <0.2.0",
"resolved": "https://registry.npmjs.org/file-utils/-/file-utils-0.1.5.tgz",
"dependencies": {
"glob": {
"version": "3.2.11",
"from": "glob@>=3.2.6 <3.3.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz",
"dependencies": {
"minimatch": {
"version": "0.3.0",
"from": "minimatch@>=0.3.0 <0.4.0",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz"
}
}
},
"lodash": {
"version": "2.1.0",
"from": "lodash@>=2.1.0 <2.2.0",
@@ -3983,10 +4010,20 @@
"from": "findup-sync@>=0.1.2 <0.2.0",
"resolved": "https://registry.npmjs.org/findup-sync/-/findup-sync-0.1.3.tgz",
"dependencies": {
"glob": {
"version": "3.2.11",
"from": "glob@>=3.2.9 <3.3.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz"
},
"lodash": {
"version": "2.4.2",
"from": "lodash@>=2.4.1 <2.5.0",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz"
},
"minimatch": {
"version": "0.3.0",
"from": "minimatch@>=0.3.0 <0.4.0",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz"
}
}
},
@@ -4280,14 +4317,16 @@
"resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz"
},
"glob": {
"version": "3.2.11",
"from": "glob@>=3.2.6 <3.3.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz",
"version": "7.1.3",
"from": "glob@7.1.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz",
"dev": true,
"dependencies": {
"minimatch": {
"version": "0.3.0",
"from": "minimatch@>=0.3.0 <0.4.0",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz"
"version": "3.0.4",
"from": "minimatch@>=3.0.4 <4.0.0",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"dev": true
}
}
},
@@ -5396,7 +5435,14 @@
"is-expression": {
"version": "2.1.0",
"from": "is-expression@>=2.0.1 <3.0.0",
"resolved": "https://registry.npmjs.org/is-expression/-/is-expression-2.1.0.tgz"
"resolved": "https://registry.npmjs.org/is-expression/-/is-expression-2.1.0.tgz",
"dependencies": {
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.3.0 <3.4.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz"
}
}
},
"is-extendable": {
"version": "0.1.1",
@@ -9267,7 +9313,14 @@
"constantinople": {
"version": "3.1.0",
"from": "constantinople@>=3.0.1 <4.0.0",
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-3.1.0.tgz"
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-3.1.0.tgz",
"dependencies": {
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.1.0 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz"
}
}
}
}
},
@@ -9279,12 +9332,26 @@
"constantinople": {
"version": "3.1.0",
"from": "constantinople@>=3.0.1 <4.0.0",
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-3.1.0.tgz"
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-3.1.0.tgz",
"dependencies": {
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.1.0 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz"
}
}
},
"with": {
"version": "5.1.1",
"from": "with@>=5.0.0 <6.0.0",
"resolved": "https://registry.npmjs.org/with/-/with-5.1.1.tgz"
"resolved": "https://registry.npmjs.org/with/-/with-5.1.1.tgz",
"dependencies": {
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.1.0 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz"
}
}
}
}
},
@@ -9301,7 +9368,14 @@
"constantinople": {
"version": "3.1.0",
"from": "constantinople@>=3.0.1 <4.0.0",
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-3.1.0.tgz"
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-3.1.0.tgz",
"dependencies": {
"acorn": {
"version": "3.3.0",
"from": "acorn@>=3.1.0 <4.0.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz"
}
}
},
"source-map": {
"version": "0.5.7",

View File

@@ -111,6 +111,8 @@
"yauzl": "^2.10.0"
},
"devDependencies": {
"acorn": "^6.1.0",
"acorn-walk": "^6.1.1",
"autoprefixer": "^6.6.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
@@ -137,6 +139,7 @@
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-react": "^7.11.1",
"eslint-plugin-standard": "^3.0.1",
"glob": "^7.1.3",
"grunt": "0.4.5",
"grunt-bunyan": "0.5.0",
"grunt-cli": "^1.2.0",