Unverified Commit 9dc0afd9 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Redesign custom-routes output and show headers in output (#10444)

parent c0eab7ee
...@@ -842,7 +842,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -842,7 +842,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
isModern: config.experimental.modern, isModern: config.experimental.modern,
} }
) )
printCustomRoutes({ redirects, rewrites }) printCustomRoutes({ redirects, rewrites, headers })
if (tracer) { if (tracer) {
const parsedResults = await tracer.profiler.stopProfiling() const parsedResults = await tracer.profiler.stopProfiling()
......
...@@ -4,11 +4,7 @@ import textTable from 'next/dist/compiled/text-table' ...@@ -4,11 +4,7 @@ import textTable from 'next/dist/compiled/text-table'
import path from 'path' import path from 'path'
import { isValidElementType } from 'react-is' import { isValidElementType } from 'react-is'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { import { Redirect, Rewrite, Header } from '../lib/check-custom-routes'
Redirect,
Rewrite,
getRedirectStatus,
} from '../lib/check-custom-routes'
import { import {
SSG_GET_INITIAL_PROPS_CONFLICT, SSG_GET_INITIAL_PROPS_CONFLICT,
SERVER_PROPS_GET_INIT_PROPS_CONFLICT, SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
...@@ -232,43 +228,62 @@ export async function printTreeView( ...@@ -232,43 +228,62 @@ export async function printTreeView(
export function printCustomRoutes({ export function printCustomRoutes({
redirects, redirects,
rewrites, rewrites,
headers,
}: { }: {
redirects: Redirect[] redirects: Redirect[]
rewrites: Rewrite[] rewrites: Rewrite[]
headers: Header[]
}) { }) {
const printRoutes = ( const printRoutes = (
routes: Redirect[] | Rewrite[], routes: Redirect[] | Rewrite[] | Header[],
type: 'Redirects' | 'Rewrites' type: 'Redirects' | 'Rewrites' | 'Headers'
) => { ) => {
const isRedirects = type === 'Redirects' const isRedirects = type === 'Redirects'
const isHeaders = type === 'Headers'
console.log(chalk.underline(type)) console.log(chalk.underline(type))
console.log() console.log()
console.log( /*
textTable( ┌ source
[ ├ permanent/statusCode
[ └ destination
'Source', */
'Destination', const routesStr = (routes as any[])
...(isRedirects ? ['statusCode'] : []), .map((route: { source: string }) => {
].map(str => chalk.bold(str)), let routeStr = `┌ source: ${route.source}\n`
...Object.entries(routes).map(([key, route]) => {
return [ if (!isHeaders) {
route.source, const r = route as Rewrite
route.destination, routeStr += `${isRedirects ? '├' : '└'} destination: ${
...(isRedirects r.destination
? [getRedirectStatus(route as Redirect) + ''] }\n`
: []),
]
}),
],
{
align: ['l', 'l', 'l'],
stringLength: str => stripAnsi(str).length,
} }
) if (isRedirects) {
) const r = route as Redirect
console.log() routeStr += `└ ${
r.statusCode
? `status: ${r.statusCode}`
: `permanent: ${r.permanent}`
}\n`
}
if (isHeaders) {
const r = route as Header
routeStr += `└ headers:\n`
for (let i = 0; i < r.headers.length; i++) {
const header = r.headers[i]
const last = i === headers.length - 1
routeStr += ` ${last ? '└' : '├'} ${header.key}: ${header.value}\n`
}
}
return routeStr
})
.join('\n')
console.log(routesStr, '\n')
} }
if (redirects.length) { if (redirects.length) {
...@@ -277,6 +292,9 @@ export function printCustomRoutes({ ...@@ -277,6 +292,9 @@ export function printCustomRoutes({
if (rewrites.length) { if (rewrites.length) {
printRoutes(rewrites, 'Rewrites') printRoutes(rewrites, 'Rewrites')
} }
if (headers.length) {
printRoutes(headers, 'Headers')
}
} }
type BuildManifestShape = { pages: { [k: string]: string[] } } type BuildManifestShape = { pages: { [k: string]: string[] } }
......
...@@ -7,7 +7,6 @@ import fs from 'fs-extra' ...@@ -7,7 +7,6 @@ import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp'
import { import {
launchApp, launchApp,
killApp, killApp,
...@@ -548,14 +547,22 @@ const runTests = (isDev = false) => { ...@@ -548,14 +547,22 @@ const runTests = (isDev = false) => {
const cleanStdout = stripAnsi(stdout) const cleanStdout = stripAnsi(stdout)
expect(cleanStdout).toContain('Redirects') expect(cleanStdout).toContain('Redirects')
expect(cleanStdout).toContain('Rewrites') expect(cleanStdout).toContain('Rewrites')
expect(cleanStdout).toMatch(/Source.*?Destination.*?statusCode/i) expect(cleanStdout).toContain('Headers')
expect(cleanStdout).toMatch(/source.*?/i)
expect(cleanStdout).toMatch(/destination.*?/i)
for (const route of [...manifest.redirects, ...manifest.rewrites]) { for (const route of [...manifest.redirects, ...manifest.rewrites]) {
expect(cleanStdout).toMatch( expect(cleanStdout).toContain(route.source)
new RegExp( expect(cleanStdout).toContain(route.destination)
`${escapeRegex(route.source)}.*?${escapeRegex(route.destination)}` }
)
) for (const route of manifest.headers) {
expect(cleanStdout).toContain(route.source)
for (const header of route.headers) {
expect(cleanStdout).toContain(header.key)
expect(cleanStdout).toContain(header.value)
}
} }
}) })
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment