Skip to content

Commit 724d108

Browse files
committed
feat: add logLevel & customLogger option
closes #405
1 parent 6c9a2da commit 724d108

File tree

17 files changed

+106
-73
lines changed

17 files changed

+106
-73
lines changed

src/cli.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import debug from 'debug'
55
import { VERSION as rolldownVersion } from 'rolldown'
66
import { version } from '../package.json'
77
import { resolveComma, toArray } from './utils/general'
8-
import { logger } from './utils/logger'
8+
import { globalLogger } from './utils/logger'
99
import type { Options } from './options'
1010

1111
const cli = cac('tsdown')
@@ -26,7 +26,7 @@ cli
2626
.option('--minify', 'Minify output')
2727
.option('--debug [feat]', 'Show debug logs')
2828
.option('--target <target>', 'Bundle target, e.g "es2015", "esnext"')
29-
.option('--silent', 'Suppress non-error logs')
29+
.option('-l, --logLevel <level>', 'Set log level: info, warn, error, silent')
3030
.option('-d, --out-dir <dir>', 'Output directory', { default: 'dist' })
3131
.option('--treeshake', 'Tree-shake bundle', { default: true })
3232
.option('--sourcemap', 'Generate source map', { default: false })
@@ -60,10 +60,8 @@ cli
6060
'Generate export-related metadata for package.json (experimental)',
6161
)
6262
.action(async (input: string[], flags: Options) => {
63-
if (flags.silent) {
64-
logger.level = 'silent'
65-
}
66-
logger.info(
63+
globalLogger.level = flags.logLevel || (flags.silent ? 'silent' : 'info')
64+
globalLogger.info(
6765
`tsdown ${dim`v${version}`} powered by rolldown ${dim`v${rolldownVersion}`}`,
6866
)
6967
const { build } = await import('./index')
@@ -104,7 +102,7 @@ export async function runCLI(): Promise<void> {
104102
try {
105103
await cli.runMatchedCommand()
106104
} catch (error) {
107-
logger.error(error)
105+
globalLogger.error(error)
108106
process.exit(1)
109107
}
110108
}

src/features/attw.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { promisify } from 'node:util'
77
import { blue, dim } from 'ansis'
88
import Debug from 'debug'
99
import { fsRemove } from '../utils/fs'
10-
import { logger } from '../utils/logger'
1110
import type { ResolvedOptions } from '../options'
1211
import type { CheckPackageOptions, Problem } from '@arethetypeswrong/core'
1312

@@ -112,7 +111,7 @@ function formatProblem(problem: Problem): string {
112111
export async function attw(options: ResolvedOptions): Promise<void> {
113112
if (!options.attw) return
114113
if (!options.pkg) {
115-
logger.warn('attw is enabled but package.json is not found')
114+
options.logger.warn('attw is enabled but package.json is not found')
116115
return
117116
}
118117
const {
@@ -130,7 +129,7 @@ export async function attw(options: ResolvedOptions): Promise<void> {
130129
try {
131130
attwCore = await import('@arethetypeswrong/core')
132131
} catch {
133-
logger.error(
132+
options.logger.error(
134133
`ATTW check requires ${blue`@arethetypeswrong/core`} to be installed.`,
135134
)
136135
return
@@ -168,16 +167,16 @@ export async function attw(options: ResolvedOptions): Promise<void> {
168167
throw new Error(problemMessage)
169168
}
170169

171-
logger.warn(problemMessage)
170+
options.logger.warn(problemMessage)
172171
}
173172
} else {
174-
logger.success(
173+
options.logger.success(
175174
`No Are the types wrong problems found`,
176175
dim`(${Math.round(performance.now() - t)}ms)`,
177176
)
178177
}
179178
} catch (error) {
180-
logger.error('ATTW check failed:', error)
179+
options.logger.error('ATTW check failed:', error)
181180
debug('Found errors, setting exit code to 1')
182181
process.exitCode = 1
183182
} finally {

src/features/clean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Debug from 'debug'
33
import { glob } from 'tinyglobby'
44
import { fsRemove } from '../utils/fs'
55
import { slash } from '../utils/general'
6-
import { logger } from '../utils/logger'
6+
import { globalLogger } from '../utils/logger'
77
import type { Options, ResolvedOptions } from '../options'
88

99
const debug = Debug('tsdown:clean')
@@ -31,7 +31,7 @@ export async function cleanOutDir(configs: ResolvedOptions[]): Promise<void> {
3131
}
3232
if (!removes.size) return
3333

34-
logger.info(`Cleaning ${removes.size} files`)
34+
globalLogger.info(`Cleaning ${removes.size} files`)
3535
await Promise.all(
3636
[...removes].map(async (file) => {
3737
debug('Removing', file)

src/features/entry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import path from 'node:path'
22
import { glob } from 'tinyglobby'
33
import { fsExists, lowestCommonAncestor } from '../utils/fs'
4-
import { generateColor, logger, prettyName } from '../utils/logger'
4+
import { generateColor, prettyName, type Logger } from '../utils/logger'
55
import type { Options } from '../options'
66

77
export async function resolveEntry(
8+
logger: Logger,
89
entry: Options['entry'],
910
cwd: string,
1011
name?: string,

src/features/publint.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import process from 'node:process'
22
import { dim } from 'ansis'
33
import Debug from 'debug'
4-
import { logger } from '../utils/logger'
54
import type { ResolvedOptions } from '../options'
65

76
const debug = Debug('tsdown:publint')
87

98
export async function publint(options: ResolvedOptions): Promise<void> {
109
if (!options.publint) return
1110
if (!options.pkg) {
12-
logger.warn('publint is enabled but package.json is not found')
11+
options.logger.warn('publint is enabled but package.json is not found')
1312
return
1413
}
1514

@@ -23,7 +22,7 @@ export async function publint(options: ResolvedOptions): Promise<void> {
2322
debug('Found %d issues', messages.length)
2423

2524
if (!messages.length) {
26-
logger.success(
25+
options.logger.success(
2726
`No publint issues found`,
2827
dim`(${Math.round(performance.now() - t)}ms)`,
2928
)
@@ -35,7 +34,7 @@ export async function publint(options: ResolvedOptions): Promise<void> {
3534
const logType = (
3635
{ error: 'error', warning: 'warn', suggestion: 'info' } as const
3736
)[message.type]
38-
logger[logType](formattedMessage)
37+
options.logger[logType](formattedMessage)
3938
}
4039
if (hasError) {
4140
debug('Found errors, setting exit code to 1')

src/features/report.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Debug from 'debug'
77
import { RE_DTS } from 'rolldown-plugin-dts/filename'
88
import { formatBytes } from '../utils/format'
99
import { noop } from '../utils/general'
10-
import { logger, prettyFormat, prettyName } from '../utils/logger'
10+
import { prettyFormat, prettyName, type Logger } from '../utils/logger'
1111
import type { OutputAsset, OutputChunk, Plugin } from 'rolldown'
1212

1313
const debug = Debug('tsdown:report')
@@ -44,6 +44,7 @@ export interface ReportOptions {
4444

4545
export function ReportPlugin(
4646
options: ReportOptions,
47+
logger: Logger,
4748
cwd: string,
4849
cjsDts?: boolean,
4950
name?: string,

src/features/shebang.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { chmod } from 'node:fs/promises'
22
import path from 'node:path'
33
import { underline } from 'ansis'
44
import { fsExists } from '../utils/fs'
5-
import { logger, prettyFormat, prettyName } from '../utils/logger'
5+
import { prettyFormat, prettyName, type Logger } from '../utils/logger'
66
import type { Plugin } from 'rolldown'
77

88
const RE_SHEBANG = /^#!.*/
99

1010
export function ShebangPlugin(
11+
logger: Logger,
1112
cwd: string,
1213
name?: string,
1314
isMultiFormat?: boolean,

src/features/shortcuts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process'
22
import readline from 'node:readline'
33
import { bold, dim } from 'ansis'
4-
import { logger } from '../utils/logger'
4+
import { globalLogger } from '../utils/logger'
55

66
// Copied from https://github.com/vitejs/vite/blob/main/packages/vite/src/node/shortcuts.ts - MIT License
77

@@ -42,15 +42,15 @@ export function shortcuts(restart: () => void): void {
4242

4343
if (input === 'h') {
4444
const loggedKeys = new Set<string>()
45-
logger.info(' Shortcuts')
45+
globalLogger.info(' Shortcuts')
4646

4747
for (const shortcut of SHORTCUTS) {
4848
if (loggedKeys.has(shortcut.key)) continue
4949
loggedKeys.add(shortcut.key)
5050

5151
if (shortcut.action == null) continue
5252

53-
logger.info(
53+
globalLogger.info(
5454
dim` press ` +
5555
bold`${shortcut.key} + enter` +
5656
dim` to ${shortcut.description}`,

src/features/target.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { blue } from 'ansis'
22
import minVersion from 'semver/ranges/min-version.js'
33
import { resolveComma, toArray } from '../utils/general'
4-
import { generateColor, logger, prettyName } from '../utils/logger'
4+
import { generateColor, prettyName, type Logger } from '../utils/logger'
55
import type { PackageJson } from 'pkg-types'
66
import type { Plugin } from 'rolldown'
77

88
export function resolveTarget(
9+
logger: Logger,
910
target: string | string[] | false | undefined,
1011
pkg?: PackageJson,
1112
name?: string,
@@ -39,7 +40,10 @@ export function resolvePackageTarget(pkg?: PackageJson): string | undefined {
3940
}
4041

4142
let warned = false
42-
export function RuntimeHelperCheckPlugin(targets: string[]): Plugin {
43+
export function RuntimeHelperCheckPlugin(
44+
logger: Logger,
45+
targets: string[],
46+
): Plugin {
4347
return {
4448
name: 'tsdown:runtime-helper-check',
4549
resolveId: {

src/features/tsconfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'node:path'
22
import { blue } from 'ansis'
33
import { up as findUp } from 'empathic/find'
44
import { fsStat } from '../utils/fs'
5-
import { generateColor, logger, prettyName } from '../utils/logger'
5+
import { generateColor, prettyName, type Logger } from '../utils/logger'
66
import type { Options } from '../options'
77

88
export function findTsconfig(
@@ -13,6 +13,7 @@ export function findTsconfig(
1313
}
1414

1515
export async function resolveTsconfig(
16+
logger: Logger,
1617
tsconfig: Options['tsconfig'],
1718
cwd: string,
1819
name?: string,

0 commit comments

Comments
 (0)