@docusaurus/core 0.0.0-4630 → 0.0.0-4633
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/beforeCli.mjs +2 -2
- package/bin/docusaurus.mjs +19 -11
- package/lib/choosePort.js +22 -30
- package/lib/client/serverEntry.js +7 -9
- package/lib/commands/build.js +2 -2
- package/lib/commands/clear.js +3 -3
- package/lib/commands/deploy.js +8 -7
- package/lib/commands/swizzle/actions.d.ts +23 -0
- package/lib/commands/swizzle/actions.js +102 -0
- package/lib/commands/swizzle/common.d.ts +33 -0
- package/lib/commands/swizzle/common.js +57 -0
- package/lib/commands/swizzle/components.d.ts +29 -0
- package/lib/commands/swizzle/components.js +165 -0
- package/lib/commands/swizzle/config.d.ts +10 -0
- package/lib/commands/swizzle/config.js +77 -0
- package/lib/commands/swizzle/context.d.ts +8 -0
- package/lib/commands/swizzle/context.js +30 -0
- package/lib/commands/swizzle/index.d.ts +8 -0
- package/lib/commands/swizzle/index.js +115 -0
- package/lib/commands/swizzle/prompts.d.ts +12 -0
- package/lib/commands/swizzle/prompts.js +110 -0
- package/lib/commands/swizzle/tables.d.ts +9 -0
- package/lib/commands/swizzle/tables.js +116 -0
- package/lib/commands/swizzle/themes.d.ts +20 -0
- package/lib/commands/swizzle/themes.js +105 -0
- package/lib/commands/writeTranslations.js +1 -1
- package/lib/server/brokenLinks.js +1 -1
- package/lib/server/configValidation.js +1 -1
- package/lib/server/plugins/init.d.ts +11 -1
- package/lib/server/plugins/init.js +8 -3
- package/lib/server/translations/translations.js +3 -2
- package/lib/server/translations/translationsExtractor.js +3 -5
- package/lib/server/versions/index.d.ts +0 -1
- package/lib/server/versions/index.js +3 -6
- package/lib/webpack/plugins/CleanWebpackPlugin.js +3 -3
- package/package.json +13 -11
- package/lib/commands/swizzle.d.ts +0 -9
- package/lib/commands/swizzle.js +0 -239
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.getThemeSwizzleConfig = exports.normalizeSwizzleConfig = void 0;
|
|
10
|
+
const utils_validation_1 = require("@docusaurus/utils-validation");
|
|
11
|
+
const common_1 = require("./common");
|
|
12
|
+
const themes_1 = require("./themes");
|
|
13
|
+
function getModuleSwizzleConfig(swizzlePlugin) {
|
|
14
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
15
|
+
const getSwizzleConfig = (_d = (_b = (_a = swizzlePlugin.plugin.plugin) === null || _a === void 0 ? void 0 : _a.getSwizzleConfig) !== null && _b !== void 0 ? _b : (_c = swizzlePlugin.plugin.pluginModule) === null || _c === void 0 ? void 0 : _c.module.getSwizzleConfig) !== null && _d !== void 0 ? _d : (_f = (_e = swizzlePlugin.plugin.pluginModule) === null || _e === void 0 ? void 0 : _e.module) === null || _f === void 0 ? void 0 : _f.getSwizzleConfig;
|
|
16
|
+
if (getSwizzleConfig) {
|
|
17
|
+
return getSwizzleConfig();
|
|
18
|
+
}
|
|
19
|
+
// TODO deprecate getSwizzleComponentList later
|
|
20
|
+
const getSwizzleComponentList = (_k = (_h = (_g = swizzlePlugin.plugin.plugin) === null || _g === void 0 ? void 0 : _g.getSwizzleComponentList) !== null && _h !== void 0 ? _h : (_j = swizzlePlugin.plugin.pluginModule) === null || _j === void 0 ? void 0 : _j.module.getSwizzleComponentList) !== null && _k !== void 0 ? _k : (_m = (_l = swizzlePlugin.plugin.pluginModule) === null || _l === void 0 ? void 0 : _l.module) === null || _m === void 0 ? void 0 : _m.getSwizzleComponentList;
|
|
21
|
+
if (getSwizzleComponentList) {
|
|
22
|
+
const safeComponents = (_o = getSwizzleComponentList()) !== null && _o !== void 0 ? _o : [];
|
|
23
|
+
const safeComponentConfig = {
|
|
24
|
+
actions: {
|
|
25
|
+
eject: 'safe',
|
|
26
|
+
wrap: 'safe',
|
|
27
|
+
},
|
|
28
|
+
description: undefined,
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
components: Object.fromEntries(safeComponents.map((comp) => [comp, safeComponentConfig])),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
function normalizeSwizzleConfig(unsafeSwizzleConfig) {
|
|
37
|
+
const schema = utils_validation_1.Joi.object({
|
|
38
|
+
components: utils_validation_1.Joi.object()
|
|
39
|
+
.pattern(utils_validation_1.Joi.string(), utils_validation_1.Joi.object({
|
|
40
|
+
actions: utils_validation_1.Joi.object().pattern(utils_validation_1.Joi.string().valid(...common_1.SwizzleActions), utils_validation_1.Joi.string().valid(...common_1.SwizzleActionsStatuses)),
|
|
41
|
+
description: utils_validation_1.Joi.string(),
|
|
42
|
+
}))
|
|
43
|
+
.required(),
|
|
44
|
+
});
|
|
45
|
+
const result = schema.validate(unsafeSwizzleConfig);
|
|
46
|
+
if (result.error) {
|
|
47
|
+
throw new Error(`Swizzle config does not match expected schema: ${result.error.message}`);
|
|
48
|
+
}
|
|
49
|
+
const swizzleConfig = result.value;
|
|
50
|
+
// Ensure all components always declare all actions
|
|
51
|
+
Object.values(swizzleConfig.components).forEach((componentConfig) => {
|
|
52
|
+
common_1.SwizzleActions.forEach((action) => {
|
|
53
|
+
if (!componentConfig.actions[action]) {
|
|
54
|
+
componentConfig.actions[action] = 'unsafe';
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
return swizzleConfig;
|
|
59
|
+
}
|
|
60
|
+
exports.normalizeSwizzleConfig = normalizeSwizzleConfig;
|
|
61
|
+
const FallbackSwizzleConfig = {
|
|
62
|
+
components: {},
|
|
63
|
+
};
|
|
64
|
+
function getThemeSwizzleConfig(themeName, plugins) {
|
|
65
|
+
const plugin = (0, themes_1.getPluginByThemeName)(plugins, themeName);
|
|
66
|
+
const config = getModuleSwizzleConfig(plugin);
|
|
67
|
+
if (config) {
|
|
68
|
+
try {
|
|
69
|
+
return normalizeSwizzleConfig(config);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
throw new Error(`Invalid Swizzle config for theme ${themeName}.\n${e.message}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return FallbackSwizzleConfig;
|
|
76
|
+
}
|
|
77
|
+
exports.getThemeSwizzleConfig = getThemeSwizzleConfig;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { SwizzleContext } from './common';
|
|
8
|
+
export declare function initSwizzleContext(siteDir: string): Promise<SwizzleContext>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.initSwizzleContext = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const module_1 = require("module");
|
|
12
|
+
const server_1 = require("../../server");
|
|
13
|
+
const init_1 = (0, tslib_1.__importStar)(require("../../server/plugins/init"));
|
|
14
|
+
async function initSwizzleContext(siteDir) {
|
|
15
|
+
const context = await (0, server_1.loadContext)(siteDir);
|
|
16
|
+
const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
|
|
17
|
+
const pluginConfigs = await (0, server_1.loadPluginConfigs)(context);
|
|
18
|
+
const plugins = await (0, init_1.default)({
|
|
19
|
+
pluginConfigs,
|
|
20
|
+
context,
|
|
21
|
+
});
|
|
22
|
+
const pluginsNormalized = await (0, init_1.normalizePluginConfigs)(pluginConfigs, pluginRequire);
|
|
23
|
+
return {
|
|
24
|
+
plugins: plugins.map((plugin, pluginIndex) => ({
|
|
25
|
+
plugin: pluginsNormalized[pluginIndex],
|
|
26
|
+
instance: plugin,
|
|
27
|
+
})),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.initSwizzleContext = initSwizzleContext;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { SwizzleOptions } from './common';
|
|
8
|
+
export default function swizzle(siteDir: string, themeNameParam: string | undefined, componentNameParam: string | undefined, optionsParam: Partial<SwizzleOptions>): Promise<void>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const tslib_1 = require("tslib");
|
|
10
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
11
|
+
const themes_1 = require("./themes");
|
|
12
|
+
const components_1 = require("./components");
|
|
13
|
+
const tables_1 = require("./tables");
|
|
14
|
+
const common_1 = require("./common");
|
|
15
|
+
const actions_1 = require("./actions");
|
|
16
|
+
const config_1 = require("./config");
|
|
17
|
+
const prompts_1 = require("./prompts");
|
|
18
|
+
const context_1 = require("./context");
|
|
19
|
+
async function listAllThemeComponents({ themeNames, plugins, typescript, }) {
|
|
20
|
+
const themeComponentsTables = (await Promise.all(themeNames.map(async (themeName) => {
|
|
21
|
+
const themePath = (0, themes_1.getThemePath)({ themeName, plugins, typescript });
|
|
22
|
+
const swizzleConfig = (0, config_1.getThemeSwizzleConfig)(themeName, plugins);
|
|
23
|
+
const themeComponents = await (0, components_1.getThemeComponents)({
|
|
24
|
+
themeName,
|
|
25
|
+
themePath,
|
|
26
|
+
swizzleConfig,
|
|
27
|
+
});
|
|
28
|
+
return (0, tables_1.themeComponentsTable)(themeComponents);
|
|
29
|
+
}))).join('\n\n');
|
|
30
|
+
logger_1.default.info(`All theme components available to swizzle:
|
|
31
|
+
|
|
32
|
+
${themeComponentsTables}
|
|
33
|
+
|
|
34
|
+
${(0, tables_1.helpTables)()}
|
|
35
|
+
`);
|
|
36
|
+
return process.exit(0);
|
|
37
|
+
}
|
|
38
|
+
async function ensureActionSafety({ componentName, componentConfig, action, danger, }) {
|
|
39
|
+
const actionStatus = componentConfig.actions[action];
|
|
40
|
+
if (actionStatus === 'forbidden') {
|
|
41
|
+
logger_1.default.error `
|
|
42
|
+
Swizzle action name=${action} is forbidden for component name=${componentName}
|
|
43
|
+
`;
|
|
44
|
+
return process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
if (actionStatus === 'unsafe' && !danger) {
|
|
47
|
+
logger_1.default.warn `
|
|
48
|
+
Swizzle action name=${action} is unsafe to perform on name=${componentName}.
|
|
49
|
+
It is more likely to be affected by breaking changes in the future
|
|
50
|
+
If you want to swizzle it, use the code=${'--danger'} flag, or confirm that you understand the risks.
|
|
51
|
+
`;
|
|
52
|
+
const swizzleDangerousComponent = await (0, prompts_1.askSwizzleDangerousComponent)();
|
|
53
|
+
if (!swizzleDangerousComponent) {
|
|
54
|
+
return process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
async function swizzle(siteDir, themeNameParam, componentNameParam, optionsParam) {
|
|
60
|
+
const options = (0, common_1.normalizeOptions)(optionsParam);
|
|
61
|
+
const { list, danger, typescript } = options;
|
|
62
|
+
const { plugins } = await (0, context_1.initSwizzleContext)(siteDir);
|
|
63
|
+
const themeNames = (0, themes_1.getThemeNames)(plugins);
|
|
64
|
+
if (list && !themeNameParam) {
|
|
65
|
+
await listAllThemeComponents({ themeNames, plugins, typescript });
|
|
66
|
+
}
|
|
67
|
+
const themeName = await (0, themes_1.getThemeName)({ themeNameParam, themeNames, list });
|
|
68
|
+
const themePath = (0, themes_1.getThemePath)({ themeName, plugins, typescript });
|
|
69
|
+
const swizzleConfig = (0, config_1.getThemeSwizzleConfig)(themeName, plugins);
|
|
70
|
+
const themeComponents = await (0, components_1.getThemeComponents)({
|
|
71
|
+
themeName,
|
|
72
|
+
themePath,
|
|
73
|
+
swizzleConfig,
|
|
74
|
+
});
|
|
75
|
+
const componentName = await (0, components_1.getComponentName)({
|
|
76
|
+
componentNameParam,
|
|
77
|
+
themeComponents,
|
|
78
|
+
list,
|
|
79
|
+
});
|
|
80
|
+
const componentConfig = themeComponents.getConfig(componentName);
|
|
81
|
+
const action = await (0, actions_1.getAction)(componentConfig, options);
|
|
82
|
+
await ensureActionSafety({ componentName, componentConfig, action, danger });
|
|
83
|
+
async function executeAction() {
|
|
84
|
+
switch (action) {
|
|
85
|
+
case 'wrap': {
|
|
86
|
+
const result = await (0, actions_1.wrap)({
|
|
87
|
+
siteDir,
|
|
88
|
+
themePath,
|
|
89
|
+
componentName,
|
|
90
|
+
typescript,
|
|
91
|
+
});
|
|
92
|
+
logger_1.default.success `
|
|
93
|
+
Created wrapper of name=${componentName} from name=${themeName} in path=${result.createdFiles}.
|
|
94
|
+
`;
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
case 'eject': {
|
|
98
|
+
const result = await (0, actions_1.eject)({
|
|
99
|
+
siteDir,
|
|
100
|
+
themePath,
|
|
101
|
+
componentName,
|
|
102
|
+
});
|
|
103
|
+
logger_1.default.success `
|
|
104
|
+
Ejected name=${componentName} from name=${themeName} to path=${result.createdFiles}.
|
|
105
|
+
`;
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
default:
|
|
109
|
+
throw new Error(`Unexpected action ${action}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
await executeAction();
|
|
113
|
+
return process.exit(0);
|
|
114
|
+
}
|
|
115
|
+
exports.default = swizzle;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { ThemeComponents } from './components';
|
|
8
|
+
import type { SwizzleAction, SwizzleComponentConfig } from '@docusaurus/types';
|
|
9
|
+
export declare function askThemeName(themeNames: string[]): Promise<string>;
|
|
10
|
+
export declare function askComponentName(themeComponents: ThemeComponents): Promise<string>;
|
|
11
|
+
export declare function askSwizzleDangerousComponent(): Promise<boolean>;
|
|
12
|
+
export declare function askSwizzleAction(componentConfig: SwizzleComponentConfig): Promise<SwizzleAction>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.askSwizzleAction = exports.askSwizzleDangerousComponent = exports.askComponentName = exports.askThemeName = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
12
|
+
const prompts_1 = (0, tslib_1.__importDefault)(require("prompts"));
|
|
13
|
+
const common_1 = require("./common");
|
|
14
|
+
const ExitTitle = logger_1.default.yellow('[Exit]');
|
|
15
|
+
async function askThemeName(themeNames) {
|
|
16
|
+
const { themeName } = await (0, prompts_1.default)({
|
|
17
|
+
type: 'select',
|
|
18
|
+
name: 'themeName',
|
|
19
|
+
message: 'Select a theme to swizzle:',
|
|
20
|
+
choices: themeNames
|
|
21
|
+
.map((theme) => ({ title: theme, value: theme }))
|
|
22
|
+
.concat({ title: ExitTitle, value: '[Exit]' }),
|
|
23
|
+
});
|
|
24
|
+
if (!themeName || themeName === '[Exit]') {
|
|
25
|
+
process.exit(0);
|
|
26
|
+
}
|
|
27
|
+
return themeName;
|
|
28
|
+
}
|
|
29
|
+
exports.askThemeName = askThemeName;
|
|
30
|
+
async function askComponentName(themeComponents) {
|
|
31
|
+
function formatComponentName(componentName) {
|
|
32
|
+
const anySafe = themeComponents.hasAnySafeAction(componentName);
|
|
33
|
+
const allSafe = themeComponents.hasAllSafeAction(componentName);
|
|
34
|
+
const safestStatus = anySafe ? 'safe' : 'unsafe'; // Not 100% accurate but good enough for now.
|
|
35
|
+
const partiallySafe = anySafe && !allSafe;
|
|
36
|
+
return `${componentName}${(0, common_1.actionStatusSuffix)(safestStatus, {
|
|
37
|
+
partiallySafe,
|
|
38
|
+
})}`;
|
|
39
|
+
}
|
|
40
|
+
const { componentName } = await (0, prompts_1.default)({
|
|
41
|
+
type: 'autocomplete',
|
|
42
|
+
name: 'componentName',
|
|
43
|
+
message: `
|
|
44
|
+
Select or type the component to swizzle.
|
|
45
|
+
${common_1.PartiallySafeHint} = not safe for all swizzle actions
|
|
46
|
+
`,
|
|
47
|
+
// This doesn't work well in small-height terminals (like IDE)
|
|
48
|
+
// limit: 30,
|
|
49
|
+
// This does not work well and messes up with terminal scroll position
|
|
50
|
+
// limit: Number.POSITIVE_INFINITY,
|
|
51
|
+
choices: themeComponents.all
|
|
52
|
+
.map((compName) => ({
|
|
53
|
+
title: formatComponentName(compName),
|
|
54
|
+
value: compName,
|
|
55
|
+
}))
|
|
56
|
+
.concat({ title: ExitTitle, value: '[Exit]' }),
|
|
57
|
+
async suggest(input, choices) {
|
|
58
|
+
return choices.filter((choice) => choice.title.toLowerCase().includes(input.toLowerCase()));
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
logger_1.default.newLine();
|
|
62
|
+
if (!componentName || componentName === '[Exit]') {
|
|
63
|
+
return process.exit(0);
|
|
64
|
+
}
|
|
65
|
+
return componentName;
|
|
66
|
+
}
|
|
67
|
+
exports.askComponentName = askComponentName;
|
|
68
|
+
async function askSwizzleDangerousComponent() {
|
|
69
|
+
const { switchToDanger } = await (0, prompts_1.default)({
|
|
70
|
+
type: 'select',
|
|
71
|
+
name: 'switchToDanger',
|
|
72
|
+
message: `Do you really want to swizzle this unsafe internal component?`,
|
|
73
|
+
choices: [
|
|
74
|
+
{ title: logger_1.default.green('NO: cancel and stay safe'), value: false },
|
|
75
|
+
{
|
|
76
|
+
title: logger_1.default.red('YES: I know what I am doing!'),
|
|
77
|
+
value: true,
|
|
78
|
+
},
|
|
79
|
+
{ title: ExitTitle, value: '[Exit]' },
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
if (typeof switchToDanger === 'undefined' || switchToDanger === '[Exit]') {
|
|
83
|
+
return process.exit(0);
|
|
84
|
+
}
|
|
85
|
+
return !!switchToDanger;
|
|
86
|
+
}
|
|
87
|
+
exports.askSwizzleDangerousComponent = askSwizzleDangerousComponent;
|
|
88
|
+
async function askSwizzleAction(componentConfig) {
|
|
89
|
+
const { action } = await (0, prompts_1.default)({
|
|
90
|
+
type: 'select',
|
|
91
|
+
name: 'action',
|
|
92
|
+
message: `Which swizzle action do you want to do?`,
|
|
93
|
+
choices: [
|
|
94
|
+
{
|
|
95
|
+
title: `${logger_1.default.bold('Wrap')}${(0, common_1.actionStatusSuffix)(componentConfig.actions.wrap)}`,
|
|
96
|
+
value: 'wrap',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
title: `${logger_1.default.bold('Eject')}${(0, common_1.actionStatusSuffix)(componentConfig.actions.eject)}`,
|
|
100
|
+
value: 'eject',
|
|
101
|
+
},
|
|
102
|
+
{ title: ExitTitle, value: '[Exit]' },
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
if (typeof action === 'undefined' || action === '[Exit]') {
|
|
106
|
+
return process.exit(0);
|
|
107
|
+
}
|
|
108
|
+
return action;
|
|
109
|
+
}
|
|
110
|
+
exports.askSwizzleAction = askSwizzleAction;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { ThemeComponents } from './components';
|
|
8
|
+
export declare function helpTables(): string;
|
|
9
|
+
export declare function themeComponentsTable(themeComponents: ThemeComponents): string;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.themeComponentsTable = exports.helpTables = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
12
|
+
const cli_table3_1 = (0, tslib_1.__importDefault)(require("cli-table3"));
|
|
13
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
14
|
+
const actions_1 = require("./actions");
|
|
15
|
+
const common_1 = require("./common");
|
|
16
|
+
function tableStatusLabel(status) {
|
|
17
|
+
return (0, common_1.actionStatusColor)(status, (0, common_1.actionStatusLabel)(status));
|
|
18
|
+
}
|
|
19
|
+
function getStatusLabel(status) {
|
|
20
|
+
return (0, common_1.actionStatusColor)(status, (0, common_1.actionStatusLabel)(status));
|
|
21
|
+
}
|
|
22
|
+
function statusTable() {
|
|
23
|
+
const table = new cli_table3_1.default({
|
|
24
|
+
head: ['Status', 'CLI option', 'Description'],
|
|
25
|
+
});
|
|
26
|
+
table.push({
|
|
27
|
+
[tableStatusLabel('safe')]: [
|
|
28
|
+
'',
|
|
29
|
+
`
|
|
30
|
+
This component is safe to swizzle and was designed for this purpose.
|
|
31
|
+
The swizzled component is retro-compatible with minor version upgrades.
|
|
32
|
+
`,
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
table.push({
|
|
36
|
+
[tableStatusLabel('unsafe')]: [
|
|
37
|
+
logger_1.default.code('--danger'),
|
|
38
|
+
`
|
|
39
|
+
This component is unsafe to swizzle, but you can still do it!
|
|
40
|
+
Warning: we may release breaking changes within minor version upgrades.
|
|
41
|
+
You will have to upgrade your component manually and maintain it over time.
|
|
42
|
+
|
|
43
|
+
${logger_1.default.green('Tip')}: your customization can't be done in a ${tableStatusLabel('safe')} way?
|
|
44
|
+
Report it here: https://github.com/facebook/docusaurus/discussions/5468
|
|
45
|
+
`,
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
table.push({
|
|
49
|
+
[tableStatusLabel('forbidden')]: [
|
|
50
|
+
'',
|
|
51
|
+
`
|
|
52
|
+
This component should not meant to be swizzled.
|
|
53
|
+
`,
|
|
54
|
+
],
|
|
55
|
+
});
|
|
56
|
+
return table.toString();
|
|
57
|
+
}
|
|
58
|
+
function actionsTable() {
|
|
59
|
+
const table = new cli_table3_1.default({
|
|
60
|
+
head: ['Actions', 'CLI option', 'Description'],
|
|
61
|
+
});
|
|
62
|
+
table.push({
|
|
63
|
+
[logger_1.default.bold('Wrap')]: [
|
|
64
|
+
logger_1.default.code('--wrap'),
|
|
65
|
+
`
|
|
66
|
+
Creates a wrapper around the original theme component.
|
|
67
|
+
Allows rendering other components before/after the original theme component.
|
|
68
|
+
|
|
69
|
+
${logger_1.default.green('Tip')}: prefer ${logger_1.default.code('--wrap')} whenever possible to reduces the amount of code to maintain.
|
|
70
|
+
`,
|
|
71
|
+
],
|
|
72
|
+
});
|
|
73
|
+
table.push({
|
|
74
|
+
[logger_1.default.bold('Eject')]: [
|
|
75
|
+
logger_1.default.code('--eject'),
|
|
76
|
+
`
|
|
77
|
+
Ejects the full source code of the original theme component.
|
|
78
|
+
Allows overriding the original component entirely with your own UI and logic.
|
|
79
|
+
|
|
80
|
+
${logger_1.default.green('Tip')}: ${logger_1.default.code('--eject')} can be useful to completely redesign a component.
|
|
81
|
+
`,
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
return table.toString();
|
|
85
|
+
}
|
|
86
|
+
function helpTables() {
|
|
87
|
+
return `${logger_1.default.bold('Swizzle actions')}:
|
|
88
|
+
${actionsTable()}
|
|
89
|
+
|
|
90
|
+
${logger_1.default.bold('Swizzle safety statuses')}:
|
|
91
|
+
${statusTable()}
|
|
92
|
+
|
|
93
|
+
${logger_1.default.bold('Swizzle guide')}: https://docusaurus.io/docs/swizzling`;
|
|
94
|
+
}
|
|
95
|
+
exports.helpTables = helpTables;
|
|
96
|
+
function themeComponentsTable(themeComponents) {
|
|
97
|
+
const table = new cli_table3_1.default({
|
|
98
|
+
head: [
|
|
99
|
+
'Component name',
|
|
100
|
+
...actions_1.SwizzleActions.map((action) => lodash_1.default.capitalize(action)),
|
|
101
|
+
'Description',
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
themeComponents.all.forEach((component) => {
|
|
105
|
+
table.push({
|
|
106
|
+
[component]: [
|
|
107
|
+
...actions_1.SwizzleActions.map((action) => getStatusLabel(themeComponents.getActionStatus(component, action))),
|
|
108
|
+
themeComponents.getDescription(component),
|
|
109
|
+
],
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
return `${logger_1.default.bold(`Components available for swizzle in ${logger_1.default.name(themeComponents.themeName)}`)}:
|
|
113
|
+
${table.toString()}
|
|
114
|
+
`;
|
|
115
|
+
}
|
|
116
|
+
exports.themeComponentsTable = themeComponentsTable;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import { type SwizzlePlugin } from './common';
|
|
8
|
+
export declare function pluginToThemeName(plugin: SwizzlePlugin): string | undefined;
|
|
9
|
+
export declare function getPluginByThemeName(plugins: SwizzlePlugin[], themeName: string): SwizzlePlugin;
|
|
10
|
+
export declare function getThemeNames(plugins: SwizzlePlugin[]): string[];
|
|
11
|
+
export declare function getThemeName({ themeNameParam, themeNames, list, }: {
|
|
12
|
+
themeNameParam: string | undefined;
|
|
13
|
+
themeNames: string[];
|
|
14
|
+
list: boolean | undefined;
|
|
15
|
+
}): Promise<string>;
|
|
16
|
+
export declare function getThemePath({ plugins, themeName, typescript, }: {
|
|
17
|
+
plugins: SwizzlePlugin[];
|
|
18
|
+
themeName: string;
|
|
19
|
+
typescript: boolean | undefined;
|
|
20
|
+
}): string;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.getThemePath = exports.getThemeName = exports.getThemeNames = exports.getPluginByThemeName = exports.pluginToThemeName = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
12
|
+
const leven_1 = (0, tslib_1.__importDefault)(require("leven"));
|
|
13
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
14
|
+
const prompts_1 = require("./prompts");
|
|
15
|
+
const common_1 = require("./common");
|
|
16
|
+
function pluginToThemeName(plugin) {
|
|
17
|
+
var _a;
|
|
18
|
+
if (plugin.instance.getThemePath) {
|
|
19
|
+
return ((_a = plugin.instance.version.name) !== null && _a !== void 0 ? _a : plugin.instance.name);
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
exports.pluginToThemeName = pluginToThemeName;
|
|
24
|
+
function getPluginByThemeName(plugins, themeName) {
|
|
25
|
+
const plugin = plugins.find((p) => pluginToThemeName(p) === themeName);
|
|
26
|
+
if (!plugin) {
|
|
27
|
+
throw new Error(`Theme ${themeName} not found`);
|
|
28
|
+
}
|
|
29
|
+
return plugin;
|
|
30
|
+
}
|
|
31
|
+
exports.getPluginByThemeName = getPluginByThemeName;
|
|
32
|
+
function getThemeNames(plugins) {
|
|
33
|
+
const themeNames = lodash_1.default.uniq(
|
|
34
|
+
// The fact that getThemePath is attached to the plugin instance makes
|
|
35
|
+
// this code impossible to optimize. If this is a static method, we don't
|
|
36
|
+
// need to initialize all plugins just to filter which are themes
|
|
37
|
+
// Benchmark: loadContext-58ms; initPlugins-323ms
|
|
38
|
+
plugins.map((plugin) => pluginToThemeName(plugin)).filter(Boolean));
|
|
39
|
+
// Opinionated ordering: user is most likely to swizzle:
|
|
40
|
+
// - the classic theme
|
|
41
|
+
// - official themes
|
|
42
|
+
// - official plugins
|
|
43
|
+
return lodash_1.default.orderBy(themeNames, [
|
|
44
|
+
(t) => t === '@docusaurus/theme-classic',
|
|
45
|
+
(t) => t.includes('@docusaurus/theme'),
|
|
46
|
+
(t) => t.includes('@docusaurus'),
|
|
47
|
+
], ['desc', 'desc', 'desc']);
|
|
48
|
+
}
|
|
49
|
+
exports.getThemeNames = getThemeNames;
|
|
50
|
+
// Returns a valid value if recovering is possible
|
|
51
|
+
function handleInvalidThemeName({ themeNameParam, themeNames, }) {
|
|
52
|
+
// Trying to recover invalid value
|
|
53
|
+
// We look for potential matches that only differ in casing.
|
|
54
|
+
const differentCaseMatch = (0, common_1.findStringIgnoringCase)(themeNameParam, themeNames);
|
|
55
|
+
if (differentCaseMatch) {
|
|
56
|
+
logger_1.default.warn `Theme name=${themeNameParam} doesn't exist.`;
|
|
57
|
+
logger_1.default.info `name=${differentCaseMatch} will be used instead of name=${themeNameParam}.`;
|
|
58
|
+
return differentCaseMatch;
|
|
59
|
+
}
|
|
60
|
+
// TODO recover from short theme-names here: "classic" => "@docusaurus/theme-classic"
|
|
61
|
+
// No recovery value is possible: print error
|
|
62
|
+
const suggestion = themeNames.find((name) => (0, leven_1.default)(name, themeNameParam) < 4);
|
|
63
|
+
logger_1.default.error `Theme name=${themeNameParam} not found. ${suggestion
|
|
64
|
+
? logger_1.default.interpolate `Did you mean name=${suggestion}?`
|
|
65
|
+
: logger_1.default.interpolate `Themes available for swizzle: ${themeNames}`}`;
|
|
66
|
+
return process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
async function validateThemeName({ themeNameParam, themeNames, }) {
|
|
69
|
+
const isValidName = themeNames.includes(themeNameParam);
|
|
70
|
+
if (!isValidName) {
|
|
71
|
+
return handleInvalidThemeName({
|
|
72
|
+
themeNameParam,
|
|
73
|
+
themeNames,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return themeNameParam;
|
|
77
|
+
}
|
|
78
|
+
async function getThemeName({ themeNameParam, themeNames, list, }) {
|
|
79
|
+
if (list && !themeNameParam) {
|
|
80
|
+
logger_1.default.info `Themes available for swizzle: name=${themeNames}`;
|
|
81
|
+
return process.exit(0);
|
|
82
|
+
}
|
|
83
|
+
return themeNameParam
|
|
84
|
+
? validateThemeName({ themeNameParam, themeNames })
|
|
85
|
+
: (0, prompts_1.askThemeName)(themeNames);
|
|
86
|
+
}
|
|
87
|
+
exports.getThemeName = getThemeName;
|
|
88
|
+
function getThemePath({ plugins, themeName, typescript, }) {
|
|
89
|
+
var _a, _b, _c, _d;
|
|
90
|
+
const pluginInstance = getPluginByThemeName(plugins, themeName);
|
|
91
|
+
const themePath = typescript
|
|
92
|
+
? (_b = (_a = pluginInstance.instance).getTypeScriptThemePath) === null || _b === void 0 ? void 0 : _b.call(_a)
|
|
93
|
+
: (_d = (_c = pluginInstance.instance).getThemePath) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
94
|
+
if (!themePath) {
|
|
95
|
+
logger_1.default.warn(typescript
|
|
96
|
+
? logger_1.default.interpolate `name=${themeName} does not provide TypeScript theme code via ${'getTypeScriptThemePath()'}.`
|
|
97
|
+
: // This is... technically possible to happen, e.g. returning undefined
|
|
98
|
+
// from getThemePath. Plugins may intentionally or unintentionally
|
|
99
|
+
// disguise as themes?
|
|
100
|
+
logger_1.default.interpolate `name=${themeName} does not provide any theme code.`);
|
|
101
|
+
return process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
return themePath;
|
|
104
|
+
}
|
|
105
|
+
exports.getThemePath = getThemePath;
|
|
@@ -25,7 +25,7 @@ async function getExtraSourceCodeFilePaths() {
|
|
|
25
25
|
const themeCommonSourceDir = path_1.default.dirname(require.resolve('@docusaurus/theme-common/lib'));
|
|
26
26
|
return (0, translationsExtractor_1.globSourceCodeFilePaths)([themeCommonSourceDir]);
|
|
27
27
|
}
|
|
28
|
-
catch
|
|
28
|
+
catch {
|
|
29
29
|
return []; // User may not use a Docusaurus official theme? Quite unlikely...
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -4,7 +4,17 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
/// <reference types="node" />
|
|
8
|
+
import type { ImportedPluginModule, LoadContext, PluginModule, PluginConfig, PluginOptions, InitializedPlugin } from '@docusaurus/types';
|
|
9
|
+
export declare type NormalizedPluginConfig = {
|
|
10
|
+
plugin: PluginModule;
|
|
11
|
+
options: PluginOptions;
|
|
12
|
+
pluginModule?: {
|
|
13
|
+
path: string;
|
|
14
|
+
module: ImportedPluginModule;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare function normalizePluginConfigs(pluginConfigs: PluginConfig[], pluginRequire: NodeRequire): Promise<NormalizedPluginConfig[]>;
|
|
8
18
|
export default function initPlugins({ pluginConfigs, context, }: {
|
|
9
19
|
pluginConfigs: PluginConfig[];
|
|
10
20
|
context: LoadContext;
|