/
index.ts
163 lines (142 loc) · 4.58 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import logger from '@docusaurus/logger';
import {getThemeName, getThemePath, getThemeNames} from './themes';
import {getThemeComponents, getComponentName} from './components';
import {helpTables, themeComponentsTable} from './tables';
import {normalizeOptions} from './common';
import {eject, getAction, wrap} from './actions';
import {getThemeSwizzleConfig} from './config';
import {askSwizzleDangerousComponent} from './prompts';
import {initSwizzleContext} from './context';
import type {SwizzleAction, SwizzleComponentConfig} from '@docusaurus/types';
import type {SwizzleCLIOptions, SwizzlePlugin} from './common';
import type {ActionResult} from './actions';
async function listAllThemeComponents({
themeNames,
plugins,
typescript,
}: {
themeNames: string[];
plugins: SwizzlePlugin[];
typescript: SwizzleCLIOptions['typescript'];
}) {
const themeComponentsTables = (
await Promise.all(
themeNames.map(async (themeName) => {
const themePath = getThemePath({themeName, plugins, typescript});
const swizzleConfig = getThemeSwizzleConfig(themeName, plugins);
const themeComponents = await getThemeComponents({
themeName,
themePath,
swizzleConfig,
});
return themeComponentsTable(themeComponents);
}),
)
).join('\n\n');
logger.info(`All theme components available to swizzle:
${themeComponentsTables}
${helpTables()}
`);
return process.exit(0);
}
async function ensureActionSafety({
componentName,
componentConfig,
action,
danger,
}: {
componentName: string;
componentConfig: SwizzleComponentConfig;
action: SwizzleAction;
danger: boolean;
}): Promise<void> {
const actionStatus = componentConfig.actions[action];
if (actionStatus === 'forbidden') {
logger.error`
Swizzle action name=${action} is forbidden for component name=${componentName}
`;
return process.exit(1);
}
if (actionStatus === 'unsafe' && !danger) {
logger.warn`
Swizzle action name=${action} is unsafe to perform on name=${componentName}.
It is more likely to be affected by breaking changes in the future
If you want to swizzle it, use the code=${'--danger'} flag, or confirm that you understand the risks.
`;
const swizzleDangerousComponent = await askSwizzleDangerousComponent();
if (!swizzleDangerousComponent) {
return process.exit(1);
}
}
return undefined;
}
export async function swizzle(
themeNameParam: string | undefined = undefined,
componentNameParam: string | undefined = undefined,
siteDirParam: string = '.',
optionsParam: Partial<SwizzleCLIOptions> = {},
): Promise<void> {
const siteDir = await fs.realpath(siteDirParam);
const options = normalizeOptions(optionsParam);
const {list, danger, typescript} = options;
const {plugins} = await initSwizzleContext(siteDir);
const themeNames = getThemeNames(plugins);
if (list && !themeNameParam) {
await listAllThemeComponents({themeNames, plugins, typescript});
}
const themeName = await getThemeName({themeNameParam, themeNames, list});
const themePath = getThemePath({themeName, plugins, typescript});
const swizzleConfig = getThemeSwizzleConfig(themeName, plugins);
const themeComponents = await getThemeComponents({
themeName,
themePath,
swizzleConfig,
});
const componentName = await getComponentName({
componentNameParam,
themeComponents,
list,
});
const componentConfig = themeComponents.getConfig(componentName);
const action = await getAction(componentConfig, options);
await ensureActionSafety({componentName, componentConfig, action, danger});
async function executeAction(): Promise<ActionResult> {
switch (action) {
case 'wrap': {
const result = await wrap({
siteDir,
themePath,
componentName,
typescript,
});
logger.success`
Created wrapper of name=${componentName} from name=${themeName} in path=${result.createdFiles}
`;
return result;
}
case 'eject': {
const result = await eject({
siteDir,
themePath,
componentName,
typescript,
});
logger.success`
Ejected name=${componentName} from name=${themeName} to path=${result.createdFiles}
`;
return result;
}
default:
throw new Error(`Unexpected action ${action}`);
}
}
await executeAction();
return process.exit(0);
}