Skip to content

Commit 548e1c8

Browse files
puneetdixit200Deepak kudi
andauthored
fix(cli): keep logs off stdout (#5207)
Co-authored-by: Deepak kudi <deepakkudi23@adsl-172-10-9-116.dsl.sndg02.sbcglobal.net>
1 parent 20e7530 commit 548e1c8

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

packages-engine/cli/src/index.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import { debugDetailsTable } from './debug'
1717
import { handleError, PrettyError } from './errors'
1818
import { getWatcher } from './watcher'
1919

20+
function createCliLogger(options: Pick<CliOptions, 'stdout'>) {
21+
return options.stdout
22+
? consola.create({ stdout: process.stderr })
23+
: consola
24+
}
25+
2026
async function resolveOptions(options: CliOptions, userConfig: ResolvedConfig): Promise<ResolvedCliOptions> {
2127
const resolvedOptions = {
2228
...options,
@@ -48,7 +54,9 @@ async function resolveOptions(options: CliOptions, userConfig: ResolvedConfig):
4854
}
4955

5056
if (resolvedOptions.writeTransformed) {
51-
consola.warn(`--write-transformed is deprecated, please use ${yellow('--rewrite')} instead.`)
57+
createCliLogger(options).warn(
58+
`--write-transformed is deprecated, please use ${yellow('--rewrite')} instead.`,
59+
)
5260
}
5361

5462
return resolvedOptions
@@ -138,24 +146,25 @@ export async function build(_options: CliOptions) {
138146
const fileCache = new Map<string, FileEntryItem[]>()
139147
const configResult = await initializeConfig(_options)
140148
const options = await resolveOptions(_options, configResult.config)
149+
const logger = createCliLogger(options)
141150
options.ctx = configResult.ctx
142151

143152
if (options.stdout && options.outFile) {
144-
consola.fatal(`Cannot use --stdout and --out-file at the same time`)
153+
logger.fatal(`Cannot use --stdout and --out-file at the same time`)
145154
return
146155
}
147156

148-
consola.log(green(`UnoCSS v${version}`))
157+
logger.log(green(`UnoCSS v${version}`))
149158

150159
if (options.watch)
151-
consola.start(`UnoCSS in watch mode...`)
160+
logger.start(`UnoCSS in watch mode...`)
152161
else
153-
consola.start(`UnoCSS for production...`)
162+
logger.start(`UnoCSS for production...`)
154163

155164
await parseEntries(options, fileCache)
156165

157166
if (fileCache.size === 0) {
158-
consola.warn('No files matched the provided patterns.')
167+
logger.warn('No files matched the provided patterns.')
159168
return
160169
}
161170

@@ -207,8 +216,8 @@ export async function build(_options: CliOptions) {
207216
watcher.add(configSources)
208217

209218
if (type === 'change')
210-
consola.info(`${cyan(basename(file))} changed, setting new config`)
211-
consola.info(
219+
logger.info(`${cyan(basename(file))} changed, setting new config`)
220+
logger.info(
212221
`Watching for changes in ${
213222
[
214223
...options.entries.flatMap(i => i.patterns),
@@ -220,7 +229,7 @@ export async function build(_options: CliOptions) {
220229
}
221230
else {
222231
if (type === 'change') {
223-
consola.log(`${green(type)} ${dim(file)}`)
232+
logger.log(`${green(type)} ${dim(file)}`)
224233
const content = await fs.readFile(absolutePath, 'utf8')
225234
const matchedEntry = fileCache.keys().find(outfile => outfile === absolutePath)
226235
if (matchedEntry)
@@ -234,7 +243,7 @@ export async function build(_options: CliOptions) {
234243
}
235244
}
236245
else if (type === 'unlink') {
237-
consola.log(`${green(type)} ${dim(file)}`)
246+
logger.log(`${green(type)} ${dim(file)}`)
238247
for (const [, files] of fileCache.entries()) {
239248
const index = files.findIndex(f => f.id === absolutePath)
240249
if (index !== -1) {
@@ -243,7 +252,7 @@ export async function build(_options: CliOptions) {
243252
}
244253
}
245254
else if (type === 'add') {
246-
consola.log(`${green(type)} ${dim(file)}`)
255+
logger.log(`${green(type)} ${dim(file)}`)
247256
await parseEntries(options, fileCache)
248257
}
249258
}

packages-engine/cli/test/cli.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { execa } from 'execa'
12
import fs from 'fs-extra'
23
import { resolve } from 'pathe'
34
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
45
import { getWatcher } from '../src/watcher'
5-
import { getTestDir, readFile, runAsyncChildProcess, runCli, sleep, tempDir } from './utils'
6+
import { cli, getTestDir, readFile, runAsyncChildProcess, runCli, sleep, tempDir } from './utils'
67

78
beforeAll(async () => {
89
await fs.remove(tempDir)
@@ -185,6 +186,39 @@ describe('cli', () => {
185186
expect(output).not.toContain('.bg-red')
186187
expect(output).not.toContain('.text-white')
187188
})
189+
190+
it('keeps cli logs off stdout when writing generated css to stdout', async () => {
191+
const testDir = getTestDir()
192+
await fs.outputFile(resolve(testDir, 'views/index.html'), '<div class="bg-black"></div>', 'utf8')
193+
194+
const { stdout, stderr } = await execa(
195+
'pnpm',
196+
[
197+
'exec',
198+
'tsx',
199+
cli,
200+
resolve(testDir, 'views/index.html'),
201+
'--stdout',
202+
'--preset',
203+
'wind4',
204+
'--preflights',
205+
'false',
206+
],
207+
{
208+
env: {
209+
NODE_ENV: 'development',
210+
TEST: 'false',
211+
VITEST: 'false',
212+
},
213+
},
214+
)
215+
216+
expect(stdout).toContain('.bg-black')
217+
expect(stdout).not.toContain('UnoCSS v')
218+
expect(stdout).not.toContain('UnoCSS for production')
219+
expect(stderr).toContain('UnoCSS v')
220+
expect(stderr).toContain('UnoCSS for production')
221+
})
188222
})
189223

190224
describe.skipIf(process.version.startsWith('v20'))('cli watch mode', () => {

0 commit comments

Comments
 (0)