@docusaurus/core 0.0.0-4632 → 0.0.0-4635
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/docusaurus.mjs +19 -11
- 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/server/plugins/init.d.ts +11 -1
- package/lib/server/plugins/init.js +8 -3
- package/package.json +13 -11
- package/lib/commands/swizzle.d.ts +0 -9
- package/lib/commands/swizzle.js +0 -239
package/lib/commands/swizzle.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
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.getPluginNames = void 0;
|
|
10
|
-
const tslib_1 = require("tslib");
|
|
11
|
-
/* eslint-disable no-restricted-properties */
|
|
12
|
-
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
13
|
-
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
14
|
-
const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
|
|
15
|
-
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
16
|
-
const leven_1 = (0, tslib_1.__importDefault)(require("leven"));
|
|
17
|
-
const lodash_1 = require("lodash");
|
|
18
|
-
const utils_1 = require("@docusaurus/utils");
|
|
19
|
-
const server_1 = require("../server");
|
|
20
|
-
const init_1 = (0, tslib_1.__importDefault)(require("../server/plugins/init"));
|
|
21
|
-
const utils_validation_1 = require("@docusaurus/utils-validation");
|
|
22
|
-
function getPluginNames(plugins) {
|
|
23
|
-
return plugins
|
|
24
|
-
.filter((plugin) => typeof plugin === 'string' ||
|
|
25
|
-
(Array.isArray(plugin) && typeof plugin[0] === 'string'))
|
|
26
|
-
.map((plugin) => {
|
|
27
|
-
const pluginPath = Array.isArray(plugin) ? plugin[0] : plugin;
|
|
28
|
-
if (typeof pluginPath === 'string') {
|
|
29
|
-
let packagePath = path_1.default.dirname(pluginPath);
|
|
30
|
-
while (packagePath) {
|
|
31
|
-
if (fs_extra_1.default.existsSync(path_1.default.join(packagePath, 'package.json'))) {
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
packagePath = path_1.default.dirname(packagePath);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (packagePath === '.') {
|
|
39
|
-
return pluginPath;
|
|
40
|
-
}
|
|
41
|
-
return (0, import_fresh_1.default)(path_1.default.join(packagePath, 'package.json')).name;
|
|
42
|
-
}
|
|
43
|
-
return '';
|
|
44
|
-
})
|
|
45
|
-
.filter((plugin) => plugin !== '');
|
|
46
|
-
}
|
|
47
|
-
exports.getPluginNames = getPluginNames;
|
|
48
|
-
const formatComponentName = (componentName) => componentName
|
|
49
|
-
.replace(/[\\/]index\.(?:jsx?|tsx?)/, '')
|
|
50
|
-
.replace(/\.(?:jsx?|tsx?)/, '');
|
|
51
|
-
function readComponent(themePath) {
|
|
52
|
-
function walk(dir) {
|
|
53
|
-
let results = [];
|
|
54
|
-
const list = fs_extra_1.default.readdirSync(dir);
|
|
55
|
-
list.forEach((file) => {
|
|
56
|
-
const fullPath = path_1.default.join(dir, file);
|
|
57
|
-
const stat = fs_extra_1.default.statSync(fullPath);
|
|
58
|
-
if (stat && stat.isDirectory()) {
|
|
59
|
-
results = results.concat(walk(fullPath));
|
|
60
|
-
}
|
|
61
|
-
else if (!/\.css|\.d\.ts|\.d\.map/.test(fullPath)) {
|
|
62
|
-
results.push(fullPath);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
return results;
|
|
66
|
-
}
|
|
67
|
-
return walk(themePath).map((filePath) => formatComponentName(path_1.default.relative(themePath, filePath)));
|
|
68
|
-
}
|
|
69
|
-
// load components from theme based on configurations
|
|
70
|
-
function getComponentName(themePath, plugin, danger) {
|
|
71
|
-
var _a, _b;
|
|
72
|
-
// support both commonjs and ES style exports
|
|
73
|
-
const getSwizzleComponentList = (_b = (_a = plugin.default) === null || _a === void 0 ? void 0 : _a.getSwizzleComponentList) !== null && _b !== void 0 ? _b : plugin.getSwizzleComponentList;
|
|
74
|
-
if (getSwizzleComponentList) {
|
|
75
|
-
const allowedComponent = getSwizzleComponentList();
|
|
76
|
-
if (danger) {
|
|
77
|
-
return readComponent(themePath);
|
|
78
|
-
}
|
|
79
|
-
return allowedComponent;
|
|
80
|
-
}
|
|
81
|
-
return readComponent(themePath);
|
|
82
|
-
}
|
|
83
|
-
function themeComponents(themePath, plugin) {
|
|
84
|
-
const components = colorCode(themePath, plugin);
|
|
85
|
-
if (components.length === 0) {
|
|
86
|
-
return 'No component to swizzle.';
|
|
87
|
-
}
|
|
88
|
-
return `Theme components available for swizzle.
|
|
89
|
-
|
|
90
|
-
${logger_1.default.green(logger_1.default.bold('green =>'))} safe: lower breaking change risk
|
|
91
|
-
${logger_1.default.red(logger_1.default.bold('red =>'))} unsafe: higher breaking change risk
|
|
92
|
-
|
|
93
|
-
${components.join('\n')}
|
|
94
|
-
`;
|
|
95
|
-
}
|
|
96
|
-
function colorCode(themePath, plugin) {
|
|
97
|
-
var _a, _b;
|
|
98
|
-
// support both commonjs and ES style exports
|
|
99
|
-
const getSwizzleComponentList = (_b = (_a = plugin.default) === null || _a === void 0 ? void 0 : _a.getSwizzleComponentList) !== null && _b !== void 0 ? _b : plugin.getSwizzleComponentList;
|
|
100
|
-
const components = readComponent(themePath);
|
|
101
|
-
const allowedComponent = getSwizzleComponentList
|
|
102
|
-
? getSwizzleComponentList()
|
|
103
|
-
: [];
|
|
104
|
-
const [greenComponents, redComponents] = (0, lodash_1.partition)(components, (comp) => allowedComponent.includes(comp));
|
|
105
|
-
return [
|
|
106
|
-
...greenComponents.map((component) => `${logger_1.default.green(logger_1.default.bold('safe:'))} ${component}`),
|
|
107
|
-
...redComponents.map((component) => `${logger_1.default.red(logger_1.default.bold('unsafe:'))} ${component}`),
|
|
108
|
-
];
|
|
109
|
-
}
|
|
110
|
-
async function swizzle(siteDir, themeName, componentName, typescript, danger) {
|
|
111
|
-
var _a, _b, _c, _d, _e;
|
|
112
|
-
const context = await (0, server_1.loadContext)(siteDir);
|
|
113
|
-
const pluginConfigs = await (0, server_1.loadPluginConfigs)(context);
|
|
114
|
-
const pluginNames = getPluginNames(pluginConfigs);
|
|
115
|
-
const plugins = await (0, init_1.default)({
|
|
116
|
-
pluginConfigs,
|
|
117
|
-
context,
|
|
118
|
-
});
|
|
119
|
-
const themeNames = pluginNames.filter((_, index) => typescript
|
|
120
|
-
? plugins[index].getTypeScriptThemePath
|
|
121
|
-
: plugins[index].getThemePath);
|
|
122
|
-
if (!themeName) {
|
|
123
|
-
logger_1.default.info `Themes available for swizzle: name=${themeNames}`;
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
let pluginModule;
|
|
127
|
-
try {
|
|
128
|
-
pluginModule = (0, import_fresh_1.default)(themeName);
|
|
129
|
-
}
|
|
130
|
-
catch {
|
|
131
|
-
let suggestion;
|
|
132
|
-
themeNames.forEach((name) => {
|
|
133
|
-
if ((0, leven_1.default)(name, themeName) < 4) {
|
|
134
|
-
suggestion = name;
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
logger_1.default.error `Theme name=${themeName} not found. ${suggestion
|
|
138
|
-
? logger_1.default.interpolate `Did you mean name=${suggestion}?`
|
|
139
|
-
: logger_1.default.interpolate `Themes available for swizzle: ${themeNames}`}`;
|
|
140
|
-
process.exit(1);
|
|
141
|
-
}
|
|
142
|
-
let pluginOptions = {};
|
|
143
|
-
const resolvedThemeName = require.resolve(themeName);
|
|
144
|
-
// find the plugin from list of plugin and get options if specified
|
|
145
|
-
pluginConfigs.forEach((pluginConfig) => {
|
|
146
|
-
// plugin can be a [string], [string,object] or string.
|
|
147
|
-
if (Array.isArray(pluginConfig) && typeof pluginConfig[0] === 'string') {
|
|
148
|
-
if (require.resolve(pluginConfig[0]) === resolvedThemeName) {
|
|
149
|
-
if (pluginConfig.length === 2) {
|
|
150
|
-
const [, options] = pluginConfig;
|
|
151
|
-
pluginOptions = options;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
// support both commonjs and ES style exports
|
|
157
|
-
const validateOptions = (_b = (_a = pluginModule.default) === null || _a === void 0 ? void 0 : _a.validateOptions) !== null && _b !== void 0 ? _b : pluginModule.validateOptions;
|
|
158
|
-
if (validateOptions) {
|
|
159
|
-
pluginOptions = validateOptions({
|
|
160
|
-
validate: utils_validation_1.normalizePluginOptions,
|
|
161
|
-
options: pluginOptions,
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
// support both commonjs and ES style exports
|
|
165
|
-
const plugin = (_c = pluginModule.default) !== null && _c !== void 0 ? _c : pluginModule;
|
|
166
|
-
const pluginInstance = await plugin(context, pluginOptions);
|
|
167
|
-
const themePath = typescript
|
|
168
|
-
? (_d = pluginInstance.getTypeScriptThemePath) === null || _d === void 0 ? void 0 : _d.call(pluginInstance)
|
|
169
|
-
: (_e = pluginInstance.getThemePath) === null || _e === void 0 ? void 0 : _e.call(pluginInstance);
|
|
170
|
-
if (!themePath) {
|
|
171
|
-
logger_1.default.warn(typescript
|
|
172
|
-
? logger_1.default.interpolate `name=${themeName} does not provide TypeScript theme code via ${'getTypeScriptThemePath()'}.`
|
|
173
|
-
: logger_1.default.interpolate `name=${themeName} does not provide any theme code.`);
|
|
174
|
-
process.exit(1);
|
|
175
|
-
}
|
|
176
|
-
if (!componentName) {
|
|
177
|
-
logger_1.default.info(themeComponents(themePath, pluginModule));
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const components = getComponentName(themePath, pluginModule, Boolean(danger));
|
|
181
|
-
const formattedComponentName = formatComponentName(componentName);
|
|
182
|
-
const isComponentExists = components.find((component) => component === formattedComponentName);
|
|
183
|
-
let mostSuitableComponent = componentName;
|
|
184
|
-
if (!isComponentExists) {
|
|
185
|
-
let mostSuitableMatch = componentName;
|
|
186
|
-
let score = formattedComponentName.length;
|
|
187
|
-
components.forEach((component) => {
|
|
188
|
-
if (component.toLowerCase() === formattedComponentName.toLowerCase()) {
|
|
189
|
-
// may be components with same lowercase key, try to match closest
|
|
190
|
-
// component
|
|
191
|
-
const currentScore = (0, leven_1.default)(formattedComponentName, component);
|
|
192
|
-
if (currentScore < score) {
|
|
193
|
-
score = currentScore;
|
|
194
|
-
mostSuitableMatch = component;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
if (mostSuitableMatch !== componentName) {
|
|
199
|
-
mostSuitableComponent = mostSuitableMatch;
|
|
200
|
-
logger_1.default.error `Component name=${componentName} doesn't exist.`;
|
|
201
|
-
logger_1.default.info `name=${mostSuitableComponent} is swizzled instead of name=${componentName}.`;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
let fromPath = path_1.default.join(themePath, mostSuitableComponent);
|
|
205
|
-
let toPath = path_1.default.resolve(siteDir, utils_1.THEME_PATH, mostSuitableComponent);
|
|
206
|
-
// Handle single TypeScript/JavaScript file only.
|
|
207
|
-
// E.g: if <fromPath> does not exist, we try to swizzle
|
|
208
|
-
// <fromPath>.(ts|tsx|js) instead
|
|
209
|
-
if (!fs_extra_1.default.existsSync(fromPath)) {
|
|
210
|
-
if (fs_extra_1.default.existsSync(`${fromPath}.ts`)) {
|
|
211
|
-
[fromPath, toPath] = [`${fromPath}.ts`, `${toPath}.ts`];
|
|
212
|
-
}
|
|
213
|
-
else if (fs_extra_1.default.existsSync(`${fromPath}.tsx`)) {
|
|
214
|
-
[fromPath, toPath] = [`${fromPath}.tsx`, `${toPath}.tsx`];
|
|
215
|
-
}
|
|
216
|
-
else if (fs_extra_1.default.existsSync(`${fromPath}.js`)) {
|
|
217
|
-
[fromPath, toPath] = [`${fromPath}.js`, `${toPath}.js`];
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
let suggestion;
|
|
221
|
-
components.forEach((name) => {
|
|
222
|
-
if ((0, leven_1.default)(name, mostSuitableComponent) < 3) {
|
|
223
|
-
suggestion = name;
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
logger_1.default.error `Component name=${mostSuitableComponent} not found. ${suggestion
|
|
227
|
-
? logger_1.default.interpolate `Did you mean name=${suggestion} ?`
|
|
228
|
-
: themeComponents(themePath, pluginModule)}`;
|
|
229
|
-
process.exit(1);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
if (!components.includes(mostSuitableComponent) && !danger) {
|
|
233
|
-
logger_1.default.error `name=${mostSuitableComponent} is an internal component and has a higher breaking change probability. If you want to swizzle it, use the code=${'--danger'} flag.`;
|
|
234
|
-
process.exit(1);
|
|
235
|
-
}
|
|
236
|
-
await fs_extra_1.default.copy(fromPath, toPath);
|
|
237
|
-
logger_1.default.success `Copied code=${mostSuitableComponent ? `${themeName} ${mostSuitableComponent}` : themeName} to path=${path_1.default.relative(process.cwd(), toPath)}.`;
|
|
238
|
-
}
|
|
239
|
-
exports.default = swizzle;
|