From c67dc549995956fbce0b7179a86aaae3e687ca95 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 6 Feb 2020 13:17:38 -0600 Subject: [PATCH] Redesign custom-routes output and show headers in output --- packages/next/build/index.ts | 2 +- packages/next/build/utils.ts | 80 ++++++++++++------- .../custom-routes/test/index.test.js | 21 +++-- 3 files changed, 64 insertions(+), 39 deletions(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 22e0d4d1ba2cc..48ccd71dc90cc 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -839,7 +839,7 @@ export default async function build(dir: string, conf = null): Promise { isModern: config.experimental.modern, } ) - printCustomRoutes({ redirects, rewrites }) + printCustomRoutes({ redirects, rewrites, headers }) if (tracer) { const parsedResults = await tracer.profiler.stopProfiling() diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index d21c7be858a82..7263af07d6514 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -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[] } } diff --git a/test/integration/custom-routes/test/index.test.js b/test/integration/custom-routes/test/index.test.js index b9da3fc566d9e..1188e950aabdd 100644 --- a/test/integration/custom-routes/test/index.test.js +++ b/test/integration/custom-routes/test/index.test.js @@ -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 {