@docusaurus/core 2.0.0-beta.fc64c12e4 → 2.0.0

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.
Files changed (224) hide show
  1. package/README.md +1 -0
  2. package/bin/beforeCli.mjs +140 -0
  3. package/bin/{docusaurus.js → docusaurus.mjs} +81 -118
  4. package/lib/babel/preset.d.ts +8 -3
  5. package/lib/babel/preset.js +10 -8
  6. package/lib/client/App.d.ts +2 -3
  7. package/lib/client/App.js +31 -28
  8. package/lib/client/BaseUrlIssueBanner/index.d.ts +27 -0
  9. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.js → BaseUrlIssueBanner/index.js} +25 -14
  10. package/lib/client/{baseUrlIssueBanner → BaseUrlIssueBanner}/styles.module.css +0 -0
  11. package/lib/client/ClientLifecyclesDispatcher.d.ts +16 -0
  12. package/lib/client/ClientLifecyclesDispatcher.js +39 -0
  13. package/lib/client/LinksCollector.d.ts +3 -3
  14. package/lib/client/LinksCollector.js +8 -13
  15. package/lib/client/PendingNavigation.d.ts +17 -3
  16. package/lib/client/PendingNavigation.js +47 -72
  17. package/lib/client/{baseUrlIssueBanner/BaseUrlIssueBanner.d.ts → SiteMetadataDefaults.d.ts} +1 -2
  18. package/lib/client/SiteMetadataDefaults.js +21 -0
  19. package/lib/{choosePort.d.ts → client/browserContext.d.ts} +5 -5
  20. package/lib/client/browserContext.js +22 -0
  21. package/lib/client/clientEntry.js +14 -9
  22. package/lib/client/docusaurus.d.ts +12 -6
  23. package/lib/client/docusaurus.js +30 -43
  24. package/lib/client/docusaurusContext.d.ts +12 -0
  25. package/lib/client/docusaurusContext.js +25 -0
  26. package/lib/client/exports/BrowserOnly.d.ts +3 -5
  27. package/lib/client/exports/BrowserOnly.js +14 -8
  28. package/lib/client/exports/ComponentCreator.d.ts +6 -2
  29. package/lib/client/exports/ComponentCreator.js +75 -42
  30. package/lib/client/exports/ErrorBoundary.d.ts +18 -0
  31. package/lib/client/exports/ErrorBoundary.js +37 -0
  32. package/lib/client/exports/ExecutionEnvironment.js +7 -8
  33. package/lib/client/exports/Head.d.ts +2 -3
  34. package/lib/client/exports/Head.js +3 -4
  35. package/lib/client/exports/Interpolate.d.ts +2 -2
  36. package/lib/client/exports/Interpolate.js +18 -51
  37. package/lib/client/exports/Link.d.ts +4 -10
  38. package/lib/client/exports/Link.js +38 -38
  39. package/lib/client/exports/Translate.d.ts +1 -1
  40. package/lib/client/exports/Translate.js +11 -10
  41. package/lib/client/exports/constants.js +1 -11
  42. package/lib/client/exports/isInternalUrl.js +1 -1
  43. package/lib/client/exports/renderRoutes.d.ts +1 -2
  44. package/lib/client/exports/renderRoutes.js +1 -2
  45. package/lib/client/exports/router.d.ts +1 -1
  46. package/lib/client/exports/router.js +1 -1
  47. package/lib/client/exports/useBaseUrl.js +11 -14
  48. package/lib/client/exports/useDocusaurusContext.d.ts +2 -3
  49. package/lib/client/exports/useDocusaurusContext.js +3 -9
  50. package/lib/client/exports/useGlobalData.d.ts +4 -3
  51. package/lib/client/exports/useGlobalData.js +6 -13
  52. package/lib/{server/versions/__tests/index.test.d.ts → client/exports/useIsBrowser.d.ts} +1 -1
  53. package/lib/client/exports/useIsBrowser.js +11 -0
  54. package/lib/client/exports/useRouteContext.d.ts +8 -0
  55. package/lib/client/exports/useRouteContext.js +15 -0
  56. package/lib/client/flat.d.ts +12 -2
  57. package/lib/client/flat.js +19 -15
  58. package/lib/client/normalizeLocation.d.ts +2 -5
  59. package/lib/client/normalizeLocation.js +14 -10
  60. package/lib/client/prefetch.d.ts +1 -2
  61. package/lib/client/prefetch.js +12 -32
  62. package/lib/client/preload.d.ts +3 -4
  63. package/lib/client/preload.js +5 -12
  64. package/lib/client/routeContext.d.ts +13 -0
  65. package/lib/client/routeContext.js +31 -0
  66. package/lib/client/serverEntry.d.ts +10 -0
  67. package/lib/client/serverEntry.js +108 -146
  68. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  69. package/lib/client/theme-fallback/Error/index.js +45 -0
  70. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  71. package/lib/client/theme-fallback/Layout/index.js +2 -26
  72. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  73. package/lib/client/theme-fallback/Loading/index.js +50 -116
  74. package/lib/{server/html-tags/htmlTags.d.ts → client/theme-fallback/NotFound/index.d.ts} +2 -1
  75. package/lib/client/theme-fallback/NotFound/index.js +19 -18
  76. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  77. package/lib/client/theme-fallback/Root/index.js +2 -6
  78. package/lib/client/{exports/context.js → theme-fallback/SiteMetadata/index.d.ts} +2 -2
  79. package/lib/client/theme-fallback/SiteMetadata/index.js +10 -0
  80. package/lib/commands/build.d.ts +6 -2
  81. package/lib/commands/build.js +81 -64
  82. package/lib/commands/clear.d.ts +7 -1
  83. package/lib/commands/clear.js +34 -21
  84. package/lib/commands/deploy.d.ts +5 -2
  85. package/lib/commands/deploy.js +94 -81
  86. package/lib/commands/external.d.ts +2 -2
  87. package/lib/commands/external.js +9 -11
  88. package/lib/commands/serve.d.ts +8 -2
  89. package/lib/commands/serve.js +29 -28
  90. package/lib/commands/start.d.ts +9 -2
  91. package/lib/commands/start.js +107 -102
  92. package/lib/commands/swizzle/actions.d.ts +23 -0
  93. package/lib/commands/swizzle/actions.js +106 -0
  94. package/lib/commands/swizzle/common.d.ts +33 -0
  95. package/lib/commands/swizzle/common.js +56 -0
  96. package/lib/commands/swizzle/components.d.ts +29 -0
  97. package/lib/commands/swizzle/components.js +200 -0
  98. package/lib/commands/swizzle/config.d.ts +10 -0
  99. package/lib/commands/swizzle/config.js +84 -0
  100. package/lib/{server/client-modules/index.d.ts → commands/swizzle/context.d.ts} +2 -2
  101. package/lib/commands/swizzle/context.js +24 -0
  102. package/lib/commands/swizzle/index.d.ts +8 -0
  103. package/lib/commands/swizzle/index.js +119 -0
  104. package/lib/commands/swizzle/prompts.d.ts +12 -0
  105. package/lib/commands/swizzle/prompts.js +110 -0
  106. package/lib/{client/exports/context.d.ts → commands/swizzle/tables.d.ts} +3 -4
  107. package/lib/commands/swizzle/tables.js +113 -0
  108. package/lib/commands/swizzle/themes.d.ts +20 -0
  109. package/lib/commands/swizzle/themes.js +106 -0
  110. package/lib/commands/writeHeadingIds.d.ts +2 -6
  111. package/lib/commands/writeHeadingIds.js +22 -81
  112. package/lib/commands/writeTranslations.d.ts +4 -5
  113. package/lib/commands/writeTranslations.js +46 -25
  114. package/lib/index.d.ts +9 -9
  115. package/lib/index.js +14 -14
  116. package/lib/server/brokenLinks.d.ts +4 -17
  117. package/lib/server/brokenLinks.js +67 -56
  118. package/lib/server/clientModules.d.ts +12 -0
  119. package/lib/server/clientModules.js +20 -0
  120. package/lib/server/config.d.ts +5 -2
  121. package/lib/server/config.js +29 -6
  122. package/lib/server/configValidation.d.ts +4 -4
  123. package/lib/server/configValidation.js +86 -41
  124. package/lib/server/getHostPort.d.ts +14 -0
  125. package/lib/server/getHostPort.js +79 -0
  126. package/lib/server/htmlTags.d.ts +12 -0
  127. package/lib/server/htmlTags.js +62 -0
  128. package/lib/server/i18n.d.ts +3 -13
  129. package/lib/server/i18n.js +21 -55
  130. package/lib/server/index.d.ts +28 -13
  131. package/lib/server/index.js +83 -165
  132. package/lib/server/plugins/configs.d.ts +51 -0
  133. package/lib/server/plugins/configs.js +101 -0
  134. package/lib/server/plugins/index.d.ts +9 -8
  135. package/lib/server/plugins/index.js +73 -137
  136. package/lib/server/plugins/init.d.ts +6 -5
  137. package/lib/server/plugins/init.js +44 -109
  138. package/lib/server/plugins/moduleShorthand.d.ts +9 -0
  139. package/lib/server/plugins/moduleShorthand.js +46 -0
  140. package/lib/server/plugins/pluginIds.d.ts +5 -1
  141. package/lib/server/plugins/pluginIds.js +12 -7
  142. package/lib/server/plugins/presets.d.ts +12 -0
  143. package/lib/server/plugins/presets.js +49 -0
  144. package/lib/server/plugins/routeConfig.d.ts +11 -0
  145. package/lib/server/plugins/routeConfig.js +54 -0
  146. package/lib/server/plugins/synthetic.d.ts +20 -0
  147. package/lib/server/plugins/synthetic.js +111 -0
  148. package/lib/server/routes.d.ts +39 -7
  149. package/lib/server/routes.js +166 -99
  150. package/lib/server/siteMetadata.d.ts +12 -0
  151. package/lib/server/siteMetadata.js +81 -0
  152. package/lib/server/translations/translations.d.ts +14 -19
  153. package/lib/server/translations/translations.js +40 -72
  154. package/lib/server/translations/translationsExtractor.d.ts +10 -4
  155. package/lib/server/translations/translationsExtractor.js +158 -122
  156. package/lib/server/utils.d.ts +7 -1
  157. package/lib/server/utils.js +7 -8
  158. package/lib/webpack/aliases/index.d.ts +34 -0
  159. package/lib/webpack/aliases/index.js +106 -0
  160. package/lib/webpack/base.d.ts +3 -4
  161. package/lib/webpack/base.js +45 -57
  162. package/lib/webpack/client.d.ts +3 -3
  163. package/lib/webpack/client.js +12 -19
  164. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +13 -3
  165. package/lib/webpack/plugins/ChunkAssetPlugin.js +24 -17
  166. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +6 -25
  167. package/lib/webpack/plugins/CleanWebpackPlugin.js +33 -17
  168. package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
  169. package/lib/webpack/plugins/LogPlugin.js +4 -5
  170. package/lib/webpack/plugins/WaitPlugin.d.ts +3 -3
  171. package/lib/webpack/plugins/WaitPlugin.js +30 -29
  172. package/lib/webpack/server.d.ts +5 -5
  173. package/lib/webpack/server.js +21 -12
  174. package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
  175. package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
  176. package/lib/{client → webpack}/templates/ssr.html.template.js +3 -2
  177. package/lib/webpack/utils.d.ts +15 -31
  178. package/lib/webpack/utils.js +61 -182
  179. package/package.json +81 -77
  180. package/bin/beforeCli.js +0 -103
  181. package/lib/.tsbuildinfo +0 -1
  182. package/lib/choosePort.js +0 -105
  183. package/lib/client/.eslintrc.js +0 -29
  184. package/lib/client/.tsbuildinfo +0 -1
  185. package/lib/client/client-lifecycles-dispatcher.d.ts +0 -12
  186. package/lib/client/client-lifecycles-dispatcher.js +0 -27
  187. package/lib/client/nprogress.css +0 -36
  188. package/lib/commands/buildRemoteBranchUrl.d.ts +0 -7
  189. package/lib/commands/buildRemoteBranchUrl.js +0 -27
  190. package/lib/commands/commandUtils.d.ts +0 -3
  191. package/lib/commands/commandUtils.js +0 -21
  192. package/lib/commands/swizzle.d.ts +0 -9
  193. package/lib/commands/swizzle.js +0 -245
  194. package/lib/constants.d.ts +0 -18
  195. package/lib/constants.js +0 -23
  196. package/lib/server/client-modules/index.js +0 -14
  197. package/lib/server/duplicateRoutes.d.ts +0 -10
  198. package/lib/server/duplicateRoutes.js +0 -38
  199. package/lib/server/html-tags/htmlTags.js +0 -40
  200. package/lib/server/html-tags/index.d.ts +0 -9
  201. package/lib/server/html-tags/index.js +0 -43
  202. package/lib/server/loadSetup.d.ts +0 -9
  203. package/lib/server/loadSetup.js +0 -25
  204. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +0 -9
  205. package/lib/server/plugins/applyRouteTrailingSlash.js +0 -19
  206. package/lib/server/presets/index.d.ts +0 -11
  207. package/lib/server/presets/index.js +0 -48
  208. package/lib/server/themes/alias.d.ts +0 -8
  209. package/lib/server/themes/alias.js +0 -39
  210. package/lib/server/themes/index.d.ts +0 -12
  211. package/lib/server/themes/index.js +0 -47
  212. package/lib/server/versions/__fixtures__/dummy-plugin.js +0 -0
  213. package/lib/server/versions/__fixtures__/package.json +0 -3
  214. package/lib/server/versions/__tests/index.test.js +0 -25
  215. package/lib/server/versions/index.d.ts +0 -10
  216. package/lib/server/versions/index.js +0 -50
  217. package/lib/webpack/react-dev-utils-webpack5/README.md +0 -11
  218. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.js +0 -57
  219. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +0 -138
  220. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.js +0 -285
  221. package/lib/webpack/sharedModuleAliases.d.ts +0 -10
  222. package/lib/webpack/sharedModuleAliases.js +0 -18
  223. package/tsconfig.client.json +0 -13
  224. package/tsconfig.json +0 -13
