Skip to content

Commit 0ae4f18

Browse files
authored
breaking: change the provider push and pull I/F (#40)
1 parent 2e3c91d commit 0ae4f18

File tree

5 files changed

+104
-56
lines changed

5 files changed

+104
-56
lines changed

src/commands/pull.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const builder = (args: Argv): Argv<PullOptions> => {
6969
}
7070

7171
export const handler = async (args: Arguments<PullOptions>): Promise<unknown> => {
72+
const { dryRun, normalize } = args
7273
const ProviderFactory = loadProvider(args.provider)
7374

7475
if (ProviderFactory === null) {
@@ -89,7 +90,7 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
8990
try {
9091
const locales = args.locales?.split(',').filter(p => p) as Locale[] || []
9192
const provider = ProviderFactory(conf)
92-
const resource = await provider.pull(locales, args.dryRun, args.normalize)
93+
const resource = await provider.pull({ locales, dryRun, normalize })
9394
await applyPullResource(args.output, resource, args.dryRun)
9495
// TODO: should refactor console message
9596
console.log('pull success')

src/commands/push.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const builder = (args: Argv): Argv<PushOptions> => {
8080
}
8181

8282
export const handler = async (args: Arguments<PushOptions>): Promise<unknown> => {
83+
const { dryRun, normalize } = args
8384
const ProviderFactory = loadProvider(args.provider)
8485

8586
if (ProviderFactory === null) {
@@ -107,7 +108,7 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
107108

108109
try {
109110
const provider = ProviderFactory(conf)
110-
await provider.push(resource, args.dryRun, args.normalize)
111+
await provider.push({ resource, dryRun, normalize })
111112
// TODO: should refactor console message
112113
console.log('push success')
113114
} catch (e) {

test/commands/pull.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test('--provider: not found', async () => {
8585

8686
test('--conf option', async () => {
8787
// setup mocks
88-
mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}}))
88+
mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: {}, en: {}}))
8989

9090
// run
9191
const pull = await import('../../src/commands/pull')
@@ -125,7 +125,7 @@ test('--conf option omit', async () => {
125125

126126
test('--locales option', async () => {
127127
// setup mocks
128-
mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}}))
128+
mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: {}, en: {}}))
129129

130130
// run
131131
const pull = await import('../../src/commands/pull')
@@ -139,12 +139,16 @@ test('--locales option', async () => {
139139
})
140140
})
141141

142-
expect(mockPull).toHaveBeenCalledWith(['en', 'ja', 'fr'], true, undefined)
142+
expect(mockPull).toHaveBeenCalledWith({
143+
locales: ['en', 'ja', 'fr'],
144+
dryRun: true,
145+
normalize: undefined
146+
})
143147
})
144148

145149
test('--output option', async () => {
146150
// setup mocks
147-
mockPull.mockImplementation(locales => Promise.resolve({ ja: { hello: 'hello' }, en: { hello: 'こんにちわわわ!' }}))
151+
mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: { hello: 'hello' }, en: { hello: 'こんにちわわわ!' }}))
148152
const mockFS = fs as jest.Mocked<typeof fs>
149153
mockFS.mkdir.mockImplementation((p, option, cb) => cb(null))
150154
mockFS.writeFile.mockImplementation((p, data, cb) => cb(null))
@@ -167,7 +171,7 @@ test('--output option', async () => {
167171

168172
test('--normalize option', async () => {
169173
// setup mocks
170-
mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}}))
174+
mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: {}, en: {}}))
171175

172176
// run
173177
const pull = await import('../../src/commands/pull')
@@ -180,5 +184,9 @@ test('--normalize option', async () => {
180184
})
181185
})
182186

183-
expect(mockPull).toHaveBeenCalledWith([], false, 'hierarchy')
187+
expect(mockPull).toHaveBeenCalledWith({
188+
locales: [],
189+
dryRun: false,
190+
normalize: 'hierarchy'
191+
})
184192
})

test/commands/push.test.ts

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test('not specified --target and --targetPaths', async () => {
8282

8383
test('--target option', async () => {
8484
// setup mocks
85-
mockPush.mockImplementation(resource => true)
85+
mockPush.mockImplementation(({ resource }) => true)
8686

8787
// run
8888
const push = await import('../../src/commands/push')
@@ -94,16 +94,20 @@ test('--target option', async () => {
9494
})
9595

9696
expect(mockPush).toHaveBeenCalledWith({
97-
messages: {
98-
en: { hello: 'world' }
97+
resource: {
98+
messages: {
99+
en: { hello: 'world' }
100+
},
101+
mode: 'locale-message'
99102
},
100-
mode: 'locale-message'
101-
}, false, undefined)
103+
dryRun: false,
104+
normalize: undefined
105+
})
102106
})
103107

104108
test('--locale option', async () => {
105109
// setup mocks
106-
mockPush.mockImplementation(resource => true)
110+
mockPush.mockImplementation(({ resource }) => true)
107111

108112
// run
109113
const push = await import('../../src/commands/push')
@@ -115,16 +119,20 @@ test('--locale option', async () => {
115119
})
116120

117121
expect(mockPush).toHaveBeenCalledWith({
118-
messages: {
119-
ja: { hello: '世界' }
122+
resource: {
123+
messages: {
124+
ja: { hello: '世界' }
125+
},
126+
mode: 'locale-message'
120127
},
121-
mode: 'locale-message'
122-
}, false, undefined)
128+
dryRun: false,
129+
normalize: undefined
130+
})
123131
})
124132

