Skip to content

Commit 191bd0a

Browse files
authored
fix: diff exit code (#68)
1 parent 198e626 commit 191bd0a

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/commands/diff.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,18 @@ export const builder = (args: Argv): Argv<DiffOptions> => {
6464
})
6565
.fail((msg, err) => {
6666
if (msg) {
67+
// TODO: should refactor console message
6768
console.error(msg)
6869
process.exit(1)
6970
} else {
7071
if (err instanceof DiffError) {
72+
// TODO: should refactor console message
7173
console.warn(err.message)
72-
process.exit(1)
74+
process.exit(64)
7375
} else {
74-
if (err) throw err
76+
// TODO: should refactor console message
77+
console.error(err.message)
78+
process.exit(1)
7579
}
7680
}
7781
})
@@ -83,37 +87,31 @@ export const handler = async (args: Arguments<DiffOptions>): Promise<unknown> =>
8387
const ProviderFactory = loadProvider(args.provider)
8488

8589
if (ProviderFactory === null) {
86-
// TODO: should refactor console message
87-
console.log(`Not found ${args.provider} provider`)
88-
return
90+
throw new Error(`Not found ${args.provider} provider`)
8991
}
9092

9193
if (!args.target && !args.targetPaths) {
9294
// TODO: should refactor console message
93-
console.log('You need to specify either --target or --target-paths')
94-
return
95+
throw new Error('You need to specify either --target or --target-paths')
9596
}
9697

9798
const confPath = resolveProviderConf(args.provider, args.conf)
9899
const conf = loadProviderConf(confPath) || DEFUALT_CONF
99100

100-
let localeMessages
101-
try {
102-
localeMessages = getLocaleMessages(args)
103-
} catch (e) {
104-
console.log(e.message)
105-
return
106-
}
101+
const localeMessages = getLocaleMessages(args)
107102

108103
const provider = ProviderFactory(conf)
109104
const locales = Object.keys(localeMessages) as Locale[]
110105
const serviceMessages = await provider.pull({ locales, dryRun: false, normalize, format })
106+
111107
const ret = diffString(localeMessages, serviceMessages)
112108
console.log(ret)
113109

114-
return !ret
115-
? Promise.reject(new DiffError('There are differences!'))
116-
: Promise.resolve('No difference!')
110+
if (ret) {
111+
throw new DiffError('There are differences!')
112+
}
113+
114+
return Promise.resolve()
117115
}
118116

119117
export default {

test/commands/diff.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ const PROCESS_CWD_TARGET_PATH = path.resolve(__dirname)
2727
let orgCwd // for process.cwd mock
2828
let orgExit // for process.exit mock
2929
let spyLog
30+
let spyWarn
3031
let spyError
3132
beforeEach(() => {
3233
spyLog = jest.spyOn(global.console, 'log')
34+
spyWarn = jest.spyOn(global.console, 'warn')
3335
spyError = jest.spyOn(global.console, 'error')
3436
orgCwd = process.cwd
3537
process.cwd = jest.fn(() => PROCESS_CWD_TARGET_PATH) // mock: process.cwd
@@ -38,6 +40,7 @@ beforeEach(() => {
3840

3941
afterEach(() => {
4042
spyError.mockRestore()
43+
spyWarn.mockRestore()
4144
spyLog.mockRestore()
4245
jest.clearAllMocks()
4346
process.exit = orgExit
@@ -72,7 +75,7 @@ test('--provider: not found', async () => {
7275
})
7376

7477
// verify
75-
expect(spyLog).toHaveBeenCalledWith('Not found ./404-provider.js provider')
78+
expect(spyError).toHaveBeenCalledWith('Not found ./404-provider.js provider')
7679
})
7780

7881
test('not specified --target and --targetPaths', async () => {
@@ -86,7 +89,7 @@ test('not specified --target and --targetPaths', async () => {
8689
})
8790

8891
// verify
89-
expect(spyLog).toHaveBeenCalledWith('You need to specify either --target or --target-paths')
92+
expect(spyError).toHaveBeenCalledWith('You need to specify either --target or --target-paths')
9093
})
9194

9295
test('--target option', async () => {
@@ -111,6 +114,9 @@ test('--target option', async () => {
111114

112115
// verify with snapshot
113116
expect(spyLog.mock.calls[0][0]).toMatchSnapshot()
117+
// NOTE: cannot detect process.exit calling in `fail` ...
118+
// expect(spyWarn).toHaveBeenCalledWith('There are differences!')
119+
// expect(process.exit).toHaveBeenCalledWith(64)
114120
})
115121

116122
test('--locale option', async () => {
@@ -135,6 +141,9 @@ test('--locale option', async () => {
135141

136142
// verify with snapshot
137143
expect(spyLog.mock.calls[0][0]).toMatchSnapshot()
144+
// NOTE: cannot detect process.exit calling in `fail` ...
145+
// expect(spyWarn).toHaveBeenCalledWith('There are differences!')
146+
// expect(process.exit).toHaveBeenCalledWith(64)
138147
})
139148

140149
test('--target-paths option', async () => {
@@ -159,4 +168,7 @@ test('--target-paths option', async () => {
159168

160169
// verify with snapshot
161170
expect(spyLog.mock.calls[1][0]).toMatchSnapshot()
171+
// NOTE: cannot detect process.exit calling in `fail` ...
172+
// expect(spyWarn).toHaveBeenCalledWith('There are differences!')
173+
// expect(process.exit).toHaveBeenCalledWith(64)
162174
})

0 commit comments

Comments
 (0)