Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
with:
node-version: 24.14.0
node-version: 24.15.0

# Update npm to the latest version to enable OIDC
# Use corepack to install pnpm
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
with:
node-version: 24.14.0
node-version: 24.15.0

- name: Install Dependencies
run: pnpm install && npx playwright install
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
"test": "rstest"
},
"devDependencies": {
"@rslib/core": "^0.20.0",
"@rslint/core": "^0.3.3",
"@rstest/core": "^0.9.4",
"@types/node": "^24.12.0",
"@rslib/core": "^0.21.3",
"@rslint/core": "^0.5.0",
"@rstest/core": "^0.9.9",
"@types/node": "^24.12.2",
"path-serializer": "^0.6.0",
"prettier": "^3.8.1",
"prettier": "^3.8.3",
"strip-ansi": "^7.2.0",
"supports-color": "^10.2.2",
"typescript": "^6.0.2"
"typescript": "^6.0.3"
},
"packageManager": "pnpm@10.32.1",
"packageManager": "pnpm@10.33.2",
"engines": {
"node": "^20.19.0 || >=22.12.0"
},
Expand Down
425 changes: 150 additions & 275 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { color } from './color.js';
import type { LogType } from './types.js';

export let LOG_LEVEL = {
export const LOG_LEVEL = {
silent: -1,
error: 0,
warn: 1,
Expand All @@ -11,7 +11,7 @@ export let LOG_LEVEL = {
verbose: 3,
} as const;

export let LOG_TYPES = {
export const LOG_TYPES = {
// Level error
error: {
label: 'error',
Expand Down
18 changes: 9 additions & 9 deletions src/createLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import type { Options, LogMessage, Logger, LogMethods } from './types.js';

const normalizeErrorMessage = (err: Error) => {
if (err.stack) {
let [name, ...rest] = err.stack.split('\n');
if (name.startsWith('Error: ')) {
name = name.slice(7);
}
const [rawName, ...rest] = err.stack.split('\n');
const name = rawName.startsWith('Error: ')
? rawName.slice(7)
: rawName;
return `${name}\n${color.gray(rest.join('\n'))}`;
}

return err.message;
};

export let createLogger = (options: Options = {}) => {
export const createLogger = (options: Options = {}) => {
const { level = 'info', prefix, console = globalThis.console } = options;

let maxLevel = level;

let log = (type: LogMethods, message?: LogMessage, ...args: unknown[]) => {
let logType = LOG_TYPES[type];
const log = (type: LogMethods, message?: LogMessage, ...args: unknown[]) => {
const logType = LOG_TYPES[type];
const { level } = logType;

if (LOG_LEVEL[level] > LOG_LEVEL[maxLevel]) {
Expand Down Expand Up @@ -53,7 +53,7 @@ export let createLogger = (options: Options = {}) => {
}
// change the color of error stacks to
else if (level === 'error' && typeof message === 'string') {
let lines = message.split('\n');
const lines = message.split('\n');
text = lines
.map((line) => (isErrorStackMessage(line) ? color.gray(line) : line))
.join('\n');
Expand All @@ -69,7 +69,7 @@ export let createLogger = (options: Options = {}) => {
console[method](label.length ? `${label} ${text}` : text, ...args);
};

let logger = {
const logger = {
greet: (message: string) => log('log', gradient(message)),
} as Logger;

Expand Down
20 changes: 10 additions & 10 deletions src/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import { color } from './color.js';
import { colorLevel } from './utils.js';

// RGB for #bdfff3
let startColor = [189, 255, 243];
const startColor = [189, 255, 243];
// RGB for #4ac29a
let endColor = [74, 194, 154];
const endColor = [74, 194, 154];

let isWord = (char: string) => !/[\s\n]/.test(char);
const isWord = (char: string) => !/[\s\n]/.test(char);

export let gradient = (message: string) => {
export const gradient = (message: string) => {
if (colorLevel < 3) {
return colorLevel === 2 ? color.cyan(message) : message;
}

// split string and handle emoji correctly
// https://stackoverflow.com/questions/24531751/how-can-i-split-a-string-containing-emoji-into-an-array
let chars = [...message];
let steps = chars.filter(isWord).length;
const chars = [...message];
const steps = chars.filter(isWord).length;
let r = startColor[0];
let g = startColor[1];
let b = startColor[2];
let rStep = (endColor[0] - r) / steps;
let gStep = (endColor[1] - g) / steps;
let bStep = (endColor[2] - b) / steps;
const rStep = (endColor[0] - r) / steps;
const gStep = (endColor[1] - g) / steps;
const bStep = (endColor[2] - b) / steps;
let output = '';

for (let char of chars) {
for (const char of chars) {
if (isWord(char)) {
r += rStep;
g += gStep;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createLogger } from './createLogger.js';
export { createLogger };
export { color } from './color.js';

export let logger = createLogger();
export const logger = createLogger();

export type {
Options,
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import supportsColor from 'supports-color';
// https://github.com/chalk/supports-color
export const colorLevel = supportsColor.stdout ? supportsColor.stdout.level : 0;

let errorStackRegExp = /at [^\r\n]{0,200}:\d+:\d+[\s\)]*$/;
let anonymousErrorStackRegExp = /at [^\r\n]{0,200}\(<anonymous>\)$/;
let indexErrorStackRegExp = /at [^\r\n]{0,200}\(index\s\d+\)$/;
const errorStackRegExp = /at [^\r\n]{0,200}:\d+:\d+[\s\)]*$/;
const anonymousErrorStackRegExp = /at [^\r\n]{0,200}\(<anonymous>\)$/;
const indexErrorStackRegExp = /at [^\r\n]{0,200}\(index\s\d+\)$/;

export let isErrorStackMessage = (message: string) =>
export const isErrorStackMessage = (message: string) =>
errorStackRegExp.test(message) ||
anonymousErrorStackRegExp.test(message) ||
indexErrorStackRegExp.test(message);
8 changes: 6 additions & 2 deletions tests/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ describe('logger', () => {

logger.error(err);

expect((console.error as Mock).mock.calls[0][0]).toMatchSnapshot();
expect(
stripAnsi((console.error as Mock).mock.calls[0][0].toString()),
).toMatchSnapshot();
});

test('should log error with cause correctly', () => {
Expand All @@ -125,7 +127,9 @@ describe('logger', () => {

logger.error(err);

expect((console.error as Mock).mock.calls[0][0]).toMatchSnapshot();
expect(
stripAnsi((console.error as Mock).mock.calls[0][0].toString()),
).toMatchSnapshot();
});

test('should create new logger with silent level correctly', () => {
Expand Down
Loading