@docusaurus/logger
Advanced tools
Comparing version 3.5.2 to 3.6.0-canary-6132
@@ -7,33 +7,9 @@ /** | ||
*/ | ||
import type { ReportingSeverity } from '@docusaurus/types'; | ||
type InterpolatableValue = string | number | (string | number)[]; | ||
declare function interpolate(msgs: TemplateStringsArray, ...values: InterpolatableValue[]): string; | ||
declare function info(msg: unknown): void; | ||
declare function info(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void; | ||
declare function warn(msg: unknown): void; | ||
declare function warn(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void; | ||
declare function error(msg: unknown): void; | ||
declare function error(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void; | ||
declare function success(msg: unknown): void; | ||
declare function success(msg: TemplateStringsArray, ...values: [InterpolatableValue, ...InterpolatableValue[]]): void; | ||
declare function newLine(): void; | ||
/** | ||
* Takes a message and reports it according to the severity that the user wants. | ||
* | ||
* - `ignore`: completely no-op | ||
* - `log`: uses the `INFO` log level | ||
* - `warn`: uses the `WARN` log level | ||
* - `throw`: aborts the process, throws the error. | ||
* | ||
* Since the logger doesn't have logging level filters yet, these severities | ||
* mostly just differ by their colors. | ||
* | ||
* @throws In addition to throwing when `reportingSeverity === "throw"`, this | ||
* function also throws if `reportingSeverity` is not one of the above. | ||
*/ | ||
declare function report(reportingSeverity: ReportingSeverity): typeof success; | ||
declare const logger: { | ||
import OriginalLogger from './logger'; | ||
export default OriginalLogger; | ||
export declare const logger: { | ||
red: (msg: string | number) => string; | ||
yellow: (msg: string | number) => string; | ||
green: (msg: string | number) => string; | ||
cyan: (msg: string | number) => string; | ||
bold: (msg: string | number) => string; | ||
@@ -47,11 +23,26 @@ dim: (msg: string | number) => string; | ||
num: (msg: unknown) => string; | ||
interpolate: typeof interpolate; | ||
info: typeof info; | ||
warn: typeof warn; | ||
error: typeof error; | ||
success: typeof success; | ||
report: typeof report; | ||
newLine: typeof newLine; | ||
interpolate: (msgs: TemplateStringsArray, ...values: (string | number | (string | number)[])[]) => string; | ||
info: { | ||
(msg: unknown): void; | ||
(msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void; | ||
}; | ||
warn: { | ||
(msg: unknown): void; | ||
(msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void; | ||
}; | ||
error: { | ||
(msg: unknown): void; | ||
(msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void; | ||
}; | ||
success: { | ||
(msg: unknown): void; | ||
(msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void; | ||
}; | ||
report: (reportingSeverity: import("@docusaurus/types").ReportingSeverity) => { | ||
(msg: unknown): void; | ||
(msg: TemplateStringsArray, values_0: string | number | (string | number)[], ...values: (string | number | (string | number)[])[]): void; | ||
}; | ||
newLine: () => void; | ||
}; | ||
export = logger; | ||
export { PerfLogger } from './perfLogger'; | ||
//# sourceMappingURL=index.d.ts.map |
135
lib/index.js
@@ -8,127 +8,14 @@ "use strict"; | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PerfLogger = exports.logger = void 0; | ||
const tslib_1 = require("tslib"); | ||
const chalk_1 = tslib_1.__importDefault(require("chalk")); | ||
const path = (msg) => chalk_1.default.cyan.underline(`"${String(msg)}"`); | ||
const url = (msg) => chalk_1.default.cyan.underline(msg); | ||
const name = (msg) => chalk_1.default.blue.bold(msg); | ||
const code = (msg) => chalk_1.default.cyan(`\`${String(msg)}\``); | ||
const subdue = (msg) => chalk_1.default.gray(msg); | ||
const num = (msg) => chalk_1.default.yellow(msg); | ||
function interpolate(msgs, ...values) { | ||
let res = ''; | ||
values.forEach((value, idx) => { | ||
const flag = msgs[idx].match(/[a-z]+=$/); | ||
res += msgs[idx].replace(/[a-z]+=$/, ''); | ||
const format = (() => { | ||
if (!flag) { | ||
return (a) => a; | ||
} | ||
switch (flag[0]) { | ||
case 'path=': | ||
return path; | ||
case 'url=': | ||
return url; | ||
case 'number=': | ||
return num; | ||
case 'name=': | ||
return name; | ||
case 'subdue=': | ||
return subdue; | ||
case 'code=': | ||
return code; | ||
default: | ||
throw new Error('Bad Docusaurus logging message. This is likely an internal bug, please report it.'); | ||
} | ||
})(); | ||
res += Array.isArray(value) | ||
? `\n- ${value.map((v) => format(v)).join('\n- ')}` | ||
: format(value); | ||
}); | ||
res += msgs.slice(-1)[0]; | ||
return res; | ||
} | ||
function stringify(msg) { | ||
if (String(msg) === '[object Object]') { | ||
return JSON.stringify(msg); | ||
} | ||
if (msg instanceof Date) { | ||
return msg.toUTCString(); | ||
} | ||
return String(msg); | ||
} | ||
function info(msg, ...values) { | ||
console.info(`${chalk_1.default.cyan.bold('[INFO]')} ${values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg, ...values)}`); | ||
} | ||
function warn(msg, ...values) { | ||
console.warn(chalk_1.default.yellow(`${chalk_1.default.bold('[WARNING]')} ${values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg, ...values)}`)); | ||
} | ||
function error(msg, ...values) { | ||
console.error(chalk_1.default.red(`${chalk_1.default.bold('[ERROR]')} ${values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg, ...values)}`)); | ||
} | ||
function success(msg, ...values) { | ||
console.log(`${chalk_1.default.green.bold('[SUCCESS]')} ${values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg, ...values)}`); | ||
} | ||
function throwError(msg, ...values) { | ||
throw new Error(values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg, ...values)); | ||
} | ||
function newLine() { | ||
console.log(); | ||
} | ||
/** | ||
* Takes a message and reports it according to the severity that the user wants. | ||
* | ||
* - `ignore`: completely no-op | ||
* - `log`: uses the `INFO` log level | ||
* - `warn`: uses the `WARN` log level | ||
* - `throw`: aborts the process, throws the error. | ||
* | ||
* Since the logger doesn't have logging level filters yet, these severities | ||
* mostly just differ by their colors. | ||
* | ||
* @throws In addition to throwing when `reportingSeverity === "throw"`, this | ||
* function also throws if `reportingSeverity` is not one of the above. | ||
*/ | ||
function report(reportingSeverity) { | ||
const reportingMethods = { | ||
ignore: () => { }, | ||
log: info, | ||
warn, | ||
throw: throwError, | ||
}; | ||
if (!Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity)) { | ||
throw new Error(`Unexpected "reportingSeverity" value: ${reportingSeverity}.`); | ||
} | ||
return reportingMethods[reportingSeverity]; | ||
} | ||
const logger = { | ||
red: (msg) => chalk_1.default.red(msg), | ||
yellow: (msg) => chalk_1.default.yellow(msg), | ||
green: (msg) => chalk_1.default.green(msg), | ||
bold: (msg) => chalk_1.default.bold(msg), | ||
dim: (msg) => chalk_1.default.dim(msg), | ||
path, | ||
url, | ||
name, | ||
code, | ||
subdue, | ||
num, | ||
interpolate, | ||
info, | ||
warn, | ||
error, | ||
success, | ||
report, | ||
newLine, | ||
}; | ||
module.exports = logger; | ||
const logger_1 = tslib_1.__importDefault(require("./logger")); | ||
exports.default = logger_1.default; | ||
// Extra named export to avoid problems in ESM modules | ||
// Notably: core .mjs CLI + create-docusaurus | ||
// See https://github.com/facebook/docusaurus/pull/6661 | ||
// See https://github.com/facebook/docusaurus/pull/7295 | ||
exports.logger = logger_1.default; | ||
var perfLogger_1 = require("./perfLogger"); | ||
Object.defineProperty(exports, "PerfLogger", { enumerable: true, get: function () { return perfLogger_1.PerfLogger; } }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@docusaurus/logger", | ||
"version": "3.5.2", | ||
"version": "3.6.0-canary-6132", | ||
"description": "An encapsulated logger for semantically formatting console messages.", | ||
@@ -32,3 +32,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "eeec303dd773774ed5a023884800da0b061f6942" | ||
"gitHead": "4930a868b86cafcb0786e2ddc6d400459127c4f6" | ||
} |
198
src/index.ts
@@ -8,194 +8,12 @@ /** | ||
import chalk from 'chalk'; | ||
import type {ReportingSeverity} from '@docusaurus/types'; | ||
import OriginalLogger from './logger'; | ||
type InterpolatableValue = string | number | (string | number)[]; | ||
export default OriginalLogger; | ||
const path = (msg: unknown): string => chalk.cyan.underline(`"${String(msg)}"`); | ||
const url = (msg: unknown): string => chalk.cyan.underline(msg); | ||
const name = (msg: unknown): string => chalk.blue.bold(msg); | ||
const code = (msg: unknown): string => chalk.cyan(`\`${String(msg)}\``); | ||
const subdue = (msg: unknown): string => chalk.gray(msg); | ||
const num = (msg: unknown): string => chalk.yellow(msg); | ||
// Extra named export to avoid problems in ESM modules | ||
// Notably: core .mjs CLI + create-docusaurus | ||
// See https://github.com/facebook/docusaurus/pull/6661 | ||
// See https://github.com/facebook/docusaurus/pull/7295 | ||
export const logger = OriginalLogger; | ||
function interpolate( | ||
msgs: TemplateStringsArray, | ||
...values: InterpolatableValue[] | ||
): string { | ||
let res = ''; | ||
values.forEach((value, idx) => { | ||
const flag = msgs[idx]!.match(/[a-z]+=$/); | ||
res += msgs[idx]!.replace(/[a-z]+=$/, ''); | ||
const format = (() => { | ||
if (!flag) { | ||
return (a: string | number) => a; | ||
} | ||
switch (flag[0]) { | ||
case 'path=': | ||
return path; | ||
case 'url=': | ||
return url; | ||
case 'number=': | ||
return num; | ||
case 'name=': | ||
return name; | ||
case 'subdue=': | ||
return subdue; | ||
case 'code=': | ||
return code; | ||
default: | ||
throw new Error( | ||
'Bad Docusaurus logging message. This is likely an internal bug, please report it.', | ||
); | ||
} | ||
})(); | ||
res += Array.isArray(value) | ||
? `\n- ${value.map((v) => format(v)).join('\n- ')}` | ||
: format(value); | ||
}); | ||
res += msgs.slice(-1)[0]; | ||
return res; | ||
} | ||
function stringify(msg: unknown): string { | ||
if (String(msg) === '[object Object]') { | ||
return JSON.stringify(msg); | ||
} | ||
if (msg instanceof Date) { | ||
return msg.toUTCString(); | ||
} | ||
return String(msg); | ||
} | ||
function info(msg: unknown): void; | ||
function info( | ||
msg: TemplateStringsArray, | ||
...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
): void; | ||
function info(msg: unknown, ...values: InterpolatableValue[]): void { | ||
console.info( | ||
`${chalk.cyan.bold('[INFO]')} ${ | ||
values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg as TemplateStringsArray, ...values) | ||
}`, | ||
); | ||
} | ||
function warn(msg: unknown): void; | ||
function warn( | ||
msg: TemplateStringsArray, | ||
...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
): void; | ||
function warn(msg: unknown, ...values: InterpolatableValue[]): void { | ||
console.warn( | ||
chalk.yellow( | ||
`${chalk.bold('[WARNING]')} ${ | ||
values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg as TemplateStringsArray, ...values) | ||
}`, | ||
), | ||
); | ||
} | ||
function error(msg: unknown): void; | ||
function error( | ||
msg: TemplateStringsArray, | ||
...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
): void; | ||
function error(msg: unknown, ...values: InterpolatableValue[]): void { | ||
console.error( | ||
chalk.red( | ||
`${chalk.bold('[ERROR]')} ${ | ||
values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg as TemplateStringsArray, ...values) | ||
}`, | ||
), | ||
); | ||
} | ||
function success(msg: unknown): void; | ||
function success( | ||
msg: TemplateStringsArray, | ||
...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
): void; | ||
function success(msg: unknown, ...values: InterpolatableValue[]): void { | ||
console.log( | ||
`${chalk.green.bold('[SUCCESS]')} ${ | ||
values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg as TemplateStringsArray, ...values) | ||
}`, | ||
); | ||
} | ||
function throwError(msg: unknown): void; | ||
function throwError( | ||
msg: TemplateStringsArray, | ||
...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
): void; | ||
function throwError(msg: unknown, ...values: InterpolatableValue[]): void { | ||
throw new Error( | ||
values.length === 0 | ||
? stringify(msg) | ||
: interpolate(msg as TemplateStringsArray, ...values), | ||
); | ||
} | ||
function newLine(): void { | ||
console.log(); | ||
} | ||
/** | ||
* Takes a message and reports it according to the severity that the user wants. | ||
* | ||
* - `ignore`: completely no-op | ||
* - `log`: uses the `INFO` log level | ||
* - `warn`: uses the `WARN` log level | ||
* - `throw`: aborts the process, throws the error. | ||
* | ||
* Since the logger doesn't have logging level filters yet, these severities | ||
* mostly just differ by their colors. | ||
* | ||
* @throws In addition to throwing when `reportingSeverity === "throw"`, this | ||
* function also throws if `reportingSeverity` is not one of the above. | ||
*/ | ||
function report(reportingSeverity: ReportingSeverity): typeof success { | ||
const reportingMethods = { | ||
ignore: () => {}, | ||
log: info, | ||
warn, | ||
throw: throwError, | ||
}; | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity) | ||
) { | ||
throw new Error( | ||
`Unexpected "reportingSeverity" value: ${reportingSeverity}.`, | ||
); | ||
} | ||
return reportingMethods[reportingSeverity]; | ||
} | ||
const logger = { | ||
red: (msg: string | number): string => chalk.red(msg), | ||
yellow: (msg: string | number): string => chalk.yellow(msg), | ||
green: (msg: string | number): string => chalk.green(msg), | ||
bold: (msg: string | number): string => chalk.bold(msg), | ||
dim: (msg: string | number): string => chalk.dim(msg), | ||
path, | ||
url, | ||
name, | ||
code, | ||
subdue, | ||
num, | ||
interpolate, | ||
info, | ||
warn, | ||
error, | ||
success, | ||
report, | ||
newLine, | ||
}; | ||
// TODO remove when migrating to ESM | ||
// logger can only be default-imported in ESM with this | ||
export = logger; | ||
export {PerfLogger} from './perfLogger'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138762
24
827
34
241
2
2
1