@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
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.normalizePluginConfigs = void 0;
|
|
9
10
|
const tslib_1 = require("tslib");
|
|
10
11
|
const module_1 = require("module");
|
|
11
12
|
const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
|
|
@@ -65,6 +66,10 @@ async function normalizePluginConfig(pluginConfig, pluginRequire) {
|
|
|
65
66
|
}
|
|
66
67
|
throw new Error(`Unexpected: can't load plugin for following plugin config.\n${JSON.stringify(pluginConfig)}`);
|
|
67
68
|
}
|
|
69
|
+
async function normalizePluginConfigs(pluginConfigs, pluginRequire) {
|
|
70
|
+
return Promise.all(pluginConfigs.map((pluginConfig) => normalizePluginConfig(pluginConfig, pluginRequire)));
|
|
71
|
+
}
|
|
72
|
+
exports.normalizePluginConfigs = normalizePluginConfigs;
|
|
68
73
|
function getOptionValidationFunction(normalizedPluginConfig) {
|
|
69
74
|
var _a, _b, _c, _d;
|
|
70
75
|
if (normalizedPluginConfig.pluginModule) {
|
|
@@ -85,6 +90,7 @@ async function initPlugins({ pluginConfigs, context, }) {
|
|
|
85
90
|
// We need to resolve plugins from the perspective of the siteDir, since the
|
|
86
91
|
// siteDir's package.json declares the dependency on these plugins.
|
|
87
92
|
const pluginRequire = (0, module_1.createRequire)(context.siteConfigPath);
|
|
93
|
+
const pluginConfigsNormalized = await normalizePluginConfigs(pluginConfigs, pluginRequire);
|
|
88
94
|
async function doGetPluginVersion(normalizedPluginConfig) {
|
|
89
95
|
var _a, _b;
|
|
90
96
|
// get plugin version
|
|
@@ -120,8 +126,7 @@ async function initPlugins({ pluginConfigs, context, }) {
|
|
|
120
126
|
id: (_a = normalizedPluginConfig.options.id) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PLUGIN_ID,
|
|
121
127
|
};
|
|
122
128
|
}
|
|
123
|
-
async function initializePlugin(
|
|
124
|
-
const normalizedPluginConfig = await normalizePluginConfig(pluginConfig, pluginRequire);
|
|
129
|
+
async function initializePlugin(normalizedPluginConfig) {
|
|
125
130
|
const pluginVersion = await doGetPluginVersion(normalizedPluginConfig);
|
|
126
131
|
const pluginOptions = doValidatePluginOptions(normalizedPluginConfig);
|
|
127
132
|
// Side-effect: merge the normalized theme config in the original one
|
|
@@ -136,7 +141,7 @@ async function initPlugins({ pluginConfigs, context, }) {
|
|
|
136
141
|
version: pluginVersion,
|
|
137
142
|
};
|
|
138
143
|
}
|
|
139
|
-
const plugins = (await Promise.all(
|
|
144
|
+
const plugins = (await Promise.all(pluginConfigsNormalized.map((pluginConfig) => {
|
|
140
145
|
if (!pluginConfig) {
|
|
141
146
|
return null;
|
|
142
147
|
}
|
|
@@ -35,8 +35,9 @@ async function readTranslationFileContent(filePath) {
|
|
|
35
35
|
ensureTranslationFileContent(content);
|
|
36
36
|
return content;
|
|
37
37
|
}
|
|
38
|
-
catch (
|
|
39
|
-
|
|
38
|
+
catch (err) {
|
|
39
|
+
logger_1.default.error `Invalid translation file at path=${filePath}.`;
|
|
40
|
+
throw err;
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
return undefined;
|
|
@@ -103,11 +103,9 @@ async function extractSourceCodeFileTranslations(sourceCodeFilePath, babelOption
|
|
|
103
103
|
const translations = await extractSourceCodeAstTranslations(ast, sourceCodeFilePath);
|
|
104
104
|
return translations;
|
|
105
105
|
}
|
|
106
|
-
catch (
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
throw e;
|
|
106
|
+
catch (err) {
|
|
107
|
+
logger_1.default.error `Error while attempting to extract Docusaurus translations from source code file at path=${sourceCodeFilePath}.`;
|
|
108
|
+
throw err;
|
|
111
109
|
}
|
|
112
110
|
}
|
|
113
111
|
exports.extractSourceCodeFileTranslations = extractSourceCodeFileTranslations;
|
|
@@ -6,5 +6,4 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { DocusaurusPluginVersionInformation } from '@docusaurus/types';
|
|
8
8
|
export declare function getPackageJsonVersion(packageJsonPath: string): Promise<string | undefined>;
|
|
9
|
-
export declare function getPackageJsonName(packageJsonPath: string): Promise<string | undefined>;
|
|
10
9
|
export declare function getPluginVersion(pluginPath: string, siteDir: string): Promise<DocusaurusPluginVersionInformation>;
|
|
@@ -6,15 +6,14 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.getPluginVersion = exports.
|
|
9
|
+
exports.getPluginVersion = exports.getPackageJsonVersion = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
12
12
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
13
13
|
async function getPackageJsonVersion(packageJsonPath) {
|
|
14
14
|
if (await fs_extra_1.default.pathExists(packageJsonPath)) {
|
|
15
15
|
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-dynamic-require, global-require
|
|
16
|
-
|
|
17
|
-
return typeof version === 'string' ? version : undefined;
|
|
16
|
+
return require(packageJsonPath).version;
|
|
18
17
|
}
|
|
19
18
|
return undefined;
|
|
20
19
|
}
|
|
@@ -22,12 +21,10 @@ exports.getPackageJsonVersion = getPackageJsonVersion;
|
|
|
22
21
|
async function getPackageJsonName(packageJsonPath) {
|
|
23
22
|
if (await fs_extra_1.default.pathExists(packageJsonPath)) {
|
|
24
23
|
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-dynamic-require, global-require
|
|
25
|
-
|
|
26
|
-
return typeof name === 'string' ? name : undefined;
|
|
24
|
+
return require(packageJsonPath).name;
|
|
27
25
|
}
|
|
28
26
|
return undefined;
|
|
29
27
|
}
|
|
30
|
-
exports.getPackageJsonName = getPackageJsonName;
|
|
31
28
|
async function getPluginVersion(pluginPath, siteDir) {
|
|
32
29
|
let potentialPluginPackageJsonDirectory = path_1.default.dirname(pluginPath);
|
|
33
30
|
while (potentialPluginPackageJsonDirectory !== '/') {
|
|
@@ -134,13 +134,13 @@ class CleanWebpackPlugin {
|
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
|
-
catch (
|
|
138
|
-
const needsForce = /Cannot delete files\/folders outside the current working directory\./.test(
|
|
137
|
+
catch (err) {
|
|
138
|
+
const needsForce = /Cannot delete files\/folders outside the current working directory\./.test(err.message);
|
|
139
139
|
if (needsForce) {
|
|
140
140
|
const message = 'clean-webpack-plugin: Cannot delete files/folders outside the current working directory. Can be overridden with the "dangerouslyAllowCleanPatternsOutsideProject" option.';
|
|
141
141
|
throw new Error(message);
|
|
142
142
|
}
|
|
143
|
-
throw
|
|
143
|
+
throw err;
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/core",
|
|
3
3
|
"description": "Easy to Maintain Open Source Documentation Websites",
|
|
4
|
-
"version": "0.0.0-
|
|
4
|
+
"version": "0.0.0-4633",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -41,13 +41,13 @@
|
|
|
41
41
|
"@babel/runtime": "^7.17.2",
|
|
42
42
|
"@babel/runtime-corejs3": "^7.17.2",
|
|
43
43
|
"@babel/traverse": "^7.17.3",
|
|
44
|
-
"@docusaurus/cssnano-preset": "0.0.0-
|
|
45
|
-
"@docusaurus/logger": "0.0.0-
|
|
46
|
-
"@docusaurus/mdx-loader": "0.0.0-
|
|
44
|
+
"@docusaurus/cssnano-preset": "0.0.0-4633",
|
|
45
|
+
"@docusaurus/logger": "0.0.0-4633",
|
|
46
|
+
"@docusaurus/mdx-loader": "0.0.0-4633",
|
|
47
47
|
"@docusaurus/react-loadable": "5.5.2",
|
|
48
|
-
"@docusaurus/utils": "0.0.0-
|
|
49
|
-
"@docusaurus/utils-common": "0.0.0-
|
|
50
|
-
"@docusaurus/utils-validation": "0.0.0-
|
|
48
|
+
"@docusaurus/utils": "0.0.0-4633",
|
|
49
|
+
"@docusaurus/utils-common": "0.0.0-4633",
|
|
50
|
+
"@docusaurus/utils-validation": "0.0.0-4633",
|
|
51
51
|
"@slorber/static-site-generator-webpack-plugin": "^4.0.1",
|
|
52
52
|
"@svgr/webpack": "^6.2.1",
|
|
53
53
|
"autoprefixer": "^10.4.2",
|
|
@@ -56,6 +56,7 @@
|
|
|
56
56
|
"boxen": "^6.2.1",
|
|
57
57
|
"chokidar": "^3.5.3",
|
|
58
58
|
"clean-css": "^5.2.4",
|
|
59
|
+
"cli-table3": "^0.6.1",
|
|
59
60
|
"combine-promises": "^1.1.0",
|
|
60
61
|
"commander": "^5.1.0",
|
|
61
62
|
"copy-webpack-plugin": "^10.2.4",
|
|
@@ -105,8 +106,8 @@
|
|
|
105
106
|
"webpackbar": "^5.0.2"
|
|
106
107
|
},
|
|
107
108
|
"devDependencies": {
|
|
108
|
-
"@docusaurus/module-type-aliases": "0.0.0-
|
|
109
|
-
"@docusaurus/types": "0.0.0-
|
|
109
|
+
"@docusaurus/module-type-aliases": "0.0.0-4633",
|
|
110
|
+
"@docusaurus/types": "0.0.0-4633",
|
|
110
111
|
"@types/detect-port": "^1.3.2",
|
|
111
112
|
"@types/nprogress": "^0.2.0",
|
|
112
113
|
"@types/react-dom": "^17.0.11",
|
|
@@ -117,7 +118,8 @@
|
|
|
117
118
|
"@types/wait-on": "^5.3.1",
|
|
118
119
|
"@types/webpack-bundle-analyzer": "^4.4.1",
|
|
119
120
|
"react-test-renderer": "^17.0.2",
|
|
120
|
-
"tmp-promise": "^3.0.3"
|
|
121
|
+
"tmp-promise": "^3.0.3",
|
|
122
|
+
"tree-node-cli": "^1.5.2"
|
|
121
123
|
},
|
|
122
124
|
"peerDependencies": {
|
|
123
125
|
"react": "^16.8.4 || ^17.0.0",
|
|
@@ -126,5 +128,5 @@
|
|
|
126
128
|
"engines": {
|
|
127
129
|
"node": ">=14"
|
|
128
130
|
},
|
|
129
|
-
"gitHead": "
|
|
131
|
+
"gitHead": "565aa1336079094585ee040af351f87a51df711b"
|
|
130
132
|
}
|
|
@@ -1,9 +0,0 @@
|
|
|
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 { PluginConfig } from '@docusaurus/types';
|
|
8
|
-
export declare function getPluginNames(plugins: PluginConfig[]): string[];
|
|
9
|
-
export default function swizzle(siteDir: string, themeName?: string, componentName?: string, typescript?: boolean, danger?: boolean): Promise<void>;
|
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;
|