11import { ensureFile } from '@neodx/fs'
2- import { hasOwn , isNil } from '@neodx/std'
2+ import { hasOwn , isObject } from '@neodx/std'
3+ import { Inject } from '@nestjs/common'
34import chalk from 'chalk'
45import { Command , CommandRunner , Option } from 'nest-commander'
56import { dirname , resolve } from 'node:path'
7+ import { LoggerService } from '@/logger'
68import type { AbstractPackageManager } from '@/pkg-manager'
79import { InjectPackageManager } from '@/pkg-manager'
810import { PackageManager } from '@/pkg-manager/pkg-manager.consts'
911import type { PackageJson } from '@/shared/json'
1012import { readJson } from '@/shared/json'
11- import { addLibraryPrefix } from '@/shared/misc'
13+ import { invariant } from '@/shared/misc'
1214
1315export interface BaseInitOptions {
1416 args ?: string
@@ -23,20 +25,18 @@ export interface BaseInitOptions {
2325} )
2426export class RunCommand extends CommandRunner {
2527 constructor (
26- @InjectPackageManager ( ) private readonly manager : AbstractPackageManager
28+ @InjectPackageManager ( ) private readonly manager : AbstractPackageManager ,
29+ @Inject ( LoggerService ) private readonly logger : LoggerService
2730 ) {
2831 super ( )
2932 }
3033
3134 public async run ( params : string [ ] , options : BaseInitOptions ) {
3235 const [ target , project = null ] = params
3336
34- if ( ! target ) {
35- console . log ( 'no target' )
36- return
37- }
37+ invariant ( target , 'Target is not provided.' )
3838
39- const startTime = Date . now ( )
39+ const timeEnd = this . logger . time ( )
4040
4141 let packageJsonPath = resolve ( process . cwd ( ) , 'package.json' )
4242
@@ -47,28 +47,24 @@ export class RunCommand extends CommandRunner {
4747 ( workspace ) => workspace . name === project
4848 )
4949
50- if ( ! projectMeta ) {
51- // TODO improve errors rewrite to class
52- throw new Error (
53- `Project ${ chalk . cyan ( project ) } not found. Please ensure it exists.`
54- )
55- }
50+ invariant (
51+ projectMeta ,
52+ `Project ${ project } not found. Please ensure it exists.`
53+ )
5654
5755 packageJsonPath = resolve ( projectMeta . location , 'package.json' )
5856 }
5957
6058 await ensureFile ( packageJsonPath )
6159
62- const pkg = ( await readJson ( packageJsonPath ) ) as PackageJson
60+ const pkg = await readJson < PackageJson > ( packageJsonPath )
6361
6462 const projectName = project ?? pkg . name ?? 'root'
6563
66- if ( isNil ( pkg . scripts ) || ! hasOwn ( pkg . scripts , target ) ) {
67- // TODO improve errors
68- throw new Error (
69- `Could not find target ${ chalk . cyan ( target ) } in project ${ chalk . white ( projectName ) } .`
70- )
71- }
64+ invariant (
65+ isObject ( pkg . scripts ) && hasOwn ( pkg . scripts , target ) ,
66+ `Could not find target ${ chalk . cyan ( target ) } in project ${ chalk . white ( projectName ) } .`
67+ )
7268
7369 const command = this . manager . createRunCommand ( {
7470 target,
@@ -86,15 +82,13 @@ export class RunCommand extends CommandRunner {
8682
8783 await this . manager . exec ( command , { stdio : 'inherit' , cwd } )
8884 } catch ( error ) {
89- console . log ( error )
85+ this . logger . error (
86+ `Error occurred while executing a command via ${ this . manager . agent } agent.`
87+ )
88+ this . logger . error ( error )
9089 }
9190
92- // TODO: improve logger
93- console . info (
94- addLibraryPrefix (
95- `Successfully ran target ${ chalk . cyan ( target ) } for project ${ chalk . white ( project ?? pkg . name ) } (${ Date . now ( ) - startTime } ms)`
96- )
97- )
91+ timeEnd ( `Successfully ran target ${ target } for project ${ projectName } ` )
9892 }
9993
10094 @Option ( {
@@ -106,9 +100,7 @@ export class RunCommand extends CommandRunner {
106100
107101 const isValid = args . match ( / ^ - - \w + = \w + $ / )
108102
109- if ( ! isValid ) {
110- throw new Error ( 'not valid' )
111- }
103+ invariant ( isValid , "The 'args' parameter is invalid. " )
112104
113105 return args
114106 }
0 commit comments