From c269feb600275f51ff0b426ab2979d577037d439 Mon Sep 17 00:00:00 2001 From: shota_kizawa Date: Tue, 27 Jul 2021 14:12:52 +0900 Subject: [PATCH 1/5] feat: providerArgs option to the push & pull command --- src/commands/pull.ts | 10 ++++++++-- src/commands/push.ts | 10 ++++++++-- types/index.d.ts | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/commands/pull.ts b/src/commands/pull.ts index 794c3aa..e5845cd 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -25,6 +25,7 @@ type PullOptions = { normalize?: string format: string dryRun: boolean + providerArgs?: string } export const command = 'pull' @@ -73,10 +74,15 @@ export const builder = (args: Argv): Argv => { default: false, describe: 'run the pull command, but do not pull to locale messages of localization service' }) + .option('providerArgs', { + type: 'string', + alias: 'pa', + describe: `option to give parameters to the provider by using strings like URL query parameters (e.g. arg1=1&arg2=2).` + }) } export const handler = async (args: Arguments): Promise => { - const { dryRun, normalize, format } = args + const { dryRun, normalize, format, providerArgs } = args const ProviderFactory = loadProvider(args.provider) if (ProviderFactory === null) { @@ -97,7 +103,7 @@ export const handler = async (args: Arguments): Promise => try { const locales = args.locales?.split(',').filter(p => p) as Locale[] || [] const provider = ProviderFactory(conf) - const messages = await provider.pull({ locales, dryRun, normalize, format }) + const messages = await provider.pull({ locales, dryRun, normalize, format, providerArgs }) await applyPullLocaleMessages(args.output, messages, args.dryRun) // TODO: should refactor console message console.log('pull success') diff --git a/src/commands/push.ts b/src/commands/push.ts index 63ebed3..4325f4e 100644 --- a/src/commands/push.ts +++ b/src/commands/push.ts @@ -14,6 +14,7 @@ type PushOptions = { conf?: string normalize?: string dryRun: boolean + providerArgs?: string } & PushableOptions export const command = 'push' @@ -64,10 +65,15 @@ export const builder = (args: Argv): Argv => { default: false, describe: `run the push command, but do not apply to locale messages of localization service` }) + .option('providerArgs', { + type: 'string', + alias: 'pa', + describe: `option to give parameters to the provider by using strings like URL query parameters (e.g. arg1=1&arg2=2).` + }) } export const handler = async (args: Arguments): Promise => { - const { dryRun, normalize } = args + const { dryRun, normalize, providerArgs } = args const ProviderFactory = loadProvider(args.provider) if (ProviderFactory === null) { @@ -88,7 +94,7 @@ export const handler = async (args: Arguments): Promise => try { const messages = getLocaleMessages(args) const provider = ProviderFactory(conf) - await provider.push({ messages, dryRun, normalize }) + await provider.push({ messages, dryRun, normalize, providerArgs }) // TODO: should refactor console message console.log('push success') } catch (e) { diff --git a/types/index.d.ts b/types/index.d.ts index 59d6ef4..f412b2b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -226,6 +226,7 @@ export interface Provider { type CommonArguments = { dryRun: boolean // whether the CLI run as dryRun mode normalize?: string // normalization ways for locale messages or resource + providerArgs?: string // parameters to give to the provider } /** From 5b511343edb010ea13f3824482277b7440c004a1 Mon Sep 17 00:00:00 2001 From: shota_kizawa Date: Tue, 27 Jul 2021 15:13:58 +0900 Subject: [PATCH 2/5] feat: unit test for provideArgs --- test/commands/pull.test.ts | 24 ++++++++++++++++++++++++ test/commands/push.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/test/commands/pull.test.ts b/test/commands/pull.test.ts index 574d1d8..7ed08e0 100644 --- a/test/commands/pull.test.ts +++ b/test/commands/pull.test.ts @@ -213,3 +213,27 @@ test('--format option', async () => { normalize: undefined }) }) + +test('--providerArgs option', async () => { + // setup mocks + mockPull.mockImplementation(({ locales }) => Promise.resolve({ ja: {}, en: {}})) + + // run + const pull = await import('../../src/commands/pull') + const cmd = yargs.command(pull) + await new Promise((resolve, reject) => { + cmd.parse(`pull --provider=@scope/l10n-service-provider \ + --output=./test/fixtures/locales \ + --providerArgs=arg1=1&arg2=2`, (err, argv, output) => { + err ? reject(err) : resolve(output) + }) + }) + + expect(mockPull).toHaveBeenCalledWith({ + locales: [], + format: 'json', + dryRun: false, + normalize: undefined, + providerArgs: 'arg1=1&arg2=2' + }) +}) diff --git a/test/commands/push.test.ts b/test/commands/push.test.ts index 04ad746..ca1ef12 100644 --- a/test/commands/push.test.ts +++ b/test/commands/push.test.ts @@ -266,3 +266,28 @@ test('--normalize option', async () => { normalize: 'flat' }) }) + +test('--providerArgs option', async () => { + // setup mocks + mockPush.mockImplementation(({ resource }) => true) + + // run + const push = await import('../../src/commands/push') + const cmd = yargs.command(push) + await new Promise((resolve, reject) => { + cmd.parse(`push --provider=@scope/l10n-service-provider \ + --target=./test/fixtures/locales/en.json \ + --providerArgs=arg1=1&arg2=2`, (err, argv, output) => { + err ? reject(err) : resolve(output) + }) + }) + + expect(mockPush).toHaveBeenCalledWith({ + messages: { + en: { hello: 'world' } + }, + dryRun: false, + normalize: undefined, + providerArgs: 'arg1=1&arg2=2' + }) +}) \ No newline at end of file From 60ef024d928fee45c3d24212c9948d1d8aa576b1 Mon Sep 17 00:00:00 2001 From: shota_kizawa Date: Tue, 27 Jul 2021 15:15:25 +0900 Subject: [PATCH 3/5] refactor --- test/commands/push.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/commands/push.test.ts b/test/commands/push.test.ts index ca1ef12..8949270 100644 --- a/test/commands/push.test.ts +++ b/test/commands/push.test.ts @@ -290,4 +290,4 @@ test('--providerArgs option', async () => { normalize: undefined, providerArgs: 'arg1=1&arg2=2' }) -}) \ No newline at end of file +}) From 140de87f491aee4d7f5b04d6217a0125ebf6eb42 Mon Sep 17 00:00:00 2001 From: shota_kizawa Date: Tue, 27 Jul 2021 16:35:30 +0900 Subject: [PATCH 4/5] fix: change the type of providerArgs from string to object --- package.json | 1 + src/commands/pull.ts | 6 +++++- src/commands/push.ts | 6 +++++- test/commands/pull.test.ts | 5 ++++- test/commands/push.test.ts | 5 ++++- types/index.d.ts | 2 +- yarn.lock | 25 +++++++++++++++++++++++++ 7 files changed, 45 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f3ecc55..8a83aab 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "js-yaml": "^3.13.1", "json-diff": "^0.5.4", "json5": "^2.1.0", + "query-string": "^7.0.1", "vue-template-compiler": "^2.6.10", "yargs": "^15.1.0" }, diff --git a/src/commands/pull.ts b/src/commands/pull.ts index e5845cd..2703675 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -2,6 +2,7 @@ import * as fs from 'fs' import * as path from 'path' import { promisify } from 'util' import { Arguments, Argv } from 'yargs' +import querystring from 'query-string' import { debug as Debug } from 'debug' const debug = Debug('vue-i18n-locale-message:commands:pull') @@ -103,7 +104,10 @@ export const handler = async (args: Arguments): Promise => try { const locales = args.locales?.split(',').filter(p => p) as Locale[] || [] const provider = ProviderFactory(conf) - const messages = await provider.pull({ locales, dryRun, normalize, format, providerArgs }) + const messages = await provider.pull({ + locales, dryRun, normalize, format, + providerArgs: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined + }) await applyPullLocaleMessages(args.output, messages, args.dryRun) // TODO: should refactor console message console.log('pull success') diff --git a/src/commands/push.ts b/src/commands/push.ts index 4325f4e..a6ccee5 100644 --- a/src/commands/push.ts +++ b/src/commands/push.ts @@ -1,4 +1,5 @@ import { Arguments, Argv } from 'yargs' +import querystring from 'query-string' import { resolveProviderConf, @@ -94,7 +95,10 @@ export const handler = async (args: Arguments): Promise => try { const messages = getLocaleMessages(args) const provider = ProviderFactory(conf) - await provider.push({ messages, dryRun, normalize, providerArgs }) + await provider.push({ + messages, dryRun, normalize, + providerArgs: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined + }) // TODO: should refactor console message console.log('push success') } catch (e) { diff --git a/test/commands/pull.test.ts b/test/commands/pull.test.ts index 7ed08e0..1fc081b 100644 --- a/test/commands/pull.test.ts +++ b/test/commands/pull.test.ts @@ -234,6 +234,9 @@ test('--providerArgs option', async () => { format: 'json', dryRun: false, normalize: undefined, - providerArgs: 'arg1=1&arg2=2' + providerArgs: Object({ + arg1: '1', + arg2: '2' + }) }) }) diff --git a/test/commands/push.test.ts b/test/commands/push.test.ts index 8949270..d0752e3 100644 --- a/test/commands/push.test.ts +++ b/test/commands/push.test.ts @@ -288,6 +288,9 @@ test('--providerArgs option', async () => { }, dryRun: false, normalize: undefined, - providerArgs: 'arg1=1&arg2=2' + providerArgs: { + arg1: '1', + arg2: '2' + } }) }) diff --git a/types/index.d.ts b/types/index.d.ts index f412b2b..2fd0314 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -226,7 +226,7 @@ export interface Provider { type CommonArguments = { dryRun: boolean // whether the CLI run as dryRun mode normalize?: string // normalization ways for locale messages or resource - providerArgs?: string // parameters to give to the provider + providerArgs?: object // parameters to give to the provider } /** diff --git a/yarn.lock b/yarn.lock index 546966b..8e0a044 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2654,6 +2654,11 @@ fill-range@^7.0.1: dependencies: to-regex-range "^5.0.1" +filter-obj@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/filter-obj/-/filter-obj-1.1.0.tgz#9b311112bc6c6127a16e016c6c5d7f19e0805c5b" + integrity sha1-mzERErxsYSehbgFsbF1/GeCAXFs= + find-up@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" @@ -5266,6 +5271,16 @@ q@^1.5.1: resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7" integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc= +query-string@^7.0.1: + version "7.0.1" + resolved "https://registry.yarnpkg.com/query-string/-/query-string-7.0.1.tgz#45bd149cf586aaa582dffc7ec7a8ad97dd02f75d" + integrity sha512-uIw3iRvHnk9to1blJCG3BTc+Ro56CBowJXKmNNAm3RulvPBzWLRqKSiiDk+IplJhsydwtuNMHi8UGQFcCLVfkA== + dependencies: + decode-uri-component "^0.2.0" + filter-obj "^1.1.0" + split-on-first "^1.0.0" + strict-uri-encode "^2.0.0" + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -5902,6 +5917,11 @@ spdx-license-ids@^3.0.0: resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.9.tgz#8a595135def9592bda69709474f1cbeea7c2467f" integrity sha512-Ki212dKK4ogX+xDo4CtOZBVIwhsKBEfsEEcwmJfLQzirgc2jIWdzg40Unxz/HzEUqM1WFzVlQSMF9kZZ2HboLQ== +split-on-first@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/split-on-first/-/split-on-first-1.1.0.tgz#f610afeee3b12bce1d0c30425e76398b78249a5f" + integrity sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw== + split-string@^3.0.1, split-string@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2" @@ -5951,6 +5971,11 @@ static-extend@^0.1.1: define-property "^0.2.5" object-copy "^0.1.0" +strict-uri-encode@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546" + integrity sha1-ucczDHBChi9rFC3CdLvMWGbONUY= + string-length@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/string-length/-/string-length-4.0.2.tgz#a8a8dc7bd5c1a82b9b3c8b87e125f66871b6e57a" From 2468c458502ef26d29751f249ffeab49cd90d947 Mon Sep 17 00:00:00 2001 From: shota_kizawa Date: Wed, 28 Jul 2021 09:57:34 +0900 Subject: [PATCH 5/5] fix: type definition --- types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 2fd0314..3261bfa 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -223,10 +223,10 @@ export interface Provider { export (args: ExportArguments): Promise } -type CommonArguments = { +type CommonArguments = {}> = { dryRun: boolean // whether the CLI run as dryRun mode normalize?: string // normalization ways for locale messages or resource - providerArgs?: object // parameters to give to the provider + providerArgs?: T // parameters to give to the provider } /**