@docusaurus/core 2.0.0-beta.15 → 2.0.0-beta.16
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 +136 -0
- package/bin/{docusaurus.js → docusaurus.mjs} +62 -40
- package/lib/babel/preset.d.ts +1 -2
- package/lib/babel/preset.js +5 -4
- package/lib/choosePort.js +22 -30
- package/lib/client/App.d.ts +1 -2
- package/lib/client/App.js +13 -8
- package/lib/client/LinksCollector.js +1 -1
- package/lib/client/PendingNavigation.d.ts +4 -4
- package/lib/client/PendingNavigation.js +4 -6
- package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.d.ts +8 -0
- package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +15 -10
- package/lib/client/client-lifecycles-dispatcher.d.ts +2 -5
- package/lib/client/client-lifecycles-dispatcher.js +5 -7
- package/lib/client/clientEntry.js +11 -5
- package/lib/client/docusaurus.js +6 -4
- package/lib/client/exports/BrowserOnly.d.ts +1 -2
- package/lib/client/exports/BrowserOnly.js +2 -3
- package/lib/client/exports/ComponentCreator.d.ts +1 -2
- package/lib/client/exports/ComponentCreator.js +7 -6
- package/lib/client/exports/ErrorBoundary.d.ts +2 -2
- package/lib/client/exports/ErrorBoundary.js +1 -2
- package/lib/client/exports/Head.d.ts +2 -3
- package/lib/client/exports/Head.js +3 -4
- package/lib/client/exports/Interpolate.js +9 -12
- package/lib/client/exports/Link.d.ts +11 -5
- package/lib/client/exports/Link.js +13 -7
- package/lib/client/exports/Translate.js +2 -1
- package/lib/client/exports/browserContext.js +3 -2
- package/lib/client/exports/docusaurusContext.js +1 -1
- package/lib/client/exports/isInternalUrl.js +1 -1
- package/lib/client/exports/renderRoutes.d.ts +1 -2
- package/lib/client/exports/renderRoutes.js +1 -2
- package/lib/client/exports/router.d.ts +1 -1
- package/lib/client/exports/router.js +1 -1
- package/lib/client/exports/useDocusaurusContext.d.ts +1 -2
- package/lib/client/exports/useDocusaurusContext.js +1 -2
- package/lib/client/flat.d.ts +1 -2
- package/lib/client/flat.js +1 -2
- package/lib/client/normalizeLocation.d.ts +2 -3
- package/lib/client/normalizeLocation.js +1 -2
- package/lib/client/prefetch.d.ts +1 -2
- package/lib/client/prefetch.js +1 -2
- package/lib/client/preload.d.ts +2 -1
- package/lib/client/preload.js +2 -1
- package/lib/client/serverEntry.js +23 -19
- package/lib/client/theme-fallback/Error/index.d.ts +10 -0
- package/lib/client/theme-fallback/Error/index.js +21 -29
- package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
- package/lib/client/theme-fallback/Layout/index.js +10 -19
- package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
- package/lib/client/theme-fallback/Loading/index.js +46 -114
- package/lib/{server/versions/__tests/index.test.d.ts → client/theme-fallback/NotFound/index.d.ts} +2 -1
- package/lib/client/theme-fallback/NotFound/index.js +9 -16
- package/lib/client/theme-fallback/Root/index.d.ts +10 -0
- package/lib/client/theme-fallback/Root/index.js +2 -5
- package/lib/commands/build.js +33 -34
- package/lib/commands/clear.js +23 -11
- package/lib/commands/deploy.js +12 -11
- package/lib/commands/external.d.ts +2 -2
- package/lib/commands/external.js +1 -1
- package/lib/commands/serve.js +3 -2
- package/lib/commands/start.js +4 -4
- 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/writeHeadingIds.d.ts +1 -1
- package/lib/commands/writeHeadingIds.js +13 -14
- package/lib/commands/writeTranslations.js +10 -7
- package/lib/index.d.ts +10 -9
- package/lib/index.js +20 -19
- package/lib/server/brokenLinks.js +30 -20
- package/lib/server/config.js +1 -1
- package/lib/server/configValidation.d.ts +1 -1
- package/lib/server/configValidation.js +32 -23
- package/lib/server/duplicateRoutes.js +2 -4
- package/lib/server/html-tags/htmlTags.js +1 -2
- package/lib/server/i18n.d.ts +0 -1
- package/lib/server/i18n.js +16 -26
- package/lib/server/index.d.ts +1 -1
- package/lib/server/index.js +17 -15
- package/lib/server/loadSetup.d.ts +1 -2
- package/lib/server/loadSetup.js +2 -2
- package/lib/server/moduleShorthand.js +1 -1
- package/lib/server/plugins/index.js +9 -9
- package/lib/server/plugins/init.d.ts +11 -1
- package/lib/server/plugins/init.js +23 -28
- package/lib/server/plugins/pluginIds.js +4 -3
- package/lib/server/presets/index.d.ts +2 -2
- package/lib/server/presets/index.js +3 -3
- package/lib/server/routes.js +13 -7
- package/lib/server/themes/alias.d.ts +1 -1
- package/lib/server/themes/alias.js +5 -6
- package/lib/server/themes/index.d.ts +2 -2
- package/lib/server/themes/index.js +10 -9
- package/lib/server/translations/translations.js +10 -11
- package/lib/server/translations/translationsExtractor.js +20 -19
- package/lib/server/versions/index.d.ts +2 -3
- package/lib/server/versions/index.js +22 -21
- package/lib/webpack/base.d.ts +2 -2
- package/lib/webpack/base.js +30 -22
- package/lib/webpack/client.d.ts +1 -1
- package/lib/webpack/client.js +7 -4
- package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +12 -2
- package/lib/webpack/plugins/ChunkAssetPlugin.js +17 -10
- package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +5 -5
- package/lib/webpack/plugins/CleanWebpackPlugin.js +5 -4
- package/lib/webpack/server.d.ts +1 -1
- package/lib/webpack/server.js +6 -5
- package/lib/webpack/utils.d.ts +3 -3
- package/lib/webpack/utils.js +17 -37
- package/package.json +56 -56
- package/bin/beforeCli.js +0 -124
- package/lib/commands/swizzle.d.ts +0 -9
- package/lib/commands/swizzle.js +0 -236
- package/lib/server/versions/__tests/index.test.js +0 -26
package/lib/commands/serve.js
CHANGED
|
@@ -44,13 +44,14 @@ async function serve(siteDir, cliOptions) {
|
|
|
44
44
|
res.end();
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
|
-
// Remove baseUrl before calling serveHandler
|
|
48
|
-
//
|
|
47
|
+
// Remove baseUrl before calling serveHandler, because /baseUrl/ should
|
|
48
|
+
// serve /build/index.html, not /build/baseUrl/index.html (does not exist)
|
|
49
49
|
req.url = (_b = req.url) === null || _b === void 0 ? void 0 : _b.replace(baseUrl, '/');
|
|
50
50
|
(0, serve_handler_1.default)(req, res, {
|
|
51
51
|
cleanUrls: true,
|
|
52
52
|
public: dir,
|
|
53
53
|
trailingSlash,
|
|
54
|
+
directoryListing: false,
|
|
54
55
|
});
|
|
55
56
|
});
|
|
56
57
|
logger_1.default.success `Serving path=${cliOptions.dir} directory at path=${servingUrl + baseUrl}.`;
|
package/lib/commands/start.js
CHANGED
|
@@ -12,7 +12,7 @@ const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
|
12
12
|
const chokidar_1 = (0, tslib_1.__importDefault)(require("chokidar"));
|
|
13
13
|
const html_webpack_plugin_1 = (0, tslib_1.__importDefault)(require("html-webpack-plugin"));
|
|
14
14
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
15
|
-
const lodash_1 = require("lodash");
|
|
15
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
16
16
|
const openBrowser_1 = (0, tslib_1.__importDefault)(require("react-dev-utils/openBrowser"));
|
|
17
17
|
const WebpackDevServerUtils_1 = require("react-dev-utils/WebpackDevServerUtils");
|
|
18
18
|
const evalSourceMapMiddleware_1 = (0, tslib_1.__importDefault)(require("react-dev-utils/evalSourceMapMiddleware"));
|
|
@@ -48,7 +48,7 @@ async function start(siteDir, cliOptions) {
|
|
|
48
48
|
const openUrl = (0, utils_1.normalizeUrl)([urls.localUrlForBrowser, baseUrl]);
|
|
49
49
|
logger_1.default.success `Docusaurus website is running at path=${openUrl}.`;
|
|
50
50
|
// Reload files processing.
|
|
51
|
-
const reload =
|
|
51
|
+
const reload = lodash_1.default.debounce(() => {
|
|
52
52
|
loadSite()
|
|
53
53
|
.then(({ baseUrl: newBaseUrl }) => {
|
|
54
54
|
const newOpenUrl = (0, utils_1.normalizeUrl)([urls.localUrlForBrowser, newBaseUrl]);
|
|
@@ -86,14 +86,14 @@ async function start(siteDir, cliOptions) {
|
|
|
86
86
|
? cliOptions.poll
|
|
87
87
|
: undefined,
|
|
88
88
|
};
|
|
89
|
-
const httpsConfig = (0, utils_2.getHttpsConfig)();
|
|
89
|
+
const httpsConfig = await (0, utils_2.getHttpsConfig)();
|
|
90
90
|
const fsWatcher = chokidar_1.default.watch(pathsToWatch, {
|
|
91
91
|
cwd: siteDir,
|
|
92
92
|
ignoreInitial: true,
|
|
93
93
|
...{ pollingOptions },
|
|
94
94
|
});
|
|
95
95
|
['add', 'change', 'unlink', 'addDir', 'unlinkDir'].forEach((event) => fsWatcher.on(event, reload));
|
|
96
|
-
let config = (0, webpack_merge_1.default)((0, client_1.default)(props), {
|
|
96
|
+
let config = (0, webpack_merge_1.default)(await (0, client_1.default)(props), {
|
|
97
97
|
infrastructureLogging: {
|
|
98
98
|
// Reduce log verbosity, see https://github.com/facebook/docusaurus/pull/5420#issuecomment-906613105
|
|
99
99
|
level: 'warn',
|
|
@@ -0,0 +1,23 @@
|
|
|
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 { SwizzleAction, SwizzleComponentConfig } from '@docusaurus/types';
|
|
8
|
+
import type { SwizzleOptions } from './common';
|
|
9
|
+
export declare const SwizzleActions: SwizzleAction[];
|
|
10
|
+
export declare function getAction(componentConfig: SwizzleComponentConfig, options: Pick<SwizzleOptions, 'wrap' | 'eject'>): Promise<SwizzleAction>;
|
|
11
|
+
export declare type ActionParams = {
|
|
12
|
+
siteDir: string;
|
|
13
|
+
themePath: string;
|
|
14
|
+
componentName: string;
|
|
15
|
+
};
|
|
16
|
+
export declare type ActionResult = {
|
|
17
|
+
createdFiles: string[];
|
|
18
|
+
};
|
|
19
|
+
export declare function eject({ siteDir, themePath, componentName, }: ActionParams): Promise<ActionResult>;
|
|
20
|
+
export declare function wrap({ siteDir, themePath, componentName: themeComponentName, typescript, importType, }: ActionParams & {
|
|
21
|
+
typescript: boolean;
|
|
22
|
+
importType?: 'original' | 'init';
|
|
23
|
+
}): Promise<ActionResult>;
|
|
@@ -0,0 +1,102 @@
|
|
|
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.wrap = exports.eject = exports.getAction = exports.SwizzleActions = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
12
|
+
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
13
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
14
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
15
|
+
const utils_1 = require("@docusaurus/utils");
|
|
16
|
+
const prompts_1 = require("./prompts");
|
|
17
|
+
exports.SwizzleActions = ['wrap', 'eject'];
|
|
18
|
+
async function getAction(componentConfig, options) {
|
|
19
|
+
if (options.wrap) {
|
|
20
|
+
return 'wrap';
|
|
21
|
+
}
|
|
22
|
+
if (options.eject) {
|
|
23
|
+
return 'eject';
|
|
24
|
+
}
|
|
25
|
+
return (0, prompts_1.askSwizzleAction)(componentConfig);
|
|
26
|
+
}
|
|
27
|
+
exports.getAction = getAction;
|
|
28
|
+
async function isDir(dirPath) {
|
|
29
|
+
return ((await fs_extra_1.default.pathExists(dirPath)) && (await fs_extra_1.default.stat(dirPath)).isDirectory());
|
|
30
|
+
}
|
|
31
|
+
async function eject({ siteDir, themePath, componentName, }) {
|
|
32
|
+
const fromPath = path_1.default.join(themePath, componentName);
|
|
33
|
+
const isDirectory = await isDir(fromPath);
|
|
34
|
+
const globPattern = isDirectory
|
|
35
|
+
? // do we really want to copy all components?
|
|
36
|
+
path_1.default.join(fromPath, '*')
|
|
37
|
+
: `${fromPath}.*`;
|
|
38
|
+
const globPatternPosix = (0, utils_1.posixPath)(globPattern);
|
|
39
|
+
const filesToCopy = await (0, utils_1.Globby)(globPatternPosix, {
|
|
40
|
+
ignore: ['**/*.{story,stories,test,tests}.{js,jsx,ts,tsx}'],
|
|
41
|
+
});
|
|
42
|
+
if (filesToCopy.length === 0) {
|
|
43
|
+
// This should never happen
|
|
44
|
+
throw new Error(logger_1.default.interpolate `No files to copy from path=${fromPath} with glob code=${globPatternPosix}`);
|
|
45
|
+
}
|
|
46
|
+
const toPath = isDirectory
|
|
47
|
+
? path_1.default.join(siteDir, utils_1.THEME_PATH, componentName)
|
|
48
|
+
: path_1.default.join(siteDir, utils_1.THEME_PATH);
|
|
49
|
+
await fs_extra_1.default.ensureDir(toPath);
|
|
50
|
+
const createdFiles = await Promise.all(filesToCopy.map(async (sourceFile) => {
|
|
51
|
+
const fileName = path_1.default.basename(sourceFile);
|
|
52
|
+
const targetFile = path_1.default.join(toPath, fileName);
|
|
53
|
+
try {
|
|
54
|
+
await fs_extra_1.default.copy(sourceFile, targetFile, { overwrite: true });
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
throw new Error(logger_1.default.interpolate `Could not copy file from ${sourceFile} to ${targetFile}`);
|
|
58
|
+
}
|
|
59
|
+
return targetFile;
|
|
60
|
+
}));
|
|
61
|
+
return { createdFiles };
|
|
62
|
+
}
|
|
63
|
+
exports.eject = eject;
|
|
64
|
+
async function wrap({ siteDir, themePath, componentName: themeComponentName, typescript, importType = 'original', }) {
|
|
65
|
+
const isDirectory = await isDir(path_1.default.join(themePath, themeComponentName));
|
|
66
|
+
// Top/Parent/ComponentName => ComponentName
|
|
67
|
+
const componentName = lodash_1.default.last(themeComponentName.split('/'));
|
|
68
|
+
const wrapperComponentName = `${componentName}Wrapper`;
|
|
69
|
+
const wrapperFileName = `${themeComponentName}${isDirectory ? '/index' : ''}${typescript ? '.tsx' : '.js'}`;
|
|
70
|
+
await fs_extra_1.default.ensureDir(path_1.default.resolve(siteDir, utils_1.THEME_PATH));
|
|
71
|
+
const toPath = path_1.default.resolve(siteDir, utils_1.THEME_PATH, wrapperFileName);
|
|
72
|
+
const content = typescript
|
|
73
|
+
? `import React, {ComponentProps} from 'react';
|
|
74
|
+
import type ${componentName}Type from '@theme/${themeComponentName}';
|
|
75
|
+
import ${componentName} from '@theme-${importType}/${themeComponentName}';
|
|
76
|
+
|
|
77
|
+
type Props = ComponentProps<typeof ${componentName}Type>
|
|
78
|
+
|
|
79
|
+
export default function ${wrapperComponentName}(props: Props): JSX.Element {
|
|
80
|
+
return (
|
|
81
|
+
<>
|
|
82
|
+
<${componentName} {...props} />
|
|
83
|
+
</>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
`
|
|
87
|
+
: `import React from 'react';
|
|
88
|
+
import ${componentName} from '@theme-${importType}/${themeComponentName}';
|
|
89
|
+
|
|
90
|
+
export default function ${wrapperComponentName}(props) {
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
<${componentName} {...props} />
|
|
94
|
+
</>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
`;
|
|
98
|
+
await fs_extra_1.default.ensureDir(path_1.default.dirname(toPath));
|
|
99
|
+
await fs_extra_1.default.writeFile(toPath, content);
|
|
100
|
+
return { createdFiles: [toPath] };
|
|
101
|
+
}
|
|
102
|
+
exports.wrap = wrap;
|
|
@@ -0,0 +1,33 @@
|
|
|
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 { InitializedPlugin, SwizzleAction, SwizzleActionStatus } from '@docusaurus/types';
|
|
8
|
+
import type { NormalizedPluginConfig } from '../../server/plugins/init';
|
|
9
|
+
export declare const SwizzleActions: SwizzleAction[];
|
|
10
|
+
export declare const SwizzleActionsStatuses: SwizzleActionStatus[];
|
|
11
|
+
export declare const PartiallySafeHint: string;
|
|
12
|
+
export declare function actionStatusLabel(status: SwizzleActionStatus): string;
|
|
13
|
+
export declare function actionStatusColor(status: SwizzleActionStatus, str: string): string;
|
|
14
|
+
export declare function actionStatusSuffix(status: SwizzleActionStatus, options?: {
|
|
15
|
+
partiallySafe?: boolean;
|
|
16
|
+
}): string;
|
|
17
|
+
export declare type SwizzlePlugin = {
|
|
18
|
+
instance: InitializedPlugin;
|
|
19
|
+
plugin: NormalizedPluginConfig;
|
|
20
|
+
};
|
|
21
|
+
export declare type SwizzleContext = {
|
|
22
|
+
plugins: SwizzlePlugin[];
|
|
23
|
+
};
|
|
24
|
+
export declare type SwizzleOptions = {
|
|
25
|
+
typescript: boolean;
|
|
26
|
+
danger: boolean;
|
|
27
|
+
list: boolean;
|
|
28
|
+
wrap: boolean;
|
|
29
|
+
eject: boolean;
|
|
30
|
+
};
|
|
31
|
+
export declare function normalizeOptions(options: Partial<SwizzleOptions>): SwizzleOptions;
|
|
32
|
+
export declare function findStringIgnoringCase(str: string, values: string[]): string | undefined;
|
|
33
|
+
export declare function findClosestValue(str: string, values: string[], maxLevenshtein?: number): string | undefined;
|
|
@@ -0,0 +1,57 @@
|
|
|
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.findClosestValue = exports.findStringIgnoringCase = exports.normalizeOptions = exports.actionStatusSuffix = exports.actionStatusColor = exports.actionStatusLabel = exports.PartiallySafeHint = exports.SwizzleActionsStatuses = exports.SwizzleActions = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const leven_1 = (0, tslib_1.__importDefault)(require("leven"));
|
|
12
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
13
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
14
|
+
exports.SwizzleActions = ['wrap', 'eject'];
|
|
15
|
+
exports.SwizzleActionsStatuses = [
|
|
16
|
+
'safe',
|
|
17
|
+
'unsafe',
|
|
18
|
+
'forbidden',
|
|
19
|
+
];
|
|
20
|
+
exports.PartiallySafeHint = logger_1.default.red('*');
|
|
21
|
+
function actionStatusLabel(status) {
|
|
22
|
+
return lodash_1.default.capitalize(status);
|
|
23
|
+
}
|
|
24
|
+
exports.actionStatusLabel = actionStatusLabel;
|
|
25
|
+
const SwizzleActionStatusColors = {
|
|
26
|
+
safe: logger_1.default.green,
|
|
27
|
+
unsafe: logger_1.default.yellow,
|
|
28
|
+
forbidden: logger_1.default.red,
|
|
29
|
+
};
|
|
30
|
+
function actionStatusColor(status, str) {
|
|
31
|
+
const colorFn = SwizzleActionStatusColors[status];
|
|
32
|
+
return colorFn(str);
|
|
33
|
+
}
|
|
34
|
+
exports.actionStatusColor = actionStatusColor;
|
|
35
|
+
function actionStatusSuffix(status, options = {}) {
|
|
36
|
+
return ` (${actionStatusColor(status, actionStatusLabel(status))}${options.partiallySafe ? exports.PartiallySafeHint : ''})`;
|
|
37
|
+
}
|
|
38
|
+
exports.actionStatusSuffix = actionStatusSuffix;
|
|
39
|
+
function normalizeOptions(options) {
|
|
40
|
+
var _a, _b, _c, _d, _e;
|
|
41
|
+
return {
|
|
42
|
+
typescript: (_a = options.typescript) !== null && _a !== void 0 ? _a : false,
|
|
43
|
+
danger: (_b = options.danger) !== null && _b !== void 0 ? _b : false,
|
|
44
|
+
list: (_c = options.list) !== null && _c !== void 0 ? _c : false,
|
|
45
|
+
wrap: (_d = options.wrap) !== null && _d !== void 0 ? _d : false,
|
|
46
|
+
eject: (_e = options.eject) !== null && _e !== void 0 ? _e : false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
exports.normalizeOptions = normalizeOptions;
|
|
50
|
+
function findStringIgnoringCase(str, values) {
|
|
51
|
+
return values.find((v) => v.toLowerCase() === str.toLowerCase());
|
|
52
|
+
}
|
|
53
|
+
exports.findStringIgnoringCase = findStringIgnoringCase;
|
|
54
|
+
function findClosestValue(str, values, maxLevenshtein = 3) {
|
|
55
|
+
return values.find((v) => (0, leven_1.default)(v, str) <= maxLevenshtein);
|
|
56
|
+
}
|
|
57
|
+
exports.findClosestValue = findClosestValue;
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { SwizzleAction, SwizzleActionStatus, SwizzleComponentConfig, SwizzleConfig } from '@docusaurus/types';
|
|
8
|
+
export declare type ThemeComponents = {
|
|
9
|
+
themeName: string;
|
|
10
|
+
all: string[];
|
|
11
|
+
getConfig: (component: string) => SwizzleComponentConfig;
|
|
12
|
+
getDescription: (component: string) => string;
|
|
13
|
+
getActionStatus: (component: string, action: SwizzleAction) => SwizzleActionStatus;
|
|
14
|
+
isSafeAction: (component: string, action: SwizzleAction) => boolean;
|
|
15
|
+
hasAnySafeAction: (component: string) => boolean;
|
|
16
|
+
hasAllSafeAction: (component: string) => boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare function readComponentNames(themePath: string): Promise<string[]>;
|
|
19
|
+
export declare function listComponentNames(themeComponents: ThemeComponents): string;
|
|
20
|
+
export declare function getThemeComponents({ themeName, themePath, swizzleConfig, }: {
|
|
21
|
+
themeName: string;
|
|
22
|
+
themePath: string;
|
|
23
|
+
swizzleConfig: SwizzleConfig;
|
|
24
|
+
}): Promise<ThemeComponents>;
|
|
25
|
+
export declare function getComponentName({ componentNameParam, themeComponents, list, }: {
|
|
26
|
+
componentNameParam: string | undefined;
|
|
27
|
+
themeComponents: ThemeComponents;
|
|
28
|
+
list: boolean | undefined;
|
|
29
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,165 @@
|
|
|
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.getComponentName = exports.getThemeComponents = exports.listComponentNames = exports.readComponentNames = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
12
|
+
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
13
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
14
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
15
|
+
const utils_1 = require("@docusaurus/utils");
|
|
16
|
+
const prompts_1 = require("./prompts");
|
|
17
|
+
const common_1 = require("./common");
|
|
18
|
+
const tables_1 = require("./tables");
|
|
19
|
+
const actions_1 = require("./actions");
|
|
20
|
+
const formatComponentName = (componentName) => componentName.replace(/[/\\]index\.[jt]sx?/, '').replace(/\.[jt]sx?/, '');
|
|
21
|
+
const skipReadDirNames = ['__test__', '__tests__', '__mocks__', '__fixtures__'];
|
|
22
|
+
async function readComponentNames(themePath) {
|
|
23
|
+
if (!(await fs_extra_1.default.pathExists(themePath))) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
async function walk(dir) {
|
|
27
|
+
const files = await Promise.all((await fs_extra_1.default.readdir(dir)).flatMap(async (file) => {
|
|
28
|
+
const fullPath = path_1.default.join(dir, file);
|
|
29
|
+
const stat = await fs_extra_1.default.stat(fullPath);
|
|
30
|
+
const isDir = stat.isDirectory();
|
|
31
|
+
return { file, fullPath, isDir };
|
|
32
|
+
}));
|
|
33
|
+
return (await Promise.all(files.map(async (file) => {
|
|
34
|
+
if (file.isDir) {
|
|
35
|
+
if (skipReadDirNames.includes(file.file)) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
return walk(file.fullPath);
|
|
39
|
+
}
|
|
40
|
+
else if (
|
|
41
|
+
// TODO can probably be refactored
|
|
42
|
+
/(?<!\.d)\.[jt]sx?$/.test(file.fullPath) &&
|
|
43
|
+
!/(?<!\.d)\.(?:test|tests|story|stories)\.[jt]sx?$/.test(file.fullPath)) {
|
|
44
|
+
const componentName = formatComponentName((0, utils_1.posixPath)(path_1.default.relative(themePath, file.fullPath)));
|
|
45
|
+
return [{ ...file, componentName }];
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
48
|
+
}))).flat();
|
|
49
|
+
}
|
|
50
|
+
const componentFiles = await walk(themePath);
|
|
51
|
+
const componentFilesOrdered = lodash_1.default.orderBy(componentFiles, [(f) => f.componentName], ['asc']);
|
|
52
|
+
return componentFilesOrdered.map((f) => f.componentName);
|
|
53
|
+
}
|
|
54
|
+
exports.readComponentNames = readComponentNames;
|
|
55
|
+
function listComponentNames(themeComponents) {
|
|
56
|
+
if (themeComponents.all.length === 0) {
|
|
57
|
+
return 'No component to swizzle.';
|
|
58
|
+
}
|
|
59
|
+
return `${(0, tables_1.themeComponentsTable)(themeComponents)}
|
|
60
|
+
|
|
61
|
+
${(0, tables_1.helpTables)()}
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
exports.listComponentNames = listComponentNames;
|
|
65
|
+
async function getThemeComponents({ themeName, themePath, swizzleConfig, }) {
|
|
66
|
+
const FallbackSwizzleActionStatus = 'unsafe';
|
|
67
|
+
const FallbackSwizzleComponentDescription = 'N/A';
|
|
68
|
+
const FallbackSwizzleComponentConfig = {
|
|
69
|
+
actions: {
|
|
70
|
+
wrap: FallbackSwizzleActionStatus,
|
|
71
|
+
eject: FallbackSwizzleActionStatus,
|
|
72
|
+
},
|
|
73
|
+
description: FallbackSwizzleComponentDescription,
|
|
74
|
+
};
|
|
75
|
+
const allComponents = await readComponentNames(themePath);
|
|
76
|
+
function getConfig(component) {
|
|
77
|
+
var _a;
|
|
78
|
+
if (!allComponents.includes(component)) {
|
|
79
|
+
throw new Error(`Can't get component config: component doesn't exist: ${component}`);
|
|
80
|
+
}
|
|
81
|
+
return ((_a = swizzleConfig.components[component]) !== null && _a !== void 0 ? _a : FallbackSwizzleComponentConfig);
|
|
82
|
+
}
|
|
83
|
+
function getDescription(component) {
|
|
84
|
+
var _a;
|
|
85
|
+
return ((_a = getConfig(component).description) !== null && _a !== void 0 ? _a : FallbackSwizzleComponentDescription);
|
|
86
|
+
}
|
|
87
|
+
function getActionStatus(component, action) {
|
|
88
|
+
var _a;
|
|
89
|
+
return (_a = getConfig(component).actions[action]) !== null && _a !== void 0 ? _a : FallbackSwizzleActionStatus;
|
|
90
|
+
}
|
|
91
|
+
function isSafeAction(component, action) {
|
|
92
|
+
return getActionStatus(component, action) === 'safe';
|
|
93
|
+
}
|
|
94
|
+
function hasAllSafeAction(component) {
|
|
95
|
+
return actions_1.SwizzleActions.every((action) => isSafeAction(component, action));
|
|
96
|
+
}
|
|
97
|
+
function hasAnySafeAction(component) {
|
|
98
|
+
return actions_1.SwizzleActions.some((action) => isSafeAction(component, action));
|
|
99
|
+
}
|
|
100
|
+
// Present the safest components first
|
|
101
|
+
const orderedComponents = lodash_1.default.orderBy(allComponents, [
|
|
102
|
+
hasAllSafeAction,
|
|
103
|
+
(component) => isSafeAction(component, 'wrap'),
|
|
104
|
+
(component) => isSafeAction(component, 'eject'),
|
|
105
|
+
(component) => component,
|
|
106
|
+
], ['desc', 'desc', 'desc', 'asc']);
|
|
107
|
+
return {
|
|
108
|
+
themeName,
|
|
109
|
+
all: orderedComponents,
|
|
110
|
+
getConfig,
|
|
111
|
+
getDescription,
|
|
112
|
+
getActionStatus,
|
|
113
|
+
isSafeAction,
|
|
114
|
+
hasAnySafeAction,
|
|
115
|
+
hasAllSafeAction,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
exports.getThemeComponents = getThemeComponents;
|
|
119
|
+
// Returns a valid value if recovering is possible
|
|
120
|
+
function handleInvalidComponentNameParam({ componentNameParam, themeComponents, }) {
|
|
121
|
+
// Trying to recover invalid value
|
|
122
|
+
// We look for potential matches that only differ in casing.
|
|
123
|
+
const differentCaseMatch = (0, common_1.findStringIgnoringCase)(componentNameParam, themeComponents.all);
|
|
124
|
+
if (differentCaseMatch) {
|
|
125
|
+
logger_1.default.warn `Component name=${componentNameParam} doesn't exist.`;
|
|
126
|
+
logger_1.default.info `name=${differentCaseMatch} will be used instead of name=${componentNameParam}.`;
|
|
127
|
+
return differentCaseMatch;
|
|
128
|
+
}
|
|
129
|
+
// No recovery value is possible: print error
|
|
130
|
+
logger_1.default.error `Component name=${componentNameParam} not found.`;
|
|
131
|
+
const suggestion = (0, common_1.findClosestValue)(componentNameParam, themeComponents.all);
|
|
132
|
+
if (suggestion) {
|
|
133
|
+
logger_1.default.info `Did you mean name=${suggestion}? ${themeComponents.hasAnySafeAction(suggestion)
|
|
134
|
+
? `Note: this component is an unsafe internal component and can only be swizzled with code=${'--danger'} or explicit confirmation.`
|
|
135
|
+
: ''}`;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
logger_1.default.info(listComponentNames(themeComponents));
|
|
139
|
+
}
|
|
140
|
+
return process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
async function handleComponentNameParam({ componentNameParam, themeComponents, }) {
|
|
143
|
+
const isValidName = themeComponents.all.includes(componentNameParam);
|
|
144
|
+
if (!isValidName) {
|
|
145
|
+
return handleInvalidComponentNameParam({
|
|
146
|
+
componentNameParam,
|
|
147
|
+
themeComponents,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return componentNameParam;
|
|
151
|
+
}
|
|
152
|
+
async function getComponentName({ componentNameParam, themeComponents, list, }) {
|
|
153
|
+
if (list) {
|
|
154
|
+
logger_1.default.info(listComponentNames(themeComponents));
|
|
155
|
+
return process.exit(0);
|
|
156
|
+
}
|
|
157
|
+
const componentName = componentNameParam
|
|
158
|
+
? await handleComponentNameParam({
|
|
159
|
+
componentNameParam,
|
|
160
|
+
themeComponents,
|
|
161
|
+
})
|
|
162
|
+
: await (0, prompts_1.askComponentName)(themeComponents);
|
|
163
|
+
return componentName;
|
|
164
|
+
}
|
|
165
|
+
exports.getComponentName = getComponentName;
|
|
@@ -0,0 +1,10 @@
|
|
|
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 { SwizzleConfig } from '@docusaurus/types';
|
|
8
|
+
import type { SwizzlePlugin } from './common';
|
|
9
|
+
export declare function normalizeSwizzleConfig(unsafeSwizzleConfig: unknown): SwizzleConfig;
|
|
10
|
+
export declare function getThemeSwizzleConfig(themeName: string, plugins: SwizzlePlugin[]): SwizzleConfig;
|
|
@@ -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>;
|