Skip to content

Commit f006c83

Browse files
committed
unified logging
1 parent f66a7c2 commit f006c83

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

src/cli.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
33
import { createInterface } from 'node:readline/promises'
44
import { stdin as input, stdout as output } from 'node:process'
55
import { resolve } from 'pathe'
6-
import { consola } from 'consola'
76
import { createMain, defineCommand } from 'citty'
87

98
import ensureNuxtProject from './utils/ensure-nuxt-project'
109
import { version, name, description } from '../package.json'
10+
import { logger } from './utils/logger'
1111

1212
export const main = createMain({
1313
meta: {
@@ -52,7 +52,7 @@ export const main = createMain({
5252
const pkg = pkgRaw as { scripts?: Record<string, string> }
5353
const hasProcessorDev = Boolean(pkg && pkg.scripts && pkg.scripts['processor:dev'])
5454
if (!hasProcessorDev) {
55-
consola.warn('No "processor:dev" script found in package.json.')
55+
logger.warn('No "processor:dev" script found in package.json.')
5656
const rl = createInterface({ input, output })
5757
const answer = await rl.question('Add script to package.json? (y/N) ')
5858
rl.close()
@@ -67,16 +67,16 @@ export const main = createMain({
6767
}
6868
try {
6969
writeFileSync(pkgPath, JSON.stringify(updated, null, 2) + '\n', 'utf8')
70-
consola.success('Added "processor:dev" script to package.json')
70+
logger.success('Added "processor:dev" script to package.json')
7171
}
7272
catch {
73-
consola.error('Failed to write to package.json')
73+
logger.error('Failed to write to package.json')
7474
}
7575
}
7676
}
7777
}
78-
catch {
79-
// ignore JSON parse errors
78+
catch (error) {
79+
logger.error('Failed to parse', error)
8080
}
8181
}
8282
}
@@ -91,7 +91,7 @@ export const main = createMain({
9191
const pkg = pkgRaw as { scripts?: Record<string, string> }
9292
hasProcessorDev = Boolean(pkg && pkg.scripts && pkg.scripts['processor:dev'])
9393
if (!hasProcessorDev) {
94-
consola.warn('No "processor:dev" script found in package.json.')
94+
logger.warn('No "processor:dev" script found in package.json.')
9595

9696
const rl = createInterface({ input, output })
9797
const answer = await rl.question('Add script to package.json? (y/N) ')
@@ -102,27 +102,27 @@ export const main = createMain({
102102
const updated = {
103103
...pkg,
104104
scripts: {
105-
...(pkg.scripts || {}),
105+
...(pkg.scripts ?? {}),
106106
'processor:dev': 'nuxt-processor dev',
107107
},
108108
}
109109
try {
110110
writeFileSync(pkgPath, JSON.stringify(updated, null, 2) + '\n', 'utf8')
111-
consola.success('Added "processor:dev" script to package.json')
111+
logger.success('Added "processor:dev" script to package.json')
112112
}
113113
catch {
114-
consola.error('Failed to write to package.json')
114+
logger.error('Failed to write to package.json')
115115
}
116116
}
117117
}
118118
}
119-
catch {
120-
// ignore JSON parse errors, still show guidance
119+
catch (error) {
120+
logger.error('Failed to parse', error)
121121
}
122122
}
123-
consola.error('No entry file found at .nuxt/dev/workers/index.mjs')
124-
consola.info('Please start your Nuxt dev server (e.g. `npm run dev`).')
125-
consola.info('After it starts, run `npx nuxt-processor dev` again to start the processor.')
123+
logger.error('No entry file found at .nuxt/dev/workers/index.mjs')
124+
logger.info('Please start your Nuxt dev server (e.g. `npm run dev`).')
125+
logger.info('After it starts, run `npx nuxt-processor dev` again to start the processor.')
126126
process.exit(1)
127127
}
128128

@@ -140,7 +140,7 @@ export const main = createMain({
140140
...(args.workers ? [`--workers=${args.workers}`] : []),
141141
]
142142

143-
consola.info(`Running watcher for processor`)
143+
logger.info(`Running watcher for processor`)
144144
const child = spawn(nodeBin, nodeArgs, {
145145
stdio: 'inherit',
146146
cwd: projectRoot,

src/utils/ensure-nuxt-project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Ripped from https://github.com/nuxt/telemetry/blob/main/src/cli.ts#L161C1-L172C2
22
import { resolve } from 'pathe'
33
import { loadNuxtConfig } from '@nuxt/kit'
4-
import { consola } from 'consola'
4+
import { logger } from './logger'
55

66
export default async (args: { global: boolean, dir: string }) => {
77
if (args.global) {
@@ -10,7 +10,7 @@ export default async (args: { global: boolean, dir: string }) => {
1010
const dir = resolve(args.dir)
1111
const nuxtConfig = await loadNuxtConfig({ cwd: dir })
1212
if (!nuxtConfig || !nuxtConfig._layers[0]?.configFile) {
13-
consola.error('You are not in a Nuxt project.')
13+
logger.error('You are not in a Nuxt project.')
1414
process.exit()
1515
}
1616
}

src/utils/logger.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createConsola } from 'consola'
2+
import { name } from '../../package.json'
3+
4+
export const logger = createConsola({}).withTag(name)

src/utils/scan-folder.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fg from 'fast-glob'
22
import { createResolver, useNuxt } from '@nuxt/kit'
3+
import { logger } from './logger'
34

45
// Credit for this code to
56
// https://github.com/genu/nuxt-concierge/blob/master/src/helplers/scan-folder.ts
@@ -20,5 +21,9 @@ export default async (path: string): Promise<string[]> => {
2021

2122
files.push(...new Set(updatedFiles))
2223

24+
if (files.length === 0) {
25+
logger.warn('No worker files found in project')
26+
}
27+
2328
return files
2429
}

0 commit comments

Comments
 (0)