move rewrite rules
This commit is contained in:
@ -113,30 +113,12 @@ function collectOptions (mwOptionDefinitions) {
|
|||||||
directory: process.cwd()
|
directory: process.cwd()
|
||||||
}, stored, cliOptions.server, cliOptions.middleware, cliOptions.misc)
|
}, stored, cliOptions.server, cliOptions.middleware, cliOptions.misc)
|
||||||
|
|
||||||
if (options.rewrite) {
|
|
||||||
options.rewrite = parseRewriteRules(options.rewrite)
|
|
||||||
}
|
|
||||||
// console.error(require('util').inspect(options, { depth: 3, colors: true }))
|
// console.error(require('util').inspect(options, { depth: 3, colors: true }))
|
||||||
|
|
||||||
validateOptions(options)
|
validateOptions(options)
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseRewriteRules (rules) {
|
|
||||||
return rules && rules.map(rule => {
|
|
||||||
if (t.isString(rule)) {
|
|
||||||
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
|
|
||||||
if (!(matches && matches.length >= 3)) throw new Error('Invalid rule: ' + rule)
|
|
||||||
return {
|
|
||||||
from: matches[1],
|
|
||||||
to: matches[2]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return rule
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateOptions (options) {
|
function validateOptions (options) {
|
||||||
if (!t.isNumber(options.port)) {
|
if (!t.isNumber(options.port)) {
|
||||||
tool.printError('--port must be numeric')
|
tool.printError('--port must be numeric')
|
||||||
|
@ -37,7 +37,7 @@ class MiddlewareStack extends Array {
|
|||||||
description: "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'."
|
description: "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'."
|
||||||
},
|
},
|
||||||
middleware: function (cliOptions) {
|
middleware: function (cliOptions) {
|
||||||
const options = arrayify(cliOptions.rewrite || rewriteRules)
|
const options = parseRewriteRules(arrayify(cliOptions.rewrite || rewriteRules))
|
||||||
if (options.length) {
|
if (options.length) {
|
||||||
return options.map(route => {
|
return options.map(route => {
|
||||||
if (route.to) {
|
if (route.to) {
|
||||||
@ -295,3 +295,18 @@ class MiddlewareStack extends Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = MiddlewareStack
|
module.exports = MiddlewareStack
|
||||||
|
|
||||||
|
function parseRewriteRules (rules) {
|
||||||
|
return rules && rules.map(rule => {
|
||||||
|
if (t.isString(rule)) {
|
||||||
|
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
|
||||||
|
if (!(matches && matches.length >= 3)) throw new Error('Invalid rule: ' + rule)
|
||||||
|
return {
|
||||||
|
from: matches[1],
|
||||||
|
to: matches[2]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return rule
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user