Skip to content

Commit c173e3b

Browse files
authored
feat: providerArgs option to the push & pull command (#169)
* feat: providerArgs option to the push & pull command * feat: unit test for provideArgs * refactor * fix: change the type of providerArgs from string to object * fix: type definition
1 parent 3408e22 commit c173e3b

File tree

7 files changed

+107
-5
lines changed

7 files changed

+107
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"js-yaml": "^3.13.1",
3636
"json-diff": "^0.5.4",
3737
"json5": "^2.1.0",
38+
"query-string": "^7.0.1",
3839
"vue-template-compiler": "^2.6.10",
3940
"yargs": "^15.1.0"
4041
},

src/commands/pull.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs'
22
import * as path from 'path'
33
import { promisify } from 'util'
44
import { Arguments, Argv } from 'yargs'
5+
import querystring from 'query-string'
56

67
import { debug as Debug } from 'debug'
78
const debug = Debug('vue-i18n-locale-message:commands:pull')
@@ -25,6 +26,7 @@ type PullOptions = {
2526
normalize?: string
2627
format: string
2728
dryRun: boolean
29+
providerArgs?: string
2830
}
2931

3032
export const command = 'pull'
@@ -73,10 +75,15 @@ export const builder = (args: Argv): Argv<PullOptions> => {
7375
default: false,
7476
describe: 'run the pull command, but do not pull to locale messages of localization service'
7577
})
78+
.option('providerArgs', {
79+
type: 'string',
80+
alias: 'pa',
81+
describe: `option to give parameters to the provider by using strings like URL query parameters (e.g. arg1=1&arg2=2).`
82+
})
7683
}
7784

7885
export const handler = async (args: Arguments<PullOptions>): Promise<unknown> => {
79-
const { dryRun, normalize, format } = args
86+
const { dryRun, normalize, format, providerArgs } = args
8087
const ProviderFactory = loadProvider(args.provider)
8188

8289
if (ProviderFactory === null) {
@@ -97,7 +104,10 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
97104
try {
98105
const locales = args.locales?.split(',').filter(p => p) as Locale[] || []
99106
const provider = ProviderFactory(conf)
100-
const messages = await provider.pull({ locales, dryRun, normalize, format })
107+
const messages = await provider.pull({
108+
locales, dryRun, normalize, format,
109+
providerArgs: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined
110+
})
101111
await applyPullLocaleMessages(args.output, messages, args.dryRun)
102112
// TODO: should refactor console message
103113
console.log('pull success')

src/commands/push.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Arguments, Argv } from 'yargs'
2+
import querystring from 'query-string'
23

34
import {
45
resolveProviderConf,
@@ -14,6 +15,7 @@ type PushOptions = {
1415
conf?: string
1516
normalize?: string
1617
dryRun: boolean
18+
providerArgs?: string
1719
} & PushableOptions
1820

1921
export const command = 'push'
@@ -64,10 +66,15 @@ export const builder = (args: Argv): Argv<PushOptions> => {
6466
default: false,
6567
describe: `run the push command, but do not apply to locale messages of localization service`
6668
})
69+
.option('providerArgs', {
70+
type: 'string',
71+
alias: 'pa',
72+
describe: `option to give parameters to the provider by using strings like URL query parameters (e.g. arg1=1&arg2=2).`
73+
})
6774
}
6875

6976
export const handler = async (args: Arguments<PushOptions>): Promise<unknown> => {
70-
const { dryRun, normalize } = args
77+
const { dryRun, normalize, providerArgs } = args
7178
const ProviderFactory = loadProvider(args.provider)
7279

7380
if (ProviderFactory === null) {
@@ -88,7 +95,10 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
8895
try {
8996
const messages = getLocaleMessages(args)
9097
const provider = ProviderFactory(conf)
91-
await provider.push({ messages, dryRun, normalize })
98+
await provider.push({
99+
messages, dryRun, normalize,
100+
providerArgs: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined
101+
})
92102
// TODO: should refactor console message
93103
console.log('push success')
94104
} catch (e) {

test/commands/pull.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,30 @@ test('--format option', async () => {
213213
normalize: undefined
214214
})
215215
})
216+
217+
test('--providerArgs option', async () => {
218+
// setup mocks
219+
mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: {}, en: {}}))
220+
221+
// run
222+
const pull = await import('../../src/commands/pull')
223+
const cmd = yargs.command(pull)
224+
await new Promise((resolve, reject) => {
225+
cmd.parse(`pull --provider=@scope/l10n-service-provider \
226+
--output=./test/fixtures/locales \
227+
--providerArgs=arg1=1&arg2=2`, (err, argv, output) => {
228+
err ? reject(err) : resolve(output)
229+
})
230+
})
231+
232+
expect(mockPull).toHaveBeenCalledWith({
233+
locales: [],
234+
format: 'json',
235+
dryRun: false,
236+
normalize: undefined,
237+
providerArgs: Object({
238+
arg1: '1',
239+
arg2: '2'
240+
})
241+
})
242+
})

