Skip to content

Commit 84332cd

Browse files
authored
refactor: align add command with clean CLI separation pattern (#273)
* refactor: clean separation pattern for add cli command * fix: little changes
1 parent b8e98dc commit 84332cd

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

src/add/add.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export async function runAdd(options: AddOptions): Promise<AddResult> {
8787
const proceed = await warnAboutCDNPricingLimitations()
8888
if (!proceed) {
8989
cancel('Add cancelled')
90-
process.exitCode = 1
9190
throw new Error('CDN pricing limitations warning cancelled')
9291
}
9392
}
@@ -102,7 +101,7 @@ export async function runAdd(options: AddOptions): Promise<AddResult> {
102101
if (!pathValidation.exists || !pathValidation.stats) {
103102
spinner.stop(`${pc.red('✗')} ${pathValidation.error}`)
104103
cancel('Add cancelled')
105-
process.exit(1)
104+
throw new Error(pathValidation.error)
106105
}
107106

108107
const pathStat = pathValidation.stats
@@ -249,6 +248,6 @@ export async function runAdd(options: AddOptions): Promise<AddResult> {
249248
}
250249

251250
cancel('Add failed')
252-
process.exit(1)
251+
throw error
253252
}
254253
}

src/commands/add.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ export const addCommand = new Command('add')
3030
}
3131

3232
await runAdd(addOptions)
33-
} catch (error) {
34-
console.error('Add failed:', error instanceof Error ? error.message : error)
33+
} catch {
3534
process.exit(1)
3635
}
3736
})

src/test/unit/add.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,51 +233,45 @@ describe('Add Command', () => {
233233
})
234234

235235
it('should reject when file does not exist', async () => {
236-
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => {
237-
throw new Error('process.exit called')
238-
})
236+
const mockExit = vi.spyOn(process, 'exit')
239237

240238
await expect(
241239
runAdd({
242240
filePath: '/non/existent/file.txt',
243241
privateKey: 'test-key',
244242
})
245-
).rejects.toThrow('process.exit called')
243+
).rejects.toThrow('Path not found')
246244

247-
expect(mockExit).toHaveBeenCalledWith(1)
245+
expect(mockExit).not.toHaveBeenCalled()
248246
mockExit.mockRestore()
249247
})
250248

251249
it('should reject when private key is missing', async () => {
252-
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => {
253-
throw new Error('process.exit called')
254-
})
250+
const mockExit = vi.spyOn(process, 'exit')
255251

256252
await expect(
257253
runAdd({
258254
filePath: testFile,
259255
// No private key
260256
})
261-
).rejects.toThrow('process.exit called')
257+
).rejects.toThrow()
262258

263-
expect(mockExit).toHaveBeenCalledWith(1)
259+
expect(mockExit).not.toHaveBeenCalled()
264260
mockExit.mockRestore()
265261
})
266262

267263
it('should reject --bare flag with directories', async () => {
268-
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => {
269-
throw new Error('process.exit called')
270-
})
264+
const mockExit = vi.spyOn(process, 'exit')
271265

272266
await expect(
273267
runAdd({
274268
filePath: testDir, // Directory
275269
privateKey: 'test-key',
276270
bare: true, // --bare flag should not work with directories
277271
})
278-
).rejects.toThrow('process.exit called')
272+
).rejects.toThrow('--bare flag is not supported for directories')
279273

280-
expect(mockExit).toHaveBeenCalledWith(1)
274+
expect(mockExit).not.toHaveBeenCalled()
281275
mockExit.mockRestore()
282276
})
283277
})

0 commit comments

Comments
 (0)