@@ -0,0 +1,200 @@
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 fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
12
+ const path_1 = tslib_1.__importDefault(require("path"));
13
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
14
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
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
+ function sortComponentNames(componentNames) {
22
+ return componentNames.sort(); // Algo may change?
23
+ }
24
+ /**
25
+ * Expand a list of components to include and return parent folders.
26
+ * If a folder is not directly a component (no Folder/index.tsx file),
27
+ * we still want to be able to swizzle --eject that folder.
28
+ * See https://github.com/facebook/docusaurus/pull/7175#issuecomment-1103757218
29
+ *
30
+ * @param componentNames the original list of component names
31
+ */
32
+ function getMissingIntermediateComponentFolderNames(componentNames) {
33
+ function getAllIntermediatePaths(componentName) {
34
+ const paths = componentName.split('/');
35
+ return lodash_1.default.range(1, paths.length + 1).map((i) => paths.slice(0, i).join('/'));
36
+ }
37
+ const expandedComponentNames = lodash_1.default.uniq(componentNames.flatMap((componentName) => getAllIntermediatePaths(componentName)));
38
+ return lodash_1.default.difference(expandedComponentNames, componentNames);
39
+ }
40
+ const skipReadDirNames = ['__test__', '__tests__', '__mocks__', '__fixtures__'];
41
+ async function readComponentNames(themePath) {
42
+ if (!(await fs_extra_1.default.pathExists(themePath))) {
43
+ return [];
44
+ }
45
+ async function walk(dir) {
46
+ const files = await Promise.all((await fs_extra_1.default.readdir(dir)).flatMap(async (file) => {
47
+ const fullPath = path_1.default.join(dir, file);
48
+ const stat = await fs_extra_1.default.stat(fullPath);
49
+ const isDir = stat.isDirectory();
50
+ return { file, fullPath, isDir };
51
+ }));
52
+ return (await Promise.all(files.map(async (file) => {
53
+ if (file.isDir) {
54
+ if (skipReadDirNames.includes(file.file)) {
55
+ return [];
56
+ }
57
+ return walk(file.fullPath);
58
+ }
59
+ else if (
60
+ // TODO can probably be refactored
61
+ /(?<!\.d)\.[jt]sx?$/.test(file.fullPath) &&
62
+ !/(?<!\.d)\.(?:test|tests|story|stories)\.[jt]sx?$/.test(file.fullPath)) {
63
+ const componentName = formatComponentName((0, utils_1.posixPath)(path_1.default.relative(themePath, file.fullPath)));
64
+ return [{ ...file, componentName }];
65
+ }
66
+ return [];
67
+ }))).flat();
68
+ }
69
+ const componentFiles = await walk(themePath);
70
+ const componentNames = componentFiles.map((f) => f.componentName);
71
+ return sortComponentNames(componentNames);
72
+ }
73
+ exports.readComponentNames = readComponentNames;
74
+ function listComponentNames(themeComponents) {
75
+ if (themeComponents.all.length === 0) {
76
+ return 'No component to swizzle.';
77
+ }
78
+ return `${(0, tables_1.themeComponentsTable)(themeComponents)}
79
+
80
+ ${(0, tables_1.helpTables)()}
81
+ `;
82
+ }
83
+ exports.listComponentNames = listComponentNames;
84
+ async function getThemeComponents({ themeName, themePath, swizzleConfig, }) {
85
+ const FallbackSwizzleActionStatus = 'unsafe';
86
+ const FallbackSwizzleComponentDescription = 'N/A';
87
+ const FallbackSwizzleComponentConfig = {
88
+ actions: {
89
+ wrap: FallbackSwizzleActionStatus,
90
+ eject: FallbackSwizzleActionStatus,
91
+ },
92
+ description: FallbackSwizzleComponentDescription,
93
+ };
94
+ const FallbackIntermediateFolderSwizzleComponentConfig = {
95
+ actions: {
96
+ // It doesn't make sense to wrap an intermediate folder
97
+ // because it has not any index component
98
+ wrap: 'forbidden',
99
+ eject: FallbackSwizzleActionStatus,
100
+ },
101
+ description: FallbackSwizzleComponentDescription,
102
+ };
103
+ const allInitialComponents = await readComponentNames(themePath);
104
+ const missingIntermediateComponentFolderNames = getMissingIntermediateComponentFolderNames(allInitialComponents);
105
+ const allComponents = sortComponentNames(allInitialComponents.concat(missingIntermediateComponentFolderNames));
106
+ function getConfig(component) {
107
+ if (!allComponents.includes(component)) {
108
+ throw new Error(`Can't get component config: component doesn't exist: ${component}`);
109
+ }
110
+ const config = swizzleConfig.components[component];
111
+ if (config) {
112
+ return config;
113
+ }
114
+ const isIntermediateFolder = missingIntermediateComponentFolderNames.includes(component);
115
+ if (isIntermediateFolder) {
116
+ return FallbackIntermediateFolderSwizzleComponentConfig;
117
+ }
118
+ return (swizzleConfig.components[component] ?? FallbackSwizzleComponentConfig);
119
+ }
120
+ function getDescription(component) {
121
+ return (getConfig(component).description ?? FallbackSwizzleComponentDescription);
122
+ }
123
+ function getActionStatus(component, action) {
124
+ return getConfig(component).actions[action] ?? FallbackSwizzleActionStatus;
125
+ }
126
+ function isSafeAction(component, action) {
127
+ return getActionStatus(component, action) === 'safe';
128
+ }
129
+ function hasAllSafeAction(component) {
130
+ return actions_1.SwizzleActions.every((action) => isSafeAction(component, action));
131
+ }
132
+ function hasAnySafeAction(component) {
133
+ return actions_1.SwizzleActions.some((action) => isSafeAction(component, action));
134
+ }
135
+ // Present the safest components first
136
+ const orderedComponents = lodash_1.default.orderBy(allComponents, [
137
+ hasAllSafeAction,
138
+ (component) => isSafeAction(component, 'wrap'),
139
+ (component) => isSafeAction(component, 'eject'),
140
+ (component) => component,
141
+ ], ['desc', 'desc', 'desc', 'asc']);
142
+ return {
143
+ themeName,
144
+ all: orderedComponents,
145
+ getConfig,
146
+ getDescription,
147
+ getActionStatus,
148
+ isSafeAction,
149
+ hasAnySafeAction,
150
+ hasAllSafeAction,
151
+ };
152
+ }
153
+ exports.getThemeComponents = getThemeComponents;
154
+ // Returns a valid value if recovering is possible
155
+ function handleInvalidComponentNameParam({ componentNameParam, themeComponents, }) {
156
+ // Trying to recover invalid value
157
+ // We look for potential matches that only differ in casing.
158
+ const differentCaseMatch = (0, common_1.findStringIgnoringCase)(componentNameParam, themeComponents.all);
159
+ if (differentCaseMatch) {
160
+ logger_1.default.warn `Component name=${componentNameParam} doesn't exist.`;
161
+ logger_1.default.info `name=${differentCaseMatch} will be used instead of name=${componentNameParam}.`;
162
+ return differentCaseMatch;
163
+ }
164
+ // No recovery value is possible: print error
165
+ logger_1.default.error `Component name=${componentNameParam} not found.`;
166
+ const suggestion = (0, common_1.findClosestValue)(componentNameParam, themeComponents.all);
167
+ if (suggestion) {
168
+ logger_1.default.info `Did you mean name=${suggestion}? ${themeComponents.hasAnySafeAction(suggestion)
169
+ ? `Note: this component is an unsafe internal component and can only be swizzled with code=${'--danger'} or explicit confirmation.`
170
+ : ''}`;
171
+ }
172
+ else {
173
+ logger_1.default.info(listComponentNames(themeComponents));
174
+ }
175
+ return process.exit(1);
176
+ }
177
+ async function handleComponentNameParam({ componentNameParam, themeComponents, }) {
178
+ const isValidName = themeComponents.all.includes(componentNameParam);
179
+ if (!isValidName) {
180
+ return handleInvalidComponentNameParam({
181
+ componentNameParam,
182
+ themeComponents,
183
+ });
184
+ }
185
+ return componentNameParam;
186
+ }
187
+ async function getComponentName({ componentNameParam, themeComponents, list, }) {
188
+ if (list) {
189
+ logger_1.default.info(listComponentNames(themeComponents));
190
+ return process.exit(0);
191
+ }
192
+ const componentName = componentNameParam
193
+ ? await handleComponentNameParam({
194
+ componentNameParam,
195
+ themeComponents,
196
+ })
197
+ : await (0, prompts_1.askComponentName)(themeComponents);
198
+ return componentName;
199
+ }
200
+ 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,84 @@
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 tslib_1 = require("tslib");
11
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
12
+ const utils_validation_1 = require("@docusaurus/utils-validation");
13
+ const common_1 = require("./common");
14
+ const themes_1 = require("./themes");
15
+ function getModuleSwizzleConfig(swizzlePlugin) {
16
+ const getSwizzleConfig = swizzlePlugin.plugin.plugin.getSwizzleConfig ??
17
+ swizzlePlugin.plugin.pluginModule?.module.getSwizzleConfig;
18
+ if (getSwizzleConfig) {
19
+ return getSwizzleConfig();
20
+ }
21
+ // TODO deprecate getSwizzleComponentList later
22
+ const getSwizzleComponentList = swizzlePlugin.plugin.plugin.getSwizzleComponentList ??
23
+ swizzlePlugin.plugin.pluginModule?.module.getSwizzleComponentList;
24
+ if (getSwizzleComponentList) {
25
+ const safeComponents = getSwizzleComponentList() ?? [];
26
+ const safeComponentConfig = {
27
+ actions: {
28
+ eject: 'safe',
29
+ wrap: 'safe',
30
+ },
31
+ description: undefined,
32
+ };
33
+ return {
34
+ components: Object.fromEntries(safeComponents.map((comp) => [comp, safeComponentConfig])),
35
+ };
36
+ }
37
+ return undefined;
38
+ }
39
+ const SwizzleConfigSchema = utils_validation_1.Joi.object({
40
+ components: utils_validation_1.Joi.object()
41
+ .pattern(utils_validation_1.Joi.string(), utils_validation_1.Joi.object({
42
+ 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)),
43
+ description: utils_validation_1.Joi.string(),
44
+ }))
45
+ .required(),
46
+ });
47
+ function validateSwizzleConfig(unsafeSwizzleConfig) {
48
+ const result = SwizzleConfigSchema.validate(unsafeSwizzleConfig);
49
+ if (result.error) {
50
+ throw new Error(`Swizzle config does not match expected schema: ${result.error.message}`);
51
+ }
52
+ return result.value;
53
+ }
54
+ function normalizeSwizzleConfig(unsafeSwizzleConfig) {
55
+ const swizzleConfig = validateSwizzleConfig(unsafeSwizzleConfig);
56
+ // Ensure all components always declare all actions
57
+ Object.values(swizzleConfig.components).forEach((componentConfig) => {
58
+ common_1.SwizzleActions.forEach((action) => {
59
+ if (!componentConfig.actions[action]) {
60
+ componentConfig.actions[action] = 'unsafe';
61
+ }
62
+ });
63
+ });
64
+ return swizzleConfig;
65
+ }
66
+ exports.normalizeSwizzleConfig = normalizeSwizzleConfig;
67
+ const FallbackSwizzleConfig = {
68
+ components: {},
69
+ };
70
+ function getThemeSwizzleConfig(themeName, plugins) {
71
+ const plugin = (0, themes_1.getPluginByThemeName)(plugins, themeName);
72
+ const config = getModuleSwizzleConfig(plugin);
73
+ if (config) {
74
+ try {
75
+ return normalizeSwizzleConfig(config);
76
+ }
77
+ catch (err) {
78
+ logger_1.default.error `Invalid Swizzle config for theme name=${themeName}.`;
79
+ throw err;
80
+ }
81
+ }
82
+ return FallbackSwizzleConfig;
83
+ }
84
+ exports.getThemeSwizzleConfig = getThemeSwizzleConfig;
@@ -4,5 +4,5 @@
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
- import { Plugin } from '@docusaurus/types';
8
- export default function loadClientModules(plugins: Plugin<unknown>[]): string[];
7
+ import type { SwizzleContext } from './common';
8
+ export declare function initSwizzleContext(siteDir: string): Promise<SwizzleContext>;
@@ -0,0 +1,24 @@
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 server_1 = require("../../server");
11
+ const init_1 = require("../../server/plugins/init");
12
+ const configs_1 = require("../../server/plugins/configs");
13
+ async function initSwizzleContext(siteDir) {
14
+ const context = await (0, server_1.loadContext)({ siteDir });
15
+ const plugins = await (0, init_1.initPlugins)(context);
16
+ const pluginConfigs = await (0, configs_1.loadPluginConfigs)(context);
17
+ return {
18
+ plugins: plugins.map((plugin, pluginIndex) => ({
19
+ plugin: pluginConfigs[pluginIndex],
20
+ instance: plugin,
21
+ })),
22
+ };
23
+ }
24
+ 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 { SwizzleCLIOptions } from './common';
8
+ export declare function swizzle(themeNameParam?: string | undefined, componentNameParam?: string | undefined, siteDirParam?: string, optionsParam?: Partial<SwizzleCLIOptions>): Promise<void>;
@@ -0,0 +1,119 @@
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.swizzle = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
12
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
13
+ const themes_1 = require("./themes");
14
+ const components_1 = require("./components");
15
+ const tables_1 = require("./tables");
16
+ const common_1 = require("./common");
17
+ const actions_1 = require("./actions");
18
+ const config_1 = require("./config");
19
+ const prompts_1 = require("./prompts");
20
+ const context_1 = require("./context");
21
+ async function listAllThemeComponents({ themeNames, plugins, typescript, }) {
22
+ const themeComponentsTables = (await Promise.all(themeNames.map(async (themeName) => {
23
+ const themePath = (0, themes_1.getThemePath)({ themeName, plugins, typescript });
24
+ const swizzleConfig = (0, config_1.getThemeSwizzleConfig)(themeName, plugins);
25
+ const themeComponents = await (0, components_1.getThemeComponents)({
26
+ themeName,
27
+ themePath,
28
+ swizzleConfig,
29
+ });
30
+ return (0, tables_1.themeComponentsTable)(themeComponents);
31
+ }))).join('\n\n');
32
+ logger_1.default.info(`All theme components available to swizzle:
33
+
34
+ ${themeComponentsTables}
35
+
36
+ ${(0, tables_1.helpTables)()}
37
+ `);
38
+ return process.exit(0);
39
+ }
40
+ async function ensureActionSafety({ componentName, componentConfig, action, danger, }) {
41
+ const actionStatus = componentConfig.actions[action];
42
+ if (actionStatus === 'forbidden') {
43
+ logger_1.default.error `
44
+ Swizzle action name=${action} is forbidden for component name=${componentName}
45
+ `;
46
+ return process.exit(1);
47
+ }
48
+ if (actionStatus === 'unsafe' && !danger) {
49
+ logger_1.default.warn `
50
+ Swizzle action name=${action} is unsafe to perform on name=${componentName}.
51
+ It is more likely to be affected by breaking changes in the future
52
+ If you want to swizzle it, use the code=${'--danger'} flag, or confirm that you understand the risks.
53
+ `;
54
+ const swizzleDangerousComponent = await (0, prompts_1.askSwizzleDangerousComponent)();
55
+ if (!swizzleDangerousComponent) {
56
+ return process.exit(1);
57
+ }
58
+ }
59
+ return undefined;
60
+ }
61
+ async function swizzle(themeNameParam = undefined, componentNameParam = undefined, siteDirParam = '.', optionsParam = {}) {
62
+ const siteDir = await fs_extra_1.default.realpath(siteDirParam);
63
+ const options = (0, common_1.normalizeOptions)(optionsParam);
64
+ const { list, danger, typescript } = options;
65
+ const { plugins } = await (0, context_1.initSwizzleContext)(siteDir);
66
+ const themeNames = (0, themes_1.getThemeNames)(plugins);
67
+ if (list && !themeNameParam) {
68
+ await listAllThemeComponents({ themeNames, plugins, typescript });
69
+ }
70
+ const themeName = await (0, themes_1.getThemeName)({ themeNameParam, themeNames, list });
71
+ const themePath = (0, themes_1.getThemePath)({ themeName, plugins, typescript });
72
+ const swizzleConfig = (0, config_1.getThemeSwizzleConfig)(themeName, plugins);
73
+ const themeComponents = await (0, components_1.getThemeComponents)({
74
+ themeName,
75
+ themePath,
76
+ swizzleConfig,
77
+ });
78
+ const componentName = await (0, components_1.getComponentName)({
79
+ componentNameParam,
80
+ themeComponents,
81
+ list,
82
+ });
83
+ const componentConfig = themeComponents.getConfig(componentName);
84
+ const action = await (0, actions_1.getAction)(componentConfig, options);
85
+ await ensureActionSafety({ componentName, componentConfig, action, danger });
86
+ async function executeAction() {
87
+ switch (action) {
88
+ case 'wrap': {
89
+ const result = await (0, actions_1.wrap)({
90
+ siteDir,
91
+ themePath,
92
+ componentName,
93
+ typescript,
94
+ });
95
+ logger_1.default.success `
96
+ Created wrapper of name=${componentName} from name=${themeName} in path=${result.createdFiles}
97
+ `;
98
+ return result;
99
+ }
100
+ case 'eject': {
101
+ const result = await (0, actions_1.eject)({
102
+ siteDir,
103
+ themePath,
104
+ componentName,
105
+ typescript,
106
+ });
107
+ logger_1.default.success `
108
+ Ejected name=${componentName} from name=${themeName} to path=${result.createdFiles}
109
+ `;
110
+ return result;
111
+ }
112
+ default:
113
+ throw new Error(`Unexpected action ${action}`);
114
+ }
115
+ }
116
+ await executeAction();
117
+ return process.exit(0);
118
+ }
119
+ exports.swizzle = 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 = tslib_1.__importDefault(require("@docusaurus/logger"));
12
+ const prompts_1 = 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;
@@ -4,7 +4,6 @@
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
- import React from 'react';
8
- import { DocusaurusContext } from '@docusaurus/types';
9
- declare const _default: React.Context<DocusaurusContext | null>;
10
- export default _default;
7
+ import type { ThemeComponents } from './components';
8
+ export declare function helpTables(): string;
9
+ export declare function themeComponentsTable(themeComponents: ThemeComponents): string;