Skip to content

Commit f592065

Browse files
authored
--format option feature (#52)
1 parent 61af5b8 commit f592065

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/commands/pull.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type PullOptions = {
2323
output: string
2424
locales?: string
2525
normalize?: string
26+
format: string
2627
dryRun: boolean
2728
}
2829

@@ -60,6 +61,12 @@ export const builder = (args: Argv): Argv<PullOptions> => {
6061
alias: 'n',
6162
describe: 'option for the locale messages structure, you can specify the option, if you hope to normalize for the provider.'
6263
})
64+
.option('format', {
65+
type: 'string',
66+
alias: 'f',
67+
default: 'json',
68+
describe: 'option for the locale messages format, default `json`'
69+
})
6370
.option('dryRun', {
6471
type: 'boolean',
6572
alias: 'd',
@@ -69,7 +76,7 @@ export const builder = (args: Argv): Argv<PullOptions> => {
6976
}
7077

7178
export const handler = async (args: Arguments<PullOptions>): Promise<unknown> => {
72-
const { dryRun, normalize } = args
79+
const { dryRun, normalize, format } = args
7380
const ProviderFactory = loadProvider(args.provider)
7481

7582
if (ProviderFactory === null) {
@@ -90,7 +97,7 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
9097
try {
9198
const locales = args.locales?.split(',').filter(p => p) as Locale[] || []
9299
const provider = ProviderFactory(conf)
93-
const messages = await provider.pull({ locales, dryRun, normalize })
100+
const messages = await provider.pull({ locales, dryRun, normalize, format })
94101
await applyPullLocaleMessages(args.output, messages, args.dryRun)
95102
// TODO: should refactor console message
96103
console.log('pull success')

test/commands/pull.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ test('--locales option', async () => {
139139

140140
expect(mockPull).toHaveBeenCalledWith({
141141
locales: ['en', 'ja', 'fr'],
142+
format: 'json',
142143
dryRun: true,
143144
normalize: undefined
144145
})
@@ -184,7 +185,31 @@ test('--normalize option', async () => {
184185

185186
expect(mockPull).toHaveBeenCalledWith({
186187
locales: [],
188+
format: 'json',
187189
dryRun: false,
188190
normalize: 'hierarchy'
189191
})
190192
})
193+
194+
test('--format option', async () => {
195+
// setup mocks
196+
mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: {}, en: {}}))
197+
198+
// run
199+
const pull = await import('../../src/commands/pull')
200+
const cmd = yargs.command(pull)
201+
await new Promise((resolve, reject) => {
202+
cmd.parse(`pull --provider=@scope/l10n-service-provider \
203+
--output=./test/fixtures/locales \
204+
--format=xliff`, (err, argv, output) => {
205+
err ? reject(err) : resolve(output)
206+
})
207+
})
208+
209+
expect(mockPull).toHaveBeenCalledWith({
210+
locales: [],
211+
format: 'xliff',
212+
dryRun: false,
213+
normalize: undefined
214+
})
215+
})

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export type PushArguments = {
212212
*/
213213
export type PullArguments = {
214214
locales: Locale[] // locales that pull from localization service, if empty, you must pull all locale messages
215+
format: string // locale messages format
215216
} & CommonArguments
216217

217218
/**

0 commit comments

Comments
 (0)