Skip to content

feat: providerArgs option to the push & pull command #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
14 changes: 12 additions & 2 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -25,6 +26,7 @@ type PullOptions = {
normalize?: string
format: string
dryRun: boolean
providerArgs?: string
}

export const command = 'pull'
Expand Down Expand Up @@ -73,10 +75,15 @@ export const builder = (args: Argv): Argv<PullOptions> => {
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<PullOptions>): Promise<unknown> => {
const { dryRun, normalize, format } = args
const { dryRun, normalize, format, providerArgs } = args
const ProviderFactory = loadProvider(args.provider)

if (ProviderFactory === null) {
Expand All @@ -97,7 +104,10 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
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: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined
})
await applyPullLocaleMessages(args.output, messages, args.dryRun)
// TODO: should refactor console message
console.log('pull success')
Expand Down
14 changes: 12 additions & 2 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Arguments, Argv } from 'yargs'
import querystring from 'query-string'

import {
resolveProviderConf,
Expand All @@ -14,6 +15,7 @@ type PushOptions = {
conf?: string
normalize?: string
dryRun: boolean
providerArgs?: string
} & PushableOptions

export const command = 'push'
Expand Down Expand Up @@ -64,10 +66,15 @@ export const builder = (args: Argv): Argv<PushOptions> => {
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<PushOptions>): Promise<unknown> => {
const { dryRun, normalize } = args
const { dryRun, normalize, providerArgs } = args
const ProviderFactory = loadProvider(args.provider)

if (ProviderFactory === null) {
Expand All @@ -88,7 +95,10 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
try {
const messages = getLocaleMessages(args)
const provider = ProviderFactory(conf)
await provider.push({ messages, dryRun, normalize })
await provider.push({
messages, dryRun, normalize,
providerArgs: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined
})
// TODO: should refactor console message
console.log('push success')
} catch (e) {
Expand Down
27 changes: 27 additions & 0 deletions test/commands/pull.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,30 @@ 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: Object({
arg1: '1',
arg2: '2'
})
})
})
28 changes: 28 additions & 0 deletions test/commands/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,31 @@ 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'
}
})
})
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: object // parameters to give to the provider
}

/**
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down