/
common.ts
99 lines (85 loc) · 2.33 KB
/
common.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
/**
* 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 _ from 'lodash';
import logger from '@docusaurus/logger';
import leven from 'leven';
import type {NormalizedPluginConfig} from '../../server/plugins/configs';
import type {
InitializedPlugin,
SwizzleAction,
SwizzleActionStatus,
} from '@docusaurus/types';
export const SwizzleActions: SwizzleAction[] = ['wrap', 'eject'];
export const SwizzleActionsStatuses: SwizzleActionStatus[] = [
'safe',
'unsafe',
'forbidden',
];
export const PartiallySafeHint = logger.red('*');
export function actionStatusLabel(status: SwizzleActionStatus): string {
return _.capitalize(status);
}
const SwizzleActionStatusColors: {
[status in SwizzleActionStatus]: (str: string) => string;
} = {
safe: logger.green,
unsafe: logger.yellow,
forbidden: logger.red,
};
export function actionStatusColor(
status: SwizzleActionStatus,
str: string,
): string {
const colorFn = SwizzleActionStatusColors[status];
return colorFn(str);
}
export function actionStatusSuffix(
status: SwizzleActionStatus,
options: {partiallySafe?: boolean} = {},
): string {
return ` (${actionStatusColor(status, actionStatusLabel(status))}${
options.partiallySafe ? PartiallySafeHint : ''
})`;
}
export type SwizzlePlugin = {
instance: InitializedPlugin;
plugin: NormalizedPluginConfig;
};
export type SwizzleContext = {plugins: SwizzlePlugin[]};
export type SwizzleCLIOptions = {
typescript: boolean;
danger: boolean;
list: boolean;
wrap: boolean;
eject: boolean;
config?: string;
};
export function normalizeOptions(
options: Partial<SwizzleCLIOptions>,
): SwizzleCLIOptions {
return {
typescript: options.typescript ?? false,
danger: options.danger ?? false,
list: options.list ?? false,
wrap: options.wrap ?? false,
eject: options.eject ?? false,
config: options.config ?? undefined,
};
}
export function findStringIgnoringCase(
str: string,
values: string[],
): string | undefined {
return values.find((v) => v.toLowerCase() === str.toLowerCase());
}
export function findClosestValue(
str: string,
values: string[],
maxLevenshtein = 3,
): string | undefined {
return values.find((v) => leven(v, str) <= maxLevenshtein);
}