From 94dc4652ea0ddd8124944ab09c4b1e68a80544b1 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sun, 20 Apr 2025 10:39:02 +0800 Subject: [PATCH 1/2] feat: log supports multiple colors --- src/index.ts | 8 ++++---- src/utils.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 42553f0..05ffe18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import path, { posix } from 'node:path'; import { type Options as FdirOptions, fdir } from 'fdir'; import picomatch from 'picomatch'; -import { escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts'; +import { chalkLog, escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts'; const PARENT_DIRECTORY = /^(\/?\.\.)+/; const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g; @@ -207,7 +207,7 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) { const matches = matcher(path); if (matches) { - log(`matched ${path}`); + chalkLog(`matched ${path}`, 'matched'); } return matches; @@ -220,9 +220,9 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) { const skipped = (relativePath !== '.' && !partialMatcher(relativePath)) || ignore(relativePath); if (skipped) { - log(`skipped ${p}`); + chalkLog(`skipped ${p}`, 'skipped'); } else { - log(`crawling ${p}`); + chalkLog(`crawling ${p}`, 'crawling'); } return skipped; diff --git a/src/utils.ts b/src/utils.ts index cdf6ecd..feec710 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -150,4 +150,17 @@ export function isDynamicPattern(pattern: string, options?: { caseSensitiveMatch export function log(...tasks: unknown[]): void { console.log(`[tinyglobby ${new Date().toLocaleTimeString('es')}]`, ...tasks); } +export function chalkLog(msg: string, type: string): void { + const _chalk = { + matched: '\x1b[32m', + skipped: '\x1b[90m', + crawling: '\x1b[34m' + }; + const chalk = _chalk[type as keyof typeof _chalk]; + if (chalk) { + console.log(`${chalk}%s\x1b[0m`, `[tinyglobby ${new Date().toLocaleTimeString('es')}]${msg}`); + return; + } + console.log(`[tinyglobby ${new Date().toLocaleTimeString('es')}]`, msg); +} // #endregion From bd11759f32a4a1185fce57212b2c18d3b0447b7e Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 24 Apr 2025 07:21:04 +0800 Subject: [PATCH 2/2] refactor: rename --- src/index.ts | 8 ++++---- src/utils.ts | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 05ffe18..d894ce2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import path, { posix } from 'node:path'; import { type Options as FdirOptions, fdir } from 'fdir'; import picomatch from 'picomatch'; -import { chalkLog, escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts'; +import { coloredLog, escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts'; const PARENT_DIRECTORY = /^(\/?\.\.)+/; const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g; @@ -207,7 +207,7 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) { const matches = matcher(path); if (matches) { - chalkLog(`matched ${path}`, 'matched'); + coloredLog(`matched ${path}`, 'matched'); } return matches; @@ -220,9 +220,9 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) { const skipped = (relativePath !== '.' && !partialMatcher(relativePath)) || ignore(relativePath); if (skipped) { - chalkLog(`skipped ${p}`, 'skipped'); + coloredLog(`skipped ${p}`, 'skipped'); } else { - chalkLog(`crawling ${p}`, 'crawling'); + coloredLog(`crawling ${p}`, 'crawling'); } return skipped; diff --git a/src/utils.ts b/src/utils.ts index feec710..17a0a75 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -150,15 +150,15 @@ export function isDynamicPattern(pattern: string, options?: { caseSensitiveMatch export function log(...tasks: unknown[]): void { console.log(`[tinyglobby ${new Date().toLocaleTimeString('es')}]`, ...tasks); } -export function chalkLog(msg: string, type: string): void { - const _chalk = { +export function coloredLog(msg: string, type: 'matched' | 'skipped' | 'crawling'): void { + const colors = { matched: '\x1b[32m', skipped: '\x1b[90m', crawling: '\x1b[34m' }; - const chalk = _chalk[type as keyof typeof _chalk]; - if (chalk) { - console.log(`${chalk}%s\x1b[0m`, `[tinyglobby ${new Date().toLocaleTimeString('es')}]${msg}`); + const color = colors[type as keyof typeof colors]; + if (color) { + console.log(`${color}%s\x1b[0m`, `[tinyglobby ${new Date().toLocaleTimeString('es')}]${msg}`); return; } console.log(`[tinyglobby ${new Date().toLocaleTimeString('es')}]`, msg);