|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import chalk from 'chalk'; |
9 | | -import type {ReportingSeverity} from '@docusaurus/types'; |
| 8 | +import OriginalLogger from './logger'; |
10 | 9 |
|
11 | | -type InterpolatableValue = string | number | (string | number)[]; |
| 10 | +export default OriginalLogger; |
12 | 11 |
|
13 | | -const path = (msg: unknown): string => chalk.cyan.underline(`"${String(msg)}"`); |
14 | | -const url = (msg: unknown): string => chalk.cyan.underline(msg); |
15 | | -const name = (msg: unknown): string => chalk.blue.bold(msg); |
16 | | -const code = (msg: unknown): string => chalk.cyan(`\`${String(msg)}\``); |
17 | | -const subdue = (msg: unknown): string => chalk.gray(msg); |
18 | | -const num = (msg: unknown): string => chalk.yellow(msg); |
| 12 | +// Extra named export to avoid problems in ESM modules |
| 13 | +// Notably: core .mjs CLI + create-docusaurus |
| 14 | +// See https://github.com/facebook/docusaurus/pull/6661 |
| 15 | +// See https://github.com/facebook/docusaurus/pull/7295 |
| 16 | +export const logger = OriginalLogger; |
19 | 17 |
|
20 | | -function interpolate( |
21 | | - msgs: TemplateStringsArray, |
22 | | - ...values: InterpolatableValue[] |
23 | | -): string { |
24 | | - let res = ''; |
25 | | - values.forEach((value, idx) => { |
26 | | - const flag = msgs[idx]!.match(/[a-z]+=$/); |
27 | | - res += msgs[idx]!.replace(/[a-z]+=$/, ''); |
28 | | - const format = (() => { |
29 | | - if (!flag) { |
30 | | - return (a: string | number) => a; |
31 | | - } |
32 | | - switch (flag[0]) { |
33 | | - case 'path=': |
34 | | - return path; |
35 | | - case 'url=': |
36 | | - return url; |
37 | | - case 'number=': |
38 | | - return num; |
39 | | - case 'name=': |
40 | | - return name; |
41 | | - case 'subdue=': |
42 | | - return subdue; |
43 | | - case 'code=': |
44 | | - return code; |
45 | | - default: |
46 | | - throw new Error( |
47 | | - 'Bad Docusaurus logging message. This is likely an internal bug, please report it.', |
48 | | - ); |
49 | | - } |
50 | | - })(); |
51 | | - res += Array.isArray(value) |
52 | | - ? `\n- ${value.map((v) => format(v)).join('\n- ')}` |
53 | | - : format(value); |
54 | | - }); |
55 | | - res += msgs.slice(-1)[0]; |
56 | | - return res; |
57 | | -} |
58 | | - |
59 | | -function stringify(msg: unknown): string { |
60 | | - if (String(msg) === '[object Object]') { |
61 | | - return JSON.stringify(msg); |
62 | | - } |
63 | | - if (msg instanceof Date) { |
64 | | - return msg.toUTCString(); |
65 | | - } |
66 | | - return String(msg); |
67 | | -} |
68 | | - |
69 | | -function info(msg: unknown): void; |
70 | | -function info( |
71 | | - msg: TemplateStringsArray, |
72 | | - ...values: [InterpolatableValue, ...InterpolatableValue[]] |
73 | | -): void; |
74 | | -function info(msg: unknown, ...values: InterpolatableValue[]): void { |
75 | | - console.info( |
76 | | - `${chalk.cyan.bold('[INFO]')} ${ |
77 | | - values.length === 0 |
78 | | - ? stringify(msg) |
79 | | - : interpolate(msg as TemplateStringsArray, ...values) |
80 | | - }`, |
81 | | - ); |
82 | | -} |
83 | | -function warn(msg: unknown): void; |
84 | | -function warn( |
85 | | - msg: TemplateStringsArray, |
86 | | - ...values: [InterpolatableValue, ...InterpolatableValue[]] |
87 | | -): void; |
88 | | -function warn(msg: unknown, ...values: InterpolatableValue[]): void { |
89 | | - console.warn( |
90 | | - chalk.yellow( |
91 | | - `${chalk.bold('[WARNING]')} ${ |
92 | | - values.length === 0 |
93 | | - ? stringify(msg) |
94 | | - : interpolate(msg as TemplateStringsArray, ...values) |
95 | | - }`, |
96 | | - ), |
97 | | - ); |
98 | | -} |
99 | | -function error(msg: unknown): void; |
100 | | -function error( |
101 | | - msg: TemplateStringsArray, |
102 | | - ...values: [InterpolatableValue, ...InterpolatableValue[]] |
103 | | -): void; |
104 | | -function error(msg: unknown, ...values: InterpolatableValue[]): void { |
105 | | - console.error( |
106 | | - chalk.red( |
107 | | - `${chalk.bold('[ERROR]')} ${ |
108 | | - values.length === 0 |
109 | | - ? stringify(msg) |
110 | | - : interpolate(msg as TemplateStringsArray, ...values) |
111 | | - }`, |
112 | | - ), |
113 | | - ); |
114 | | -} |
115 | | -function success(msg: unknown): void; |
116 | | -function success( |
117 | | - msg: TemplateStringsArray, |
118 | | - ...values: [InterpolatableValue, ...InterpolatableValue[]] |
119 | | -): void; |
120 | | -function success(msg: unknown, ...values: InterpolatableValue[]): void { |
121 | | - console.log( |
122 | | - `${chalk.green.bold('[SUCCESS]')} ${ |
123 | | - values.length === 0 |
124 | | - ? stringify(msg) |
125 | | - : interpolate(msg as TemplateStringsArray, ...values) |
126 | | - }`, |
127 | | - ); |
128 | | -} |
129 | | -function throwError(msg: unknown): void; |
130 | | -function throwError( |
131 | | - msg: TemplateStringsArray, |
132 | | - ...values: [InterpolatableValue, ...InterpolatableValue[]] |
133 | | -): void; |
134 | | -function throwError(msg: unknown, ...values: InterpolatableValue[]): void { |
135 | | - throw new Error( |
136 | | - values.length === 0 |
137 | | - ? stringify(msg) |
138 | | - : interpolate(msg as TemplateStringsArray, ...values), |
139 | | - ); |
140 | | -} |
141 | | - |
142 | | -function newLine(): void { |
143 | | - console.log(); |
144 | | -} |
145 | | - |
146 | | -/** |
147 | | - * Takes a message and reports it according to the severity that the user wants. |
148 | | - * |
149 | | - * - `ignore`: completely no-op |
150 | | - * - `log`: uses the `INFO` log level |
151 | | - * - `warn`: uses the `WARN` log level |
152 | | - * - `throw`: aborts the process, throws the error. |
153 | | - * |
154 | | - * Since the logger doesn't have logging level filters yet, these severities |
155 | | - * mostly just differ by their colors. |
156 | | - * |
157 | | - * @throws In addition to throwing when `reportingSeverity === "throw"`, this |
158 | | - * function also throws if `reportingSeverity` is not one of the above. |
159 | | - */ |
160 | | -function report(reportingSeverity: ReportingSeverity): typeof success { |
161 | | - const reportingMethods = { |
162 | | - ignore: () => {}, |
163 | | - log: info, |
164 | | - warn, |
165 | | - throw: throwError, |
166 | | - }; |
167 | | - if ( |
168 | | - !Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity) |
169 | | - ) { |
170 | | - throw new Error( |
171 | | - `Unexpected "reportingSeverity" value: ${reportingSeverity}.`, |
172 | | - ); |
173 | | - } |
174 | | - return reportingMethods[reportingSeverity]; |
175 | | -} |
176 | | - |
177 | | -const logger = { |
178 | | - red: (msg: string | number): string => chalk.red(msg), |
179 | | - yellow: (msg: string | number): string => chalk.yellow(msg), |
180 | | - green: (msg: string | number): string => chalk.green(msg), |
181 | | - bold: (msg: string | number): string => chalk.bold(msg), |
182 | | - dim: (msg: string | number): string => chalk.dim(msg), |
183 | | - path, |
184 | | - url, |
185 | | - name, |
186 | | - code, |
187 | | - subdue, |
188 | | - num, |
189 | | - interpolate, |
190 | | - info, |
191 | | - warn, |
192 | | - error, |
193 | | - success, |
194 | | - report, |
195 | | - newLine, |
196 | | -}; |
197 | | - |
198 | | -// TODO remove when migrating to ESM |
199 | | -// logger can only be default-imported in ESM with this |
200 | | -export = logger; |
| 18 | +export {PerfLogger} from './perfLogger'; |
0 commit comments