test/commands/push.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,31 @@ test('--normalize option', async () => {
266266
normalize: 'flat'
267267
})
268268
})
269+
270+
test('--providerArgs option', async () => {
271+
// setup mocks
272+
mockPush.mockImplementation(({ resource }) => true)
273+
274+
// run
275+
const push = await import('../../src/commands/push')
276+
const cmd = yargs.command(push)
277+
await new Promise((resolve, reject) => {
278+
cmd.parse(`push --provider=@scope/l10n-service-provider \
279+
--target=./test/fixtures/locales/en.json \
280+
--providerArgs=arg1=1&arg2=2`, (err, argv, output) => {
281+
err ? reject(err) : resolve(output)
282+
})
283+
})
284+
285+
expect(mockPush).toHaveBeenCalledWith({
286+
messages: {
287+
en: { hello: 'world' }
288+
},
289+
dryRun: false,
290+
normalize: undefined,
291+
providerArgs: {
292+
arg1: '1',
293+
arg2: '2'
294+
}
295+
})
296+
})

types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ export interface Provider {
223223
export (args: ExportArguments): Promise<RawLocaleMessage[]>
224224
}
225225

226-
type CommonArguments = {
226+
type CommonArguments<T extends Record<string, any> = {}> = {
227227
dryRun: boolean // whether the CLI run as dryRun mode
228228
normalize?: string // normalization ways for locale messages or resource
229+
providerArgs?: T // parameters to give to the provider
229230
}
230231

231232
/**

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,11 @@ fill-range@^7.0.1:
26542654
dependencies:
26552655
to-regex-range "^5.0.1"
26562656

2657+
filter-obj@^1.1.0:
2658+
version "1.1.0"
2659+
resolved "https://registry.yarnpkg.com/filter-obj/-/filter-obj-1.1.0.tgz#9b311112bc6c6127a16e016c6c5d7f19e0805c5b"
2660+
integrity sha1-mzERErxsYSehbgFsbF1/GeCAXFs=
2661+
26572662
find-up@^1.0.0:
26582663
version "1.1.2"
26592664
resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f"
@@ -5266,6 +5271,16 @@ q@^1.5.1:
52665271
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
52675272
integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=
52685273

5274+
query-string@^7.0.1:
5275+
version "7.0.1"
5276+
resolved "https://registry.yarnpkg.com/query-string/-/query-string-7.0.1.tgz#45bd149cf586aaa582dffc7ec7a8ad97dd02f75d"
5277+
integrity sha512-uIw3iRvHnk9to1blJCG3BTc+Ro56CBowJXKmNNAm3RulvPBzWLRqKSiiDk+IplJhsydwtuNMHi8UGQFcCLVfkA==
5278+
dependencies:
5279+
decode-uri-component "^0.2.0"
5280+
filter-obj "^1.1.0"
5281+
split-on-first "^1.0.0"
5282+
strict-uri-encode "^2.0.0"
5283+
52695284
queue-microtask@^1.2.2:
52705285
version "1.2.3"
52715286
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"
@@ -5902,6 +5917,11 @@ spdx-license-ids@^3.0.0:
59025917
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.9.tgz#8a595135def9592bda69709474f1cbeea7c2467f"
59035918
integrity sha512-Ki212dKK4ogX+xDo4CtOZBVIwhsKBEfsEEcwmJfLQzirgc2jIWdzg40Unxz/HzEUqM1WFzVlQSMF9kZZ2HboLQ==
59045919

5920+
split-on-first@^1.0.0:
5921+
version "1.1.0"
5922+
resolved "https://registry.yarnpkg.com/split-on-first/-/split-on-first-1.1.0.tgz#f610afeee3b12bce1d0c30425e76398b78249a5f"
5923+
integrity sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw==
5924+
59055925
split-string@^3.0.1, split-string@^3.0.2:
59065926
version "3.1.0"
59075927
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"
@@ -5951,6 +5971,11 @@ static-extend@^0.1.1:
59515971
define-property "^0.2.5"
59525972
object-copy "^0.1.0"
59535973

5974+
strict-uri-encode@^2.0.0:
5975+
version "2.0.0"
5976+
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546"
5977+
integrity sha1-ucczDHBChi9rFC3CdLvMWGbONUY=
5978+
59545979
string-length@^4.0.1:
59555980
version "4.0.2"
59565981
resolved "https://registry.yarnpkg.com/string-length/-/string-length-4.0.2.tgz#a8a8dc7bd5c1a82b9b3c8b87e125f66871b6e57a"

0 commit comments

Comments
 (0)