Add chalk non-blocking errors + fix config bug

This commit is contained in:
slorber 2020-06-03 19:28:57 +02:00
parent 77c7ac0ddb
commit 00a79f69c7
3 changed files with 49 additions and 13 deletions

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import {flatten, uniqBy, difference} from 'lodash'; import {flatten, uniqBy, difference, groupBy} from 'lodash';
import { import {
RedirectsCreator, RedirectsCreator,
PluginContext, PluginContext,
@ -19,6 +19,8 @@ import {
} from './redirectCreators'; } from './redirectCreators';
import {validateRedirect} from './redirectValidation'; import {validateRedirect} from './redirectValidation';
import chalk from 'chalk';
export default function collectRedirects( export default function collectRedirects(
pluginContext: PluginContext, pluginContext: PluginContext,
): RedirectMetadata[] { ): RedirectMetadata[] {
@ -49,11 +51,8 @@ function validateCollectedRedirects(
} }
const allowedToPaths = pluginContext.routesPaths; const allowedToPaths = pluginContext.routesPaths;
const toPaths = redirects.map((redirect) => redirect.toRoutePath); const toPaths = redirects.map((redirect) => redirect.toRoutePath);
const illegalToPaths = difference(toPaths, allowedToPaths); const illegalToPaths = difference(toPaths, allowedToPaths);
if (illegalToPaths.length > 0) { if (illegalToPaths.length > 0) {
throw new Error( throw new Error(
`You are trying to create client-side redirections to paths that do not exist: `You are trying to create client-side redirections to paths that do not exist:
@ -70,12 +69,37 @@ function filterUnwantedRedirects(
redirects: RedirectMetadata[], redirects: RedirectMetadata[],
pluginContext: PluginContext, pluginContext: PluginContext,
): RedirectMetadata[] { ): RedirectMetadata[] {
// TODO how should we warn the user of filtered redirects?
// we don't want to create twice the same redirect // we don't want to create twice the same redirect
// that would lead to writing twice the same html redirection file
Object.entries(
groupBy(redirects, (redirect) => redirect.fromRoutePath),
).forEach(([from, groupedFromRedirects]) => {
if (groupedFromRedirects.length > 1) {
console.error(
chalk.red(
`@docusaurus/plugin-client-redirects: multiple redirects are created with the same "from" pathname=${from}
It is not possible to redirect the same pathname to multiple destinations:
- ${groupedFromRedirects.map((r) => JSON.stringify(r)).join('\n- ')}
`,
),
);
}
});
redirects = uniqBy(redirects, (redirect) => redirect.fromRoutePath); redirects = uniqBy(redirects, (redirect) => redirect.fromRoutePath);
// We don't want to override an existing route // We don't want to override an already existing route with a redirect file!
const redirectsOverridingExistingPath = redirects.filter((redirect) =>
pluginContext.routesPaths.includes(redirect.fromRoutePath),
);
if (redirectsOverridingExistingPath.length > 0) {
console.error(
chalk.red(
`@docusaurus/plugin-client-redirects: some redirects would override existing paths, and will be ignored:
- ${redirectsOverridingExistingPath.map((r) => JSON.stringify(r)).join('\n- ')}
`,
),
);
}
redirects = redirects.filter( redirects = redirects.filter(
(redirect) => !pluginContext.routesPaths.includes(redirect.fromRoutePath), (redirect) => !pluginContext.routesPaths.includes(redirect.fromRoutePath),
); );
@ -143,7 +167,14 @@ function createRoutePathRedirects(
routePath: string, routePath: string,
redirectCreator: RedirectsCreator, redirectCreator: RedirectsCreator,
): RedirectMetadata[] { ): RedirectMetadata[] {
const fromRoutePaths: string[] = redirectCreator(routePath) ?? []; const fromRoutePathsMixed: string | string[] =
redirectCreator(routePath) || [];
const fromRoutePaths: string[] =
typeof fromRoutePathsMixed === 'string'
? [fromRoutePathsMixed]
: fromRoutePathsMixed;
return fromRoutePaths.map((fromRoutePath) => { return fromRoutePaths.map((fromRoutePath) => {
return { return {
fromRoutePath, fromRoutePath,

View file

@ -33,7 +33,7 @@ export type PluginContext = Pick<
// return all the paths from which we should redirect from // return all the paths from which we should redirect from
export type RedirectsCreator = ( export type RedirectsCreator = (
routePath: string, routePath: string,
) => string[] | null | undefined; ) => string[] | string | null | undefined;
// In-memory representation of redirects we want: easier to test // In-memory representation of redirects we want: easier to test
// /!\ easy to be confused: "fromRoutePath" is the new page we should create, // /!\ easy to be confused: "fromRoutePath" is the new page we should create,

View file

@ -41,14 +41,19 @@ module.exports = {
to: '/docs', to: '/docs',
from: '/plugin-client-redirects-tests/toDocs2', from: '/plugin-client-redirects-tests/toDocs2',
}, },
{
to: '/docs',
from: '/plugin-client-redirects-tests/toHomeDuplicatePath',
},
], ],
createRedirects: function (existingPath) { createRedirects: function (existingPath) {
if (existingPath === '/') { if (existingPath === '/') {
return [ return [
[ '/',
'/docs',
'/plugin-client-redirects-tests/toHome3', '/plugin-client-redirects-tests/toHome3',
'/plugin-client-redirects-tests/toHome4', '/plugin-client-redirects-tests/toHome4',
], '/plugin-client-redirects-tests/toHomeDuplicatePath',
]; ];
} }
}, },