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> {
isModern: config.experimental.modern,
}
)
printCustomRoutes({ redirects, rewrites })
printCustomRoutes({ redirects, rewrites, headers })
if (tracer) {
const parsedResults = await tracer.profiler.stopProfiling()
......
......@@ -4,11 +4,7 @@ import textTable from 'next/dist/compiled/text-table'
import path from 'path'
import { isValidElementType } from 'react-is'
import stripAnsi from 'strip-ansi'
import {
Redirect,
Rewrite,
getRedirectStatus,
} from '../lib/check-custom-routes'
import { Redirect, Rewrite, Header } from '../lib/check-custom-routes'
import {
SSG_GET_INITIAL_PROPS_CONFLICT,
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
......@@ -232,43 +228,62 @@ export async function printTreeView(
export function printCustomRoutes({
redirects,
rewrites,
headers,
}: {
redirects: Redirect[]
rewrites: Rewrite[]
headers: Header[]
}) {
const printRoutes = (
routes: Redirect[] | Rewrite[],
type: 'Redirects' | 'Rewrites'
routes: Redirect[] | Rewrite[] | Header[],
type: 'Redirects' | 'Rewrites' | 'Headers'
) => {
const isRedirects = type === 'Redirects'
const isHeaders = type === 'Headers'
console.log(chalk.underline(type))
console.log()
console.log(
textTable(
[
[
'Source',
'Destination',
...(isRedirects ? ['statusCode'] : []),
].map(str => chalk.bold(str)),
...Object.entries(routes).map(([key, route]) => {
return [
route.source,
route.destination,
...(isRedirects
? [getRedirectStatus(route as Redirect) + '']
: []),
]
}),
],
{
align: ['l', 'l', 'l'],
stringLength: str => stripAnsi(str).length,
/*
┌ source
├ permanent/statusCode
└ destination
*/
const routesStr = (routes as any[])
.map((route: { source: string }) => {
let routeStr = `┌ source: ${route.source}\n`
if (!isHeaders) {
const r = route as Rewrite
routeStr += `${isRedirects ? '├' : '└'} destination: ${
r.destination
}\n`
}
)
)
console.log()
if (isRedirects) {
const r = route as Redirect
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) {
......@@ -277,6 +292,9 @@ export function printCustomRoutes({
if (rewrites.length) {
printRoutes(rewrites, 'Rewrites')
}
if (headers.length) {
printRoutes(headers, 'Headers')
}
}
type BuildManifestShape = { pages: { [k: string]: string[] } }
......
......@@ -7,7 +7,6 @@ import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp'
import {
launchApp,
killApp,
......@@ -548,14 +547,22 @@ const runTests = (isDev = false) => {
const cleanStdout = stripAnsi(stdout)
expect(cleanStdout).toContain('Redirects')
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]) {
expect(cleanStdout).toMatch(
new RegExp(
`${escapeRegex(route.source)}.*?${escapeRegex(route.destination)}`
)
)
expect(cleanStdout).toContain(route.source)
expect(cleanStdout).toContain(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 {
......
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