@docusaurus/logger 3.5.2 → 3.6.0-canary-6132

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.
package/lib/index.d.ts CHANGED
@@ -4,37 +4,13 @@
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 type { ReportingSeverity } from '@docusaurus/types';
8
- type InterpolatableValue = string | number | (string | number)[];
9
- declare function interpolate(msgs: TemplateStringsArray, ...values: InterpolatableValue[]): string;
10
- declare function info(msg: unknown): void;
11
- declare function info(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
12
- declare function warn(msg: unknown): void;
13
- declare function warn(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
14
- declare function error(msg: unknown): void;
15
- declare function error(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
16
- declare function success(msg: unknown): void;
17
- declare function success(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
18
- declare function newLine(): void;
19
- /**
20
- * Takes a message and reports it according to the severity that the user wants.
21
- *
22
- * - `ignore`: completely no-op
23
- * - `log`: uses the `INFO` log level
24
- * - `warn`: uses the `WARN` log level
25
- * - `throw`: aborts the process, throws the error.
26
- *
27
- * Since the logger doesn't have logging level filters yet, these severities
28
- * mostly just differ by their colors.
29
- *
30
- * @throws In addition to throwing when `reportingSeverity === "throw"`, this
31
- * function also throws if `reportingSeverity` is not one of the above.
32
- */
33
- declare function report(reportingSeverity: ReportingSeverity): typeof success;
34
- declare const logger: {
7
+ import OriginalLogger from './logger';
8
+ export default OriginalLogger;
9
+ export declare const logger: {
35
10
  red: (msg: string | number) => string;
36
11
  yellow: (msg: string | number) => string;
37
12
  green: (msg: string | number) => string;
13
+ cyan: (msg: string | number) => string;
38
14
  bold: (msg: string | number) => string;
39
15
  dim: (msg: string | number) => string;
40
16
  path: (msg: unknown) => string;
@@ -43,13 +19,28 @@ declare const logger: {
43
19
  code: (msg: unknown) => string;
44
20
  subdue: (msg: unknown) => string;
45
21
  num: (msg: unknown) => string;
46
- interpolate: typeof interpolate;
47
- info: typeof info;
48
- warn: typeof warn;
49
- error: typeof error;
50
- success: typeof success;
51
- report: typeof report;
52
- newLine: typeof newLine;
22
+ interpolate: (msgs: TemplateStringsArray, ...values: (string | number | (string | number)[])[]) => string;
23
+ info: {
24
+ (msg: unknown): void;
25
+ (msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void;
26
+ };
27
+ warn: {
28
+ (msg: unknown): void;
29
+ (msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void;
30
+ };
31
+ error: {
32
+ (msg: unknown): void;
33
+ (msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void;
34
+ };
35
+ success: {
36
+ (msg: unknown): void;
37
+ (msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void;
38
+ };
39
+ report: (reportingSeverity: import("@docusaurus/types").ReportingSeverity) => {
40
+ (msg: unknown): void;
41
+ (msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void;
42
+ };
43
+ newLine: () => void;
53
44
  };
54
- export = logger;
45
+ export { PerfLogger } from './perfLogger';
55
46
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAEzD,KAAK,mBAAmB,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;AASjE,iBAAS,WAAW,CAClB,IAAI,EAAE,oBAAoB,EAC1B,GAAG,MAAM,EAAE,mBA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AChB,OAAO,KAAG,MAAM;kBACd,OAAO,KAAG,MAAM;eACnB,OAAO,KAAG,MAAM;;;;;;;;CAkLjC,CAAC;AAIF,SAAS,MAAM,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,cAAc,MAAM,UAAU,CAAC;AAEtC,eAAe,cAAc,CAAC;AAM9B,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAiB,CAAC;AAErC,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC"}
package/lib/index.js CHANGED
@@ -5,129 +5,16 @@
5
5
  * This source code is licensed under the MIT license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.PerfLogger = exports.logger = void 0;
8
10
  const tslib_1 = require("tslib");
9
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
10
- const path = (msg) => chalk_1.default.cyan.underline(`"${String(msg)}"`);
11
- const url = (msg) => chalk_1.default.cyan.underline(msg);
12
- const name = (msg) => chalk_1.default.blue.bold(msg);
13
- const code = (msg) => chalk_1.default.cyan(`\`${String(msg)}\``);
14
- const subdue = (msg) => chalk_1.default.gray(msg);
15
- const num = (msg) => chalk_1.default.yellow(msg);
16
- function interpolate(msgs, ...values) {
17
- let res = '';
18
- values.forEach((value, idx) => {
19
- const flag = msgs[idx].match(/[a-z]+=$/);
20
- res += msgs[idx].replace(/[a-z]+=$/, '');
21
- const format = (() => {
22
- if (!flag) {
23
- return (a) => a;
24
- }
25
- switch (flag[0]) {
26
- case 'path=':
27
- return path;
28
- case 'url=':
29
- return url;
30
- case 'number=':
31
- return num;
32
- case 'name=':
33
- return name;
34
- case 'subdue=':
35
- return subdue;
36
- case 'code=':
37
- return code;
38
- default:
39
- throw new Error('Bad Docusaurus logging message. This is likely an internal bug, please report it.');
40
- }
41
- })();
42
- res += Array.isArray(value)
43
- ? `\n- ${value.map((v) => format(v)).join('\n- ')}`
44
- : format(value);
45
- });
46
- res += msgs.slice(-1)[0];
47
- return res;
48
- }
49
- function stringify(msg) {
50
- if (String(msg) === '[object Object]') {
51
- return JSON.stringify(msg);
52
- }
53
- if (msg instanceof Date) {
54
- return msg.toUTCString();
55
- }
56
- return String(msg);
57
- }
58
- function info(msg, ...values) {
59
- console.info(`${chalk_1.default.cyan.bold('[INFO]')} ${values.length === 0
60
- ? stringify(msg)
61
- : interpolate(msg, ...values)}`);
62
- }
63
- function warn(msg, ...values) {
64
- console.warn(chalk_1.default.yellow(`${chalk_1.default.bold('[WARNING]')} ${values.length === 0
65
- ? stringify(msg)
66
- : interpolate(msg, ...values)}`));
67
- }
68
- function error(msg, ...values) {
69
- console.error(chalk_1.default.red(`${chalk_1.default.bold('[ERROR]')} ${values.length === 0
70
- ? stringify(msg)
71
- : interpolate(msg, ...values)}`));
72
- }
73
- function success(msg, ...values) {
74
- console.log(`${chalk_1.default.green.bold('[SUCCESS]')} ${values.length === 0
75
- ? stringify(msg)
76
- : interpolate(msg, ...values)}`);
77
- }
78
- function throwError(msg, ...values) {
79
- throw new Error(values.length === 0
80
- ? stringify(msg)
81
- : interpolate(msg, ...values));
82
- }
83
- function newLine() {
84
- console.log();
85
- }
86
- /**
87
- * Takes a message and reports it according to the severity that the user wants.
88
- *
89
- * - `ignore`: completely no-op
90
- * - `log`: uses the `INFO` log level
91
- * - `warn`: uses the `WARN` log level
92
- * - `throw`: aborts the process, throws the error.
93
- *
94
- * Since the logger doesn't have logging level filters yet, these severities
95
- * mostly just differ by their colors.
96
- *
97
- * @throws In addition to throwing when `reportingSeverity === "throw"`, this
98
- * function also throws if `reportingSeverity` is not one of the above.
99
- */
100
- function report(reportingSeverity) {
101
- const reportingMethods = {
102
- ignore: () => { },
103
- log: info,
104
- warn,
105
- throw: throwError,
106
- };
107
- if (!Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity)) {
108
- throw new Error(`Unexpected "reportingSeverity" value: ${reportingSeverity}.`);
109
- }
110
- return reportingMethods[reportingSeverity];
111
- }
112
- const logger = {
113
- red: (msg) => chalk_1.default.red(msg),
114
- yellow: (msg) => chalk_1.default.yellow(msg),
115
- green: (msg) => chalk_1.default.green(msg),
116
- bold: (msg) => chalk_1.default.bold(msg),
117
- dim: (msg) => chalk_1.default.dim(msg),
118
- path,
119
- url,
120
- name,
121
- code,
122
- subdue,
123
- num,
124
- interpolate,
125
- info,
126
- warn,
127
- error,
128
- success,
129
- report,
130
- newLine,
131
- };
132
- module.exports = logger;
11
+ const logger_1 = tslib_1.__importDefault(require("./logger"));
12
+ exports.default = logger_1.default;
13
+ // Extra named export to avoid problems in ESM modules
14
+ // Notably: core .mjs CLI + create-docusaurus
15
+ // See https://github.com/facebook/docusaurus/pull/6661
16
+ // See https://github.com/facebook/docusaurus/pull/7295
17
+ exports.logger = logger_1.default;
18
+ var perfLogger_1 = require("./perfLogger");
19
+ Object.defineProperty(exports, "PerfLogger", { enumerable: true, get: function () { return perfLogger_1.PerfLogger; } });
133
20
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,0DAA0B;AAK1B,MAAM,IAAI,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAChF,MAAM,GAAG,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AAChE,MAAM,IAAI,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC5D,MAAM,IAAI,GAAG,CAAC,GAAY,EAAU,EAAE,CAA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,IAAI,KAAK,CACb,yCAAyC,iBAAiB,GAAG,CAC9D,CAAC;IACJ,CAAC;IACD,OAAO,gBAAgB,CAAC,iBAAiB,CAAC,CAAC;AAC7C,CAAC;AAED,MAAM,MAAM,GAAG;IACb,GAAG,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC;IACrD,MAAM,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,MAAM,CAAC,GAAG,CAAC;IAC3D,KAAK,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,KAAK,CAAC,GAAG,CAAC;IACzD,IAAI,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,GAAG,CAAC;IACvD,GAAG,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC;IACrD,IAAI;IACJ,GAAG;IACH,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,GAAG;IACH,WAAW;IACX,IAAI;IACJ,IAAI;IACJ,KAAK;IACL,OAAO;IACP,MAAM;IACN,OAAO;CACR,CAAC;AAIF,iBAAS,MAAM,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,8DAAsC;AAEtC,kBAAe,gBAAc,CAAC;AAE9B,sDAAsD;AACtD,6CAA6C;AAC7C,uDAAuD;AACvD,uDAAuD;AAC1C,QAAA,MAAM,GAAG,gBAAc,CAAC;AAErC,2CAAwC;AAAhC,wGAAA,UAAU,OAAA"}
@@ -0,0 +1,56 @@
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 { ReportingSeverity } from '@docusaurus/types';
8
+ type InterpolatableValue = string | number | (string | number)[];
9
+ declare function interpolate(msgs: TemplateStringsArray, ...values: InterpolatableValue[]): string;
10
+ declare function info(msg: unknown): void;
11
+ declare function info(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
12
+ declare function warn(msg: unknown): void;
13
+ declare function warn(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
14
+ declare function error(msg: unknown): void;
15
+ declare function error(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
16
+ declare function success(msg: unknown): void;
17
+ declare function success(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void;
18
+ declare function newLine(): void;
19
+ /**
20
+ * Takes a message and reports it according to the severity that the user wants.
21
+ *
22
+ * - `ignore`: completely no-op
23
+ * - `log`: uses the `INFO` log level
24
+ * - `warn`: uses the `WARN` log level
25
+ * - `throw`: aborts the process, throws the error.
26
+ *
27
+ * Since the logger doesn't have logging level filters yet, these severities
28
+ * mostly just differ by their colors.
29
+ *
30
+ * @throws In addition to throwing when `reportingSeverity === "throw"`, this
31
+ * function also throws if `reportingSeverity` is not one of the above.
32
+ */
33
+ declare function report(reportingSeverity: ReportingSeverity): typeof success;
34
+ declare const logger: {
35
+ red: (msg: string | number) => string;
36
+ yellow: (msg: string | number) => string;
37
+ green: (msg: string | number) => string;
38
+ cyan: (msg: string | number) => string;
39
+ bold: (msg: string | number) => string;
40
+ dim: (msg: string | number) => string;
41
+ path: (msg: unknown) => string;
42
+ url: (msg: unknown) => string;
43
+ name: (msg: unknown) => string;
44
+ code: (msg: unknown) => string;
45
+ subdue: (msg: unknown) => string;
46
+ num: (msg: unknown) => string;
47
+ interpolate: typeof interpolate;
48
+ info: typeof info;
49
+ warn: typeof warn;
50
+ error: typeof error;
51
+ success: typeof success;
52
+ report: typeof report;
53
+ newLine: typeof newLine;
54
+ };
55
+ export default logger;
56
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d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CAepE;AAED,QAAA,MAAM,MAAM;eACC,MAAM,GAAG,MAAM,KAAG,MAAM;kBACrB,MAAM,GAAG,MAAM,KAAG,MAAM;iBACzB,MAAM,GAAG,MAAM,KAAG,MAAM;gBACzB,MAAM,GAAG,MAAM,KAAG,MAAM;gBACxB,MAAM,GAAG,MAAM,KAAG,MAAM;eACzB,MAAM,GAAG,MAAM,KAAG,MAAM;gBA1KlB,OAAO,KAAG,MAAM;eACjB,OAAO,KAAG,MAAM;gBACf,OAAO,KAAG,MAAM;gBAChB,OAAO,KAAG,MAAM;kBACd,OAAO,KAAG,MAAM;eACnB,OAAO,KAAG,MAAM;;;;;;;;CAmLjC,CAAC;AAEF,eAAe,MAAM,CAAC"}
package/lib/logger.js ADDED
@@ -0,0 +1,135 @@
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
+ const tslib_1 = require("tslib");
10
+ const chalk_1 = tslib_1.__importDefault(require("chalk"));
11
+ const path = (msg) => chalk_1.default.cyan.underline(`"${String(msg)}"`);
12
+ const url = (msg) => chalk_1.default.cyan.underline(msg);
13
+ const name = (msg) => chalk_1.default.blue.bold(msg);
14
+ const code = (msg) => chalk_1.default.cyan(`\`${String(msg)}\``);
15
+ const subdue = (msg) => chalk_1.default.gray(msg);
16
+ const num = (msg) => chalk_1.default.yellow(msg);
17
+ function interpolate(msgs, ...values) {
18
+ let res = '';
19
+ values.forEach((value, idx) => {
20
+ const flag = msgs[idx].match(/[a-z]+=$/);
21
+ res += msgs[idx].replace(/[a-z]+=$/, '');
22
+ const format = (() => {
23
+ if (!flag) {
24
+ return (a) => a;
25
+ }
26
+ switch (flag[0]) {
27
+ case 'path=':
28
+ return path;
29
+ case 'url=':
30
+ return url;
31
+ case 'number=':
32
+ return num;
33
+ case 'name=':
34
+ return name;
35
+ case 'subdue=':
36
+ return subdue;
37
+ case 'code=':
38
+ return code;
39
+ default:
40
+ throw new Error('Bad Docusaurus logging message. This is likely an internal bug, please report it.');
41
+ }
42
+ })();
43
+ res += Array.isArray(value)
44
+ ? `\n- ${value.map((v) => format(v)).join('\n- ')}`
45
+ : format(value);
46
+ });
47
+ res += msgs.slice(-1)[0];
48
+ return res;
49
+ }
50
+ function stringify(msg) {
51
+ if (String(msg) === '[object Object]') {
52
+ return JSON.stringify(msg);
53
+ }
54
+ if (msg instanceof Date) {
55
+ return msg.toUTCString();
56
+ }
57
+ return String(msg);
58
+ }
59
+ function info(msg, ...values) {
60
+ console.info(`${chalk_1.default.cyan.bold('[INFO]')} ${values.length === 0
61
+ ? stringify(msg)
62
+ : interpolate(msg, ...values)}`);
63
+ }
64
+ function warn(msg, ...values) {
65
+ console.warn(chalk_1.default.yellow(`${chalk_1.default.bold('[WARNING]')} ${values.length === 0
66
+ ? stringify(msg)
67
+ : interpolate(msg, ...values)}`));
68
+ }
69
+ function error(msg, ...values) {
70
+ console.error(chalk_1.default.red(`${chalk_1.default.bold('[ERROR]')} ${values.length === 0
71
+ ? stringify(msg)
72
+ : interpolate(msg, ...values)}`));
73
+ }
74
+ function success(msg, ...values) {
75
+ console.log(`${chalk_1.default.green.bold('[SUCCESS]')} ${values.length === 0
76
+ ? stringify(msg)
77
+ : interpolate(msg, ...values)}`);
78
+ }
79
+ function throwError(msg, ...values) {
80
+ throw new Error(values.length === 0
81
+ ? stringify(msg)
82
+ : interpolate(msg, ...values));
83
+ }
84
+ function newLine() {
85
+ console.log();
86
+ }
87
+ /**
88
+ * Takes a message and reports it according to the severity that the user wants.
89
+ *
90
+ * - `ignore`: completely no-op
91
+ * - `log`: uses the `INFO` log level
92
+ * - `warn`: uses the `WARN` log level
93
+ * - `throw`: aborts the process, throws the error.
94
+ *
95
+ * Since the logger doesn't have logging level filters yet, these severities
96
+ * mostly just differ by their colors.
97
+ *
98
+ * @throws In addition to throwing when `reportingSeverity === "throw"`, this
99
+ * function also throws if `reportingSeverity` is not one of the above.
100
+ */
101
+ function report(reportingSeverity) {
102
+ const reportingMethods = {
103
+ ignore: () => { },
104
+ log: info,
105
+ warn,
106
+ throw: throwError,
107
+ };
108
+ if (!Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity)) {
109
+ throw new Error(`Unexpected "reportingSeverity" value: ${reportingSeverity}.`);
110
+ }
111
+ return reportingMethods[reportingSeverity];
112
+ }
113
+ const logger = {
114
+ red: (msg) => chalk_1.default.red(msg),
115
+ yellow: (msg) => chalk_1.default.yellow(msg),
116
+ green: (msg) => chalk_1.default.green(msg),
117
+ cyan: (msg) => chalk_1.default.cyan(msg),
118
+ bold: (msg) => chalk_1.default.bold(msg),
119
+ dim: (msg) => chalk_1.default.dim(msg),
120
+ path,
121
+ url,
122
+ name,
123
+ code,
124
+ subdue,
125
+ num,
126
+ interpolate,
127
+ info,
128
+ warn,
129
+ error,
130
+ success,
131
+ report,
132
+ newLine,
133
+ };
134
+ exports.default = logger;
135
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,0DAA0B;AAK1B,MAAM,IAAI,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAChF,MAAM,GAAG,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AAChE,MAAM,IAAI,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC5D,MAAM,IAAI,GAAG,CAAC,GAAY,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,KAAK,MAAM,CAAC,GAA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CAAC;IACJ,CAAC;IACD,OAAO,gBAAgB,CAAC,iBAAiB,CAAC,CAAC;AAC7C,CAAC;AAED,MAAM,MAAM,GAAG;IACb,GAAG,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC;IACrD,MAAM,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,MAAM,CAAC,GAAG,CAAC;IAC3D,KAAK,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,KAAK,CAAC,GAAG,CAAC;IACzD,IAAI,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,GAAG,CAAC;IACvD,IAAI,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,IAAI,CAAC,GAAG,CAAC;IACvD,GAAG,EAAE,CAAC,GAAoB,EAAU,EAAE,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC;IACrD,IAAI;IACJ,GAAG;IACH,IAAI;IACJ,IAAI;IACJ,MAAM;IACN,GAAG;IACH,WAAW;IACX,IAAI;IACJ,IAAI;IACJ,KAAK;IACL,OAAO;IACP,MAAM;IACN,OAAO;CACR,CAAC;AAEF,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,9 @@
1
+ type PerfLoggerAPI = {
2
+ start: (label: string) => void;
3
+ end: (label: string) => void;
4
+ log: (message: string) => void;
5
+ async: <Result>(label: string, asyncFn: () => Result | Promise<Result>) => Promise<Result>;
6
+ };
7
+ export declare const PerfLogger: PerfLoggerAPI;
8
+ export {};
9
+ //# sourceMappingURL=perfLogger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"perfLogger.d.ts","sourceRoot":"","sources":["../src/perfLogger.ts"],"names":[],"mappings":"AA8BA,KAAK,aAAa,GAAG;IACnB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,KAAK,EAAE,CAAC,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,KACpC,OAAO,CAAC,MAAM,CAAC,CAAC;CACtB,CAAC;AAuIF,eAAO,MAAM,UAAU,EAAE,aAAkC,CAAC"}
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PerfLogger = void 0;
4
+ const tslib_1 = require("tslib");
5
+ /**
6
+ * Copyright (c) Facebook, Inc. and its affiliates.
7
+ *
8
+ * This source code is licensed under the MIT license found in the
9
+ * LICENSE file in the root directory of this source tree.
10
+ */
11
+ const async_hooks_1 = require("async_hooks");
12
+ const logger_1 = tslib_1.__importDefault(require("./logger"));
13
+ // For now this is a private env variable we use internally
14
+ // But we'll want to expose this feature officially some day
15
+ const PerfDebuggingEnabled = process.env.DOCUSAURUS_PERF_LOGGER === 'true';
16
+ const Thresholds = {
17
+ min: 5,
18
+ yellow: 100,
19
+ red: 1000,
20
+ };
21
+ const PerfPrefix = logger_1.default.yellow(`[PERF]`);
22
+ // This is what enables to "see the parent stack" for each log
23
+ // Parent1 > Parent2 > Parent3 > child trace
24
+ const ParentPrefix = new async_hooks_1.AsyncLocalStorage();
25
+ function applyParentPrefix(label) {
26
+ const parentPrefix = ParentPrefix.getStore();
27
+ return parentPrefix ? `${parentPrefix} > ${label}` : label;
28
+ }
29
+ function getMemory() {
30
+ // Before reading memory stats, we explicitly call the GC
31
+ // Note: this only works when Node.js option "--expose-gc" is provided
32
+ globalThis.gc?.();
33
+ return process.memoryUsage();
34
+ }
35
+ function createPerfLogger() {
36
+ if (!PerfDebuggingEnabled) {
37
+ const noop = () => { };
38
+ return {
39
+ start: noop,
40
+ end: noop,
41
+ log: noop,
42
+ async: async (_label, asyncFn) => asyncFn(),
43
+ };
44
+ }
45
+ const formatDuration = (duration) => {
46
+ if (duration > Thresholds.red) {
47
+ return logger_1.default.red(`${(duration / 1000).toFixed(2)} seconds!`);
48
+ }
49
+ else if (duration > Thresholds.yellow) {
50
+ return logger_1.default.yellow(`${duration.toFixed(2)} ms`);
51
+ }
52
+ else {
53
+ return logger_1.default.green(`${duration.toFixed(2)} ms`);
54
+ }
55
+ };
56
+ const formatMemory = (memory) => {
57
+ const fmtHead = (bytes) => logger_1.default.cyan(`${(bytes / 1000000).toFixed(0)}mb`);
58
+ return logger_1.default.dim(`(${fmtHead(memory.before.heapUsed)} -> ${fmtHead(memory.after.heapUsed)})`);
59
+ };
60
+ const formatStatus = (error) => {
61
+ return error ? logger_1.default.red('[KO]') : ''; // logger.green('[OK]');
62
+ };
63
+ const printPerfLog = ({ label, duration, memory, error, }) => {
64
+ if (duration < Thresholds.min) {
65
+ return;
66
+ }
67
+ console.log(`${PerfPrefix}${formatStatus(error)} ${label} - ${formatDuration(duration)} - ${formatMemory(memory)}`);
68
+ };
69
+ const start = (label) => performance.mark(label, {
70
+ detail: {
71
+ memoryUsage: getMemory(),
72
+ },
73
+ });
74
+ const end = (label) => {
75
+ const { duration, detail: { memoryUsage }, } = performance.measure(label);
76
+ performance.clearMarks(label);
77
+ printPerfLog({
78
+ label: applyParentPrefix(label),
79
+ duration,
80
+ memory: {
81
+ before: memoryUsage,
82
+ after: getMemory(),
83
+ },
84
+ error: undefined,
85
+ });
86
+ };
87
+ const log = (label) => console.log(`${PerfPrefix} ${applyParentPrefix(label)}`);
88
+ const async = async (label, asyncFn) => {
89
+ const finalLabel = applyParentPrefix(label);
90
+ const before = performance.now();
91
+ const memoryBefore = getMemory();
92
+ const asyncEnd = ({ error }) => {
93
+ const memoryAfter = getMemory();
94
+ const duration = performance.now() - before;
95
+ printPerfLog({
96
+ error,
97
+ label: finalLabel,
98
+ duration,
99
+ memory: {
100
+ before: memoryBefore,
101
+ after: memoryAfter,
102
+ },
103
+ });
104
+ };
105
+ try {
106
+ const result = await ParentPrefix.run(finalLabel, () => asyncFn());
107
+ asyncEnd({ error: undefined });
108
+ return result;
109
+ }
110
+ catch (e) {
111
+ asyncEnd({ error: e });
112
+ throw e;
113
+ }
114
+ };
115
+ return {
116
+ start,
117
+ end,
118
+ log,
119
+ async,
120
+ };
121
+ }
122
+ exports.PerfLogger = createPerfLogger();
123
+ //# sourceMappingURL=perfLogger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"perfLogger.js","sourceRoot":"","sources":["../src/perfLogger.ts"],"names":[],"mappings":";;;;AAAA;;;;;GAKG;AACH,6CAA8C;AAC9C,8DAA8B;AAE9B,2DAA2D;AAC3D,4DAA4D;AAC5D,MAAM,oBAAoB,GACxB,OAAO,CAAC,GAAG,CAAC,sBAAsB,KAAK,MAAM,CAAC;AAEhD,MAAM,UAAU,GAAG;IACjB,GAAG,EAAE,CAAC;IACN,MAAM,EAAE,GAAG;IACX,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,MAAM,UAAU,GAAG,gBAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAE3C,8DAA8D;AAC9D,4CAA4C;AAC5C,MAAM,YAAY,GAAG,IAAI,+BAAiB,EAAU,CAAC;AACrD,SAAS,iBAAiB,CAAC,KAAa;IACtC,MAAM,YAAY,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC;IAC7C,OAAO,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7D,CAAC;AAiBD,SAAS,SAAS;IAChB,yDAAyD;IACzD,sEAAsE;IACtE,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC;IAElB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED,SAAS,gBAAgB;IACvB,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,MAAM,IAAI,GAAG,GAAG,EAAE,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O,EAAE,EAAE;QAC7D,MAAM,UAAU,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAC5C,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QACjC,MAAM,YAAY,GAAG,SAAS,EAAE,CAAC;QAEjC,MAAM,QAAQ,GAAG,CAAC,EAAC,KAAK,EAA6B,EAAE,EAAE;YACvD,MAAM,WAAW,GAAG,SAAS,EAAE,CAAC;YAChC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC;YAC5C,YAAY,CAAC;gBACX,KAAK;gBACL,KAAK,EAAE,UAAU;gBACjB,QAAQ;gBACR,MAAM,EAAE;oBACN,MAAM,EAAE,YAAY;oBACpB,KAAK,EAAE,WAAW;iBACnB;aACF,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;YACnE,QAAQ,CAAC,EAAC,KAAK,EAAE,SAAS,EAAC,CAAC,CAAC;YAC7B,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,QAAQ,CAAC,EAAC,KAAK,EAAE,CAAU,EAAC,CAAC,CAAC;YAC9B,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,KAAK;QACL,GAAG;QACH,GAAG;QACH,KAAK;KACN,CAAC;AACJ,CAAC;AAEY,QAAA,UAAU,GAAkB,gBAAgB,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/logger",
3
- "version": "3.5.2",
3
+ "version": "3.6.0-canary-6132",
4
4
  "description": "An encapsulated logger for semantically formatting console messages.",
5
5
  "main": "./lib/index.js",
6
6
  "repository": {
@@ -29,5 +29,5 @@
29
29
  "devDependencies": {
30
30
  "@types/supports-color": "^8.1.1"
31
31
  },
32
- "gitHead": "eeec303dd773774ed5a023884800da0b061f6942"
32
+ "gitHead": "4930a868b86cafcb0786e2ddc6d400459127c4f6"
33
33
  }
package/src/index.ts CHANGED
@@ -5,196 +5,14 @@
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';
package/src/logger.ts ADDED
@@ -0,0 +1,199 @@
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
+
8
+ import chalk from 'chalk';
9
+ import type {ReportingSeverity} from '@docusaurus/types';
10
+
11
+ type InterpolatableValue = string | number | (string | number)[];
12
+
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);
19
+
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
+ cyan: (msg: string | number): string => chalk.cyan(msg),
182
+ bold: (msg: string | number): string => chalk.bold(msg),
183
+ dim: (msg: string | number): string => chalk.dim(msg),
184
+ path,
185
+ url,
186
+ name,
187
+ code,
188
+ subdue,
189
+ num,
190
+ interpolate,
191
+ info,
192
+ warn,
193
+ error,
194
+ success,
195
+ report,
196
+ newLine,
197
+ };
198
+
199
+ export default logger;
@@ -0,0 +1,174 @@
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 {AsyncLocalStorage} from 'async_hooks';
8
+ import logger from './logger';
9
+
10
+ // For now this is a private env variable we use internally
11
+ // But we'll want to expose this feature officially some day
12
+ const PerfDebuggingEnabled: boolean =
13
+ process.env.DOCUSAURUS_PERF_LOGGER === 'true';
14
+
15
+ const Thresholds = {
16
+ min: 5,
17
+ yellow: 100,
18
+ red: 1000,
19
+ };
20
+
21
+ const PerfPrefix = logger.yellow(`[PERF]`);
22
+
23
+ // This is what enables to "see the parent stack" for each log
24
+ // Parent1 > Parent2 > Parent3 > child trace
25
+ const ParentPrefix = new AsyncLocalStorage<string>();
26
+ function applyParentPrefix(label: string) {
27
+ const parentPrefix = ParentPrefix.getStore();
28
+ return parentPrefix ? `${parentPrefix} > ${label}` : label;
29
+ }
30
+
31
+ type PerfLoggerAPI = {
32
+ start: (label: string) => void;
33
+ end: (label: string) => void;
34
+ log: (message: string) => void;
35
+ async: <Result>(
36
+ label: string,
37
+ asyncFn: () => Result | Promise<Result>,
38
+ ) => Promise<Result>;
39
+ };
40
+
41
+ type Memory = {
42
+ before: NodeJS.MemoryUsage;
43
+ after: NodeJS.MemoryUsage;
44
+ };
45
+
46
+ function getMemory(): NodeJS.MemoryUsage {
47
+ // Before reading memory stats, we explicitly call the GC
48
+ // Note: this only works when Node.js option "--expose-gc" is provided
49
+ globalThis.gc?.();
50
+
51
+ return process.memoryUsage();
52
+ }
53
+
54
+ function createPerfLogger(): PerfLoggerAPI {
55
+ if (!PerfDebuggingEnabled) {
56
+ const noop = () => {};
57
+ return {
58
+ start: noop,
59
+ end: noop,
60
+ log: noop,
61
+ async: async (_label, asyncFn) => asyncFn(),
62
+ };
63
+ }
64
+
65
+ const formatDuration = (duration: number): string => {
66
+ if (duration > Thresholds.red) {
67
+ return logger.red(`${(duration / 1000).toFixed(2)} seconds!`);
68
+ } else if (duration > Thresholds.yellow) {
69
+ return logger.yellow(`${duration.toFixed(2)} ms`);
70
+ } else {
71
+ return logger.green(`${duration.toFixed(2)} ms`);
72
+ }
73
+ };
74
+
75
+ const formatMemory = (memory: Memory): string => {
76
+ const fmtHead = (bytes: number) =>
77
+ logger.cyan(`${(bytes / 1000000).toFixed(0)}mb`);
78
+ return logger.dim(
79
+ `(${fmtHead(memory.before.heapUsed)} -> ${fmtHead(
80
+ memory.after.heapUsed,
81
+ )})`,
82
+ );
83
+ };
84
+
85
+ const formatStatus = (error: Error | undefined): string => {
86
+ return error ? logger.red('[KO]') : ''; // logger.green('[OK]');
87
+ };
88
+
89
+ const printPerfLog = ({
90
+ label,
91
+ duration,
92
+ memory,
93
+ error,
94
+ }: {
95
+ label: string;
96
+ duration: number;
97
+ memory: Memory;
98
+ error: Error | undefined;
99
+ }) => {
100
+ if (duration < Thresholds.min) {
101
+ return;
102
+ }
103
+ console.log(
104
+ `${PerfPrefix}${formatStatus(error)} ${label} - ${formatDuration(
105
+ duration,
106
+ )} - ${formatMemory(memory)}`,
107
+ );
108
+ };
109
+
110
+ const start: PerfLoggerAPI['start'] = (label) =>
111
+ performance.mark(label, {
112
+ detail: {
113
+ memoryUsage: getMemory(),
114
+ },
115
+ });
116
+
117
+ const end: PerfLoggerAPI['end'] = (label) => {
118
+ const {
119
+ duration,
120
+ detail: {memoryUsage},
121
+ } = performance.measure(label);
122
+ performance.clearMarks(label);
123
+ printPerfLog({
124
+ label: applyParentPrefix(label),
125
+ duration,
126
+ memory: {
127
+ before: memoryUsage,
128
+ after: getMemory(),
129
+ },
130
+ error: undefined,
131
+ });
132
+ };
133
+
134
+ const log: PerfLoggerAPI['log'] = (label: string) =>
135
+ console.log(`${PerfPrefix} ${applyParentPrefix(label)}`);
136
+
137
+ const async: PerfLoggerAPI['async'] = async (label, asyncFn) => {
138
+ const finalLabel = applyParentPrefix(label);
139
+ const before = performance.now();
140
+ const memoryBefore = getMemory();
141
+
142
+ const asyncEnd = ({error}: {error: Error | undefined}) => {
143
+ const memoryAfter = getMemory();
144
+ const duration = performance.now() - before;
145
+ printPerfLog({
146
+ error,
147
+ label: finalLabel,
148
+ duration,
149
+ memory: {
150
+ before: memoryBefore,
151
+ after: memoryAfter,
152
+ },
153
+ });
154
+ };
155
+
156
+ try {
157
+ const result = await ParentPrefix.run(finalLabel, () => asyncFn());
158
+ asyncEnd({error: undefined});
159
+ return result;
160
+ } catch (e) {
161
+ asyncEnd({error: e as Error});
162
+ throw e;
163
+ }
164
+ };
165
+
166
+ return {
167
+ start,
168
+ end,
169
+ log,
170
+ async,
171
+ };
172
+ }
173
+
174
+ export const PerfLogger: PerfLoggerAPI = createPerfLogger();