125133
test('--conf option', async () => {
126134
// setup mocks
127-
mockPush.mockImplementation(reosurce => true)
135+
mockPush.mockImplementation(({ reosurce }) => true)
128136

129137
// run
130138
const TARGET_LOCALE = './test/fixtures/locales/en.json'
@@ -141,12 +149,16 @@ test('--conf option', async () => {
141149
pushMode: 'file-path'
142150
})
143151
expect(mockPush).toHaveBeenCalledWith({
144-
files: [{
145-
locale: 'en',
146-
path: path.resolve(TARGET_LOCALE)
147-
}],
148-
mode: 'file-path'
149-
}, false, undefined)
152+
resource: {
153+
files: [{
154+
locale: 'en',
155+
path: path.resolve(TARGET_LOCALE)
156+
}],
157+
mode: 'file-path'
158+
},
159+
dryRun: false,
160+
normalize: undefined
161+
})
150162
})
151163

152164
test('--conf option omit', async () => {
@@ -168,7 +180,7 @@ test('--conf option omit', async () => {
168180

169181
test('--target-paths option', async () => {
170182
// setup mocks
171-
mockPush.mockImplementation(resource => true)
183+
mockPush.mockImplementation(({ resource }) => true)
172184

173185
// run
174186
const push = await import('../../src/commands/push')
@@ -180,25 +192,29 @@ test('--target-paths option', async () => {
180192
})
181193

182194
expect(mockPush).toHaveBeenCalledWith({
183-
messages: {
184-
en: {
185-
hello: 'world'
195+
resource: {
196+
messages: {
197+
en: {
198+
hello: 'world'
199+
},
200+
lang: {
201+
hello: '世界'
202+
},
203+
ja: {
204+
hello: 'こんにちわわわ!',
205+
world: 'ザ・ワールド'
206+
}
186207
},
187-
lang: {
188-
hello: '世界'
189-
},
190-
ja: {
191-
hello: 'こんにちわわわ!',
192-
world: 'ザ・ワールド'
193-
}
208+
mode: 'locale-message'
194209
},
195-
mode: 'locale-message'
196-
}, false, undefined)
210+
dryRun: false,
211+
normalize: undefined
212+
})
197213
})
198214

199215
test('not specified --filename-match', async () => {
200216
// setup mocks
201-
mockPush.mockImplementation(resource => true)
217+
mockPush.mockImplementation(({ resource }) => true)
202218

203219
// run
204220
const push = await import('../../src/commands/push')
@@ -214,7 +230,7 @@ test('not specified --filename-match', async () => {
214230

215231
test('--dry-run option', async () => {
216232
// setup mocks
217-
mockPush.mockImplementation(resource => false)
233+
mockPush.mockImplementation(({ resource }) => false)
218234

219235
// run
220236
const push = await import('../../src/commands/push')
@@ -226,16 +242,20 @@ test('--dry-run option', async () => {
226242
})
227243

228244
expect(mockPush).toHaveBeenCalledWith({
229-
messages: {
230-
ja: { hello: '世界' }
245+
resource: {
246+
messages: {
247+
ja: { hello: '世界' }
248+
},
249+
mode: 'locale-message'
231250
},
232-
mode: 'locale-message'
233-
}, true, undefined)
251+
dryRun: true,
252+
normalize: undefined
253+
})
234254
})
235255

236256
test('--normalize option', async () => {
237257
// setup mocks
238-
mockPush.mockImplementation(resource => false)
258+
mockPush.mockImplementation(({ resource }) => false)
239259

240260
// run
241261
const push = await import('../../src/commands/push')
@@ -247,9 +267,13 @@ test('--normalize option', async () => {
247267
})
248268

249269
expect(mockPush).toHaveBeenCalledWith({
250-
messages: {
251-
ja: { hello: '世界' }
270+
resource: {
271+
messages: {
272+
ja: { hello: '世界' }
273+
},
274+
mode: 'locale-message'
252275
},
253-
mode: 'locale-message'
254-
}, false, 'flat')
276+
dryRun: false,
277+
normalize: 'flat'
278+
})
255279
})

types/index.d.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,33 @@ export type ProviderFactory<T = {}> = (configration: ProviderConfiguration<T>) =
167167
export interface Provider {
168168
/**
169169
* push the resource to localization service
170-
* @param resource the resource that push to localization service
171-
* @param dryRun whether the CLI run as dryRun mode
172170
*/
173-
push (resource: ProviderPushResource, dryRun: boolean, normalize?: string): Promise<void>
171+
push (args: PushArguments): Promise<void>
174172
/**
175173
* pull the resource from localization service
176-
* @param locales locales that pull from localization service, if empty, you must pull the all locale messages
177-
* @param dryRun whether the CLI run as dryRun mode
178-
* @returns the resource of localization service
179174
*/
180-
pull (locales: Locale[], dryRun: boolean, normalize?: string): Promise<ProviderPullResource>
175+
pull (args: PullArguments): Promise<ProviderPullResource>
181176
}
182177

178+
type CommonArguments = {
179+
dryRun: boolean // whether the CLI run as dryRun mode
180+
normalize?: string // normalization ways for locale messages or resource
181+
}
182+
183+
/**
184+
* Provider Push Arguments
185+
*/
186+
export type PushArguments = {
187+
resource: ProviderPushResource // the resource that push to localization service
188+
} & CommonArguments
189+
190+
/**
191+
* Provider Pull Arguments
192+
*/
193+
export type PullArguments = {
194+
locales: Locale[] // locales that pull from localization service, if empty, you must pull the all locale messages
195+
} & CommonArguments
196+
183197
/**
184198
* mode that can be processed with provider push
185199
* - 'file-path':

0 commit comments

Comments
 (0)