/
actions.ts
160 lines (136 loc) · 4.3 KB
/
actions.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
/**
* 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 path from 'path';
import _ from 'lodash';
import logger from '@docusaurus/logger';
import {Globby, posixPath, THEME_PATH} from '@docusaurus/utils';
import {askSwizzleAction} from './prompts';
import type {SwizzleAction, SwizzleComponentConfig} from '@docusaurus/types';
import type {SwizzleCLIOptions} from './common';
export const SwizzleActions: SwizzleAction[] = ['wrap', 'eject'];
export async function getAction(
componentConfig: SwizzleComponentConfig,
options: Pick<SwizzleCLIOptions, 'wrap' | 'eject'>,
): Promise<SwizzleAction> {
if (options.wrap) {
return 'wrap';
}
if (options.eject) {
return 'eject';
}
return askSwizzleAction(componentConfig);
}
export type ActionParams = {
siteDir: string;
themePath: string;
componentName: string;
typescript: boolean;
};
export type ActionResult = {
createdFiles: string[];
};
async function isDir(dirPath: string): Promise<boolean> {
return (
(await fs.pathExists(dirPath)) && (await fs.stat(dirPath)).isDirectory()
);
}
export async function eject({
siteDir,
themePath,
componentName,
typescript,
}: ActionParams): Promise<ActionResult> {
const fromPath = path.join(themePath, componentName);
const isDirectory = await isDir(fromPath);
const globPattern = isDirectory
? // Do we really want to copy all components?
path.join(fromPath, '*')
: `${fromPath}.*`;
const globPatternPosix = posixPath(globPattern);
const filesToCopy = await Globby(globPatternPosix, {
ignore: _.compact([
'**/*.{story,stories,test,tests}.{js,jsx,ts,tsx}',
// When ejecting JS components, we want to avoid emitting TS files
// In particular the .d.ts files that theme build output contains
typescript ? null : '**/*.{d.ts,ts,tsx}',
]),
});
if (filesToCopy.length === 0) {
// This should never happen
throw new Error(
logger.interpolate`No files to copy from path=${fromPath} with glob code=${globPatternPosix}`,
);
}
const toPath = path.join(siteDir, THEME_PATH);
await fs.ensureDir(toPath);
const createdFiles = await Promise.all(
filesToCopy.map(async (sourceFile: string) => {
const targetFile = path.join(
toPath,
path.relative(themePath, sourceFile),
);
try {
const fileContents = await fs.readFile(sourceFile, 'utf-8');
await fs.outputFile(
targetFile,
fileContents.trimStart().replace(/^\/\*.+?\*\/\s*/ms, ''),
);
} catch (err) {
logger.error`Could not copy file from path=${sourceFile} to path=${targetFile}`;
throw err;
}
return targetFile;
}),
);
return {createdFiles};
}
export async function wrap({
siteDir,
themePath,
componentName: themeComponentName,
typescript,
importType = 'original',
}: ActionParams & {
importType?: 'original' | 'init';
}): Promise<ActionResult> {
const isDirectory = await isDir(path.join(themePath, themeComponentName));
// Top/Parent/ComponentName => ComponentName
const componentName = _.last(themeComponentName.split('/'))!;
const wrapperComponentName = `${componentName}Wrapper`;
const wrapperFileName = `${themeComponentName}${isDirectory ? '/index' : ''}${
typescript ? '.tsx' : '.js'
}`;
await fs.ensureDir(path.resolve(siteDir, THEME_PATH));
const toPath = path.resolve(siteDir, THEME_PATH, wrapperFileName);
const content = typescript
? `import React from 'react';
import ${componentName} from '@theme-${importType}/${themeComponentName}';
import type ${componentName}Type from '@theme/${themeComponentName}';
import type {WrapperProps} from '@docusaurus/types';
type Props = WrapperProps<typeof ${componentName}Type>;
export default function ${wrapperComponentName}(props: Props): JSX.Element {
return (
<>
<${componentName} {...props} />
</>
);
}
`
: `import React from 'react';
import ${componentName} from '@theme-${importType}/${themeComponentName}';
export default function ${wrapperComponentName}(props) {
return (
<>
<${componentName} {...props} />
</>
);
}
`;
await fs.outputFile(toPath, content);
return {createdFiles: [toPath]};
}