Skip to content

Commit 2e3c91d

Browse files
authored
support --normalize option (#39)
* add nomalize option * update type definition and add tests
1 parent bdd83d3 commit 2e3c91d

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

src/commands/pull.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type PullOptions = {
2222
conf?: string
2323
output: string
2424
locales?: string
25+
normalize?: string
2526
dryRun: boolean
2627
}
2728

@@ -54,6 +55,11 @@ export const builder = (args: Argv): Argv<PullOptions> => {
5455
default: '',
5556
describe: `option for some locales of locale messages, you can also be specified multi locale with comma delimiter. if it's not specified pull all locale messages`
5657
})
58+
.option('normalize', {
59+
type: 'string',
60+
alias: 'n',
61+
describe: 'option for the locale messages structure, you can specify the option, if you hope to normalize for the provider.'
62+
})
5763
.option('dryRun', {
5864
type: 'boolean',
5965
alias: 'd',
@@ -83,7 +89,7 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
8389
try {
8490
const locales = args.locales?.split(',').filter(p => p) as Locale[] || []
8591
const provider = ProviderFactory(conf)
86-
const resource = await provider.pull(locales, args.dryRun)
92+
const resource = await provider.pull(locales, args.dryRun, args.normalize)
8793
await applyPullResource(args.output, resource, args.dryRun)
8894
// TODO: should refactor console message
8995
console.log('pull success')

src/commands/push.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type PushOptions = {
2525
locale?: string
2626
targetPaths?: string
2727
filenameMatch?: string
28+
normalize?: string
2829
dryRun: boolean
2930
}
3031

@@ -65,6 +66,11 @@ export const builder = (args: Argv): Argv<PushOptions> => {
6566
alias: 'm',
6667
describe: `option should be accepted a regex filenames, must be specified together --targets if it's directory path of locale messages`
6768
})
69+
.option('normalize', {
70+
type: 'string',
71+
alias: 'n',
72+
describe: 'option for the locale messages structure, you can specify the option, if you hope to normalize for the provider.'
73+
})
6874
.option('dryRun', {
6975
type: 'boolean',
7076
alias: 'd',
@@ -101,7 +107,7 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
101107

102108
try {
103109
const provider = ProviderFactory(conf)
104-
await provider.push(resource, args.dryRun)
110+
await provider.push(resource, args.dryRun, args.normalize)
105111
// TODO: should refactor console message
106112
console.log('push success')
107113
} catch (e) {

test/commands/pull.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ test('--locales option', async () => {
139139
})
140140
})
141141

142-
expect(mockPull).toHaveBeenCalledWith(['en', 'ja', 'fr'], true)
142+
expect(mockPull).toHaveBeenCalledWith(['en', 'ja', 'fr'], true, undefined)
143143
})
144144

145145
test('--output option', async () => {
@@ -164,3 +164,21 @@ test('--output option', async () => {
164164
expect(mockFS.mkdir.mock.calls[0][0]).toEqual(OUTPUT_FULL_PATH)
165165
expect(mockFS.mkdir.mock.calls[0][1]).toEqual({ recursive: true })
166166
})
167+
168+
test('--normalize option', async () => {
169+
// setup mocks
170+
mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}}))
171+
172+
// run
173+
const pull = await import('../../src/commands/pull')
174+
const cmd = yargs.command(pull)
175+
await new Promise((resolve, reject) => {
176+
cmd.parse(`pull --provider=@scope/l10n-service-provider \
177+
--output=./test/fixtures/locales \
178+
--normalize=hierarchy`, (err, argv, output) => {
179+
err ? reject(err) : resolve(output)
180+
})
181+
})
182+
183+
expect(mockPull).toHaveBeenCalledWith([], false, 'hierarchy')
184+
})

test/commands/push.test.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test('--target option', async () => {
9898
en: { hello: 'world' }
9999
},
100100
mode: 'locale-message'
101-
}, false)
101+
}, false, undefined)
102102
})
103103

104104
test('--locale option', async () => {
@@ -119,7 +119,7 @@ test('--locale option', async () => {
119119
ja: { hello: '世界' }
120120
},
121121
mode: 'locale-message'
122-
}, false)
122+
}, false, undefined)
123123
})
124124

125125
test('--conf option', async () => {
@@ -146,7 +146,7 @@ test('--conf option', async () => {
146146
path: path.resolve(TARGET_LOCALE)
147147
}],
148148
mode: 'file-path'
149-
}, false)
149+
}, false, undefined)
150150
})
151151

152152
test('--conf option omit', async () => {
@@ -193,7 +193,7 @@ test('--target-paths option', async () => {
193193
}
194194
},
195195
mode: 'locale-message'
196-
}, false)
196+
}, false, undefined)
197197
})
198198

199199
test('not specified --filename-match', async () => {
@@ -230,5 +230,26 @@ test('--dry-run option', async () => {
230230
ja: { hello: '世界' }
231231
},
232232
mode: 'locale-message'
233-
}, true)
233+
}, true, undefined)
234+
})
235+
236+
test('--normalize option', async () => {
237+
// setup mocks
238+
mockPush.mockImplementation(resource => false)
239+
240+
// run
241+
const push = await import('../../src/commands/push')
242+
const cmd = yargs.command(push)
243+
await new Promise((resolve, reject) => {
244+
cmd.parse(`push --provider=@scope/l10n-service-provider --target=./test/fixtures/locales/lang.json --locale=ja --normalize=flat`, (err, argv, output) => {
245+
err ? reject(err) : resolve(output)
246+
})
247+
})
248+
249+
expect(mockPush).toHaveBeenCalledWith({
250+
messages: {
251+
ja: { hello: '世界' }
252+
},
253+
mode: 'locale-message'
254+
}, false, 'flat')
234255
})

types/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,19 @@ export interface Provider {
170170
* @param resource the resource that push to localization service
171171
* @param dryRun whether the CLI run as dryRun mode
172172
*/
173-
push (resource: ProviderPushResource, dryRun: boolean): Promise<void>
173+
push (resource: ProviderPushResource, dryRun: boolean, normalize?: string): Promise<void>
174174
/**
175175
* pull the resource from localization service
176176
* @param locales locales that pull from localization service, if empty, you must pull the all locale messages
177177
* @param dryRun whether the CLI run as dryRun mode
178178
* @returns the resource of localization service
179179
*/
180-
pull (locales: Locale[], dryRun: boolean): Promise<ProviderPullResource>
180+
pull (locales: Locale[], dryRun: boolean, normalize?: string): Promise<ProviderPullResource>
181181
}
182182

183183
/**
184-
* mode that can be processed with provider push
185-
* - 'file-path':
184+
* mode that can be processed with provider push
185+
* - 'file-path':
186186
* To use when the provider uses the locale message directly from the file.
187187
* for example, used for file uploading.
188188
* When specified that mode, `ProviderPushResource` are passed from the CLI as `files`.

0 commit comments

Comments
 (0)