Skip to content

Commit 81168e8

Browse files
committed
fix: logging
1 parent b98f548 commit 81168e8

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/commands/run.command.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ensureFile } from '@neodx/fs'
22
import { hasOwn, isObject } from '@neodx/std'
33
import { Inject } from '@nestjs/common'
4-
import chalk from 'chalk'
54
import { Command, CommandRunner, Option } from 'nest-commander'
65
import { dirname, resolve } from 'node:path'
76
import { LoggerService } from '@/logger'
@@ -33,16 +32,13 @@ export class RunCommand extends CommandRunner {
3332

3433
public async run(params: string[], options: BaseInitOptions) {
3534
const [target, project = null] = params
36-
37-
invariant(target, 'Target is not provided.')
38-
3935
const timeEnd = this.logger.time()
40-
4136
let packageJsonPath = resolve(process.cwd(), 'package.json')
4237

38+
invariant(target, 'Target is not provided.')
39+
4340
if (project) {
4441
const workspaces = await this.manager.getWorkspaces()
45-
4642
const projectMeta = workspaces.find(
4743
(workspace) => workspace.name === project
4844
)
@@ -58,12 +54,11 @@ export class RunCommand extends CommandRunner {
5854
await ensureFile(packageJsonPath)
5955

6056
const pkg = await readJson<PackageJson>(packageJsonPath)
61-
6257
const projectName = project ?? pkg.name ?? 'root'
6358

6459
invariant(
6560
isObject(pkg.scripts) && hasOwn(pkg.scripts, target),
66-
`Could not find target ${chalk.cyan(target)} in project ${chalk.white(projectName)}.`
61+
`Could not find target ${target} in project ${projectName}.`
6762
)
6863

6964
const command = this.manager.createRunCommand({
@@ -86,6 +81,7 @@ export class RunCommand extends CommandRunner {
8681
`Error occurred while executing a command via ${this.manager.agent} agent.`
8782
)
8883
this.logger.error(error)
84+
return
8985
}
9086

9187
timeEnd(`Successfully ran target ${target} for project ${projectName}`)

src/logger/logger.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class LoggerService {
2020

2121
public error(msg: unknown) {
2222
if (isTypeOfString(msg)) {
23-
console.error(`\n ${this.errorPrefix} ${chalk.bold(chalk.red(msg))}\n`)
23+
console.error(`\n ${this.errorPrefix} ${chalk.bold(chalk.red(msg))}`)
2424
} else if (msg instanceof Error && msg.stack) {
2525
console.error(chalk.bold(chalk.red(msg.stack)))
2626
} else {
@@ -30,19 +30,19 @@ export class LoggerService {
3030

3131
public info(msg: unknown) {
3232
if (isTypeOfString(msg)) {
33-
console.info(`\n${this.libraryPrefix} ${chalk.bold(msg)}\n`)
33+
console.info(`\n${this.libraryPrefix} ${chalk.bold(msg)}`)
3434
} else console.info(msg)
3535
}
3636

37-
public time(method = this.log) {
37+
public time(method = this.info) {
3838
const startTime = Date.now()
3939

4040
return (msg: string) => {
41-
method(`${msg} (${Date.now() - startTime} ms)`)
41+
method.call(this, `${msg} (${Date.now() - startTime} ms)`)
4242
}
4343
}
4444

45-
public serialize(msg: unknown, method = this.log) {
45+
public serialize(msg: unknown, method = this.info) {
4646
method(serializeJson(msg))
4747
}
4848

src/shared/misc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function invariant<T extends unknown>(
66
message: string
77
): asserts condition is NonNullable<T> {
88
if (!condition) {
9-
console.error(`\n ${ERROR_PREFIX} ${chalk.bold(chalk.red(message))}\n`)
9+
console.error(`\n ${ERROR_PREFIX} ${chalk.bold(chalk.red(message))}`)
1010

1111
process.exit(1)
1212
}

0 commit comments

Comments
 (0)