Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): swizzle minor improvements #3225

Merged
merged 3 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export default function pluginContentDocs(
return path.resolve(__dirname, './theme');
},

getTypeScriptThemePath() {
return path.resolve(__dirname, '..', 'src', 'theme');
},

extendCli(cli) {
const command = isDefaultPluginId
? 'docs:version'
Expand Down
76 changes: 34 additions & 42 deletions packages/docusaurus/src/commands/swizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import importFresh from 'import-fresh';
import path from 'path';
import {Plugin, LoadContext, PluginConfig} from '@docusaurus/types';
import leven from 'leven';

import {partition} from 'lodash';
import {THEME_PATH} from '../constants';
import {loadContext, loadPluginConfigs} from '../server';
import initPlugins from '../server/plugins/init';
Expand Down Expand Up @@ -80,49 +80,45 @@ function getComponentName(
return readComponent(themePath);
}

function themeComponents(
themePath: string,
plugin: Plugin<unknown>,
danger: boolean,
): string {
const components = colorCode(themePath, plugin, danger);
return `Theme Components available for swizzle:\n${components.join('\n')}`;
function themeComponents(themePath: string, plugin: Plugin<unknown>): string {
const components = colorCode(themePath, plugin);

if (components.length === 0) {
return `${chalk.red('No component to swizzle')}`;
}

return `
${chalk.cyan('Theme Components available for swizzle')}

${chalk.green('green =>')} recommended: lower breaking change risk
${chalk.red('red =>')} internal: higher breaking change risk

${components.join('\n')}
`;
}

function formatedThemeNames(themeNames: string[]): string {
return `Themes available for swizzle:\n${themeNames.join('\n')}`;
}

function colorCode(
themePath: string,
plugin: any,
danger: boolean,
): Array<string> {
function colorCode(themePath: string, plugin: any): Array<string> {
// support both commonjs and ES style exports
const getSwizzleComponentList =
plugin.default?.getSwizzleComponentList ?? plugin.getSwizzleComponentList;
if (getSwizzleComponentList) {
const allowedComponent = getSwizzleComponentList();
if (danger) {
const components = readComponent(themePath);
const componentMap = allowedComponent.reduce(
(acc: {[key: string]: boolean}, component) => {
acc[component] = true;
return acc;
},
{},
);
const colorCodedComponent = components
.filter((component) => !componentMap[component])
.map((component) => chalk.red(component));
return [
...allowedComponent.map((component) => chalk.green(component)),
...colorCodedComponent,
];
}
return allowedComponent;
}
return readComponent(themePath);

const components = readComponent(themePath);
const allowedComponent = getSwizzleComponentList
? getSwizzleComponentList()
: [];

const [greenComponents, redComponents] = partition(components, (comp) =>
allowedComponent.includes(comp),
);

return [
...greenComponents.map((component) => chalk.green(component)),
...redComponents.map((component) => chalk.red(component)),
];
}

export default async function swizzle(
Expand Down Expand Up @@ -228,18 +224,14 @@ export default async function swizzle(
`Component ${componentName} not found.${
suggestion
? ` Did you mean "${suggestion}"?`
: `${themeComponents(
themePath,
pluginModule,
Boolean(danger),
)}`
: `${themeComponents(themePath, pluginModule)}`
}`,
);
}
}
if (!components.includes(componentName) && !danger) {
throw new Error(
`${componentName} is an internal component, if you want to swizzle it use "--danger" flag.`,
`${componentName} is an internal component, and have a higher breaking change probability. If you want to swizzle it, use the "--danger" flag.`,
);
}
await fs.copy(fromPath, toPath);
Expand All @@ -266,7 +258,7 @@ export default async function swizzle(
);
}
} else {
console.log(themeComponents(themePath, pluginModule, Boolean(danger)));
console.log(themeComponents(themePath, pluginModule));
}
}
}