Skip to content

Commit 9f813b9

Browse files
authored
refactor: clean separation pattern for import cli command (#290)
1 parent a320f51 commit 9f813b9

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

src/commands/import.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ export const importCommand = new Command('import')
2929
}
3030

3131
await runCarImport(importOptions)
32-
} catch (error) {
33-
console.error('Import failed:', error instanceof Error ? error.message : error)
32+
} catch {
3433
process.exit(1)
3534
}
3635
})

src/import/import.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ export async function runCarImport(options: ImportOptions): Promise<ImportResult
145145
const proceed = await warnAboutCDNPricingLimitations()
146146
if (!proceed) {
147147
cancel('Import cancelled')
148-
process.exitCode = 1
149148
throw new Error('CDN pricing limitations warning cancelled')
150149
}
151150
}
@@ -158,7 +157,7 @@ export async function runCarImport(options: ImportOptions): Promise<ImportResult
158157
if (!fileValidation.exists || !fileValidation.stats) {
159158
spinner.stop(`${pc.red('✗')} ${fileValidation.error}`)
160159
cancel('Import cancelled')
161-
process.exit(1)
160+
throw new Error(fileValidation.error)
162161
}
163162
const fileStat = fileValidation.stats
164163

@@ -169,7 +168,7 @@ export async function runCarImport(options: ImportOptions): Promise<ImportResult
169168
} catch (error) {
170169
spinner.stop(`${pc.red('✗')} Invalid CAR file: ${error instanceof Error ? error.message : 'Unknown error'}`)
171170
cancel('Import cancelled')
172-
process.exit(1)
171+
throw new Error('Invalid CAR file')
173172
}
174173

175174
// Step 3: Handle root CID cases
@@ -290,6 +289,6 @@ export async function runCarImport(options: ImportOptions): Promise<ImportResult
290289
await cleanupSynapseService()
291290

292291
cancel('Import failed')
293-
process.exit(1)
292+
throw error
294293
}
295294
}

src/test/unit/import.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ describe('CAR Import', () => {
338338
privateKey: testPrivateKey,
339339
}
340340

341-
await expect(runCarImport(options)).rejects.toThrow('process.exit called')
342-
expect(consoleMocks.error).toHaveBeenCalledWith('Import cancelled')
341+
await expect(runCarImport(options)).rejects.toThrow()
343342
})
344343

345344
it('should reject non-existent file', async () => {
@@ -348,8 +347,7 @@ describe('CAR Import', () => {
348347
privateKey: testPrivateKey,
349348
}
350349

351-
await expect(runCarImport(options)).rejects.toThrow('process.exit called')
352-
expect(consoleMocks.error).toHaveBeenCalledWith('Import cancelled')
350+
await expect(runCarImport(options)).rejects.toThrow()
353351
})
354352
})
355353

@@ -394,7 +392,7 @@ describe('CAR Import', () => {
394392
// No private key provided
395393
}
396394

397-
await expect(runCarImport(options)).rejects.toThrow('process.exit called')
395+
await expect(runCarImport(options)).rejects.toThrow()
398396
// The error is caught and logged generically as "Import failed"
399397
expect(consoleMocks.error).toHaveBeenCalled()
400398
})
@@ -493,7 +491,7 @@ describe('CAR Import', () => {
493491
privateKey: testPrivateKey,
494492
}
495493

496-
await expect(runCarImport(options)).rejects.toThrow('process.exit called')
494+
await expect(runCarImport(options)).rejects.toThrow()
497495
expect(cleanupSpy).toHaveBeenCalled()
498496
})
499497
})

0 commit comments

Comments
 (0)