Skip to content

Commit d4408a1

Browse files
authored
support default value when --conf is omitted (#38)
* update README * support default value when --conf is omitted
1 parent 7aa8772 commit d4408a1

File tree

8 files changed

+116
-19
lines changed

8 files changed

+116
-19
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ i18n locale messages management tool / library for vue-i18n
1010
<img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Become a Patreon">
1111
</a>
1212

13+
## :raising_hand: Motivations
14+
15+
The big motivation is as follows.
16+
17+
- :tired_face: Hard to integrate locale messages for localization services
18+
- :tired_face: Hard to maintain consistency of locale message keys (`eslint-plugin-vue-i18n` need it!)
19+
- :pray: Requested by 3rd vendor tools (`i18n-ally` and etc ...)
20+
1321
## :cd: Installation
1422

1523
### npm
@@ -45,6 +53,7 @@ yarn global vue-i18n-locale-message
4553
- squeeze the locale messages from `i18n` custom block
4654
- infuse the locale messages to `i18n` custom block
4755
- push the locale messages to localization service
56+
- pull the locale mesagees from localization service
4857

4958
## :rocket: Usages
5059

@@ -111,13 +120,13 @@ vue-i18n-locale-message push --provider=l10n-service-provider \
111120
--filename-match=^([\\w]*)\\.json
112121
```
113122

114-
## :raising_hand: Motivations
123+
#### pull
115124

116-
The big motivation is as follows.
117-
118-
- :tired_face: Hard to integrate locale messages for localization services
119-
- :tired_face: Hard to maintain consistency of locale message keys (`eslint-plugin-vue-i18n` need it!)
120-
- :pray: Requested by 3rd vendor tools (`i18n-ally` and etc ...)
125+
```sh
126+
vue-i18n-locale-message pull --provider=l10n-service-provider \
127+
--conf=110n-service-provider-conf.json \
128+
--output=./src/locales
129+
```
121130

122131
## :book: API: Specifications
123132

src/commands/pull.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const debug = Debug('vue-i18n-locale-message:commands:pull')
99
const mkdirPromisify = promisify(fs.mkdir)
1010
const writeFilePromisify = promisify(fs.writeFile)
1111

12-
import { resolve, loadProvider, loadProviderConf, DEFUALT_CONF } from '../utils'
12+
import {
13+
resolveProviderConf,
14+
loadProvider,
15+
loadProviderConf,
16+
DEFUALT_CONF
17+
} from '../utils'
1318
import { ProviderPullResource, Locale, LocaleMessage } from '../../types'
1419

1520
type PullOptions = {
@@ -35,7 +40,7 @@ export const builder = (args: Argv): Argv<PullOptions> => {
3540
.option('conf', {
3641
type: 'string',
3742
alias: 'c',
38-
describe: 'the json file configration of localization service provider'
43+
describe: 'the json file configration of localization service provider. If omitted, use the suffix file name with `-conf` for provider name of --provider (e.g. <provider>-conf.json).'
3944
})
4045
.option('output', {
4146
type: 'string',
@@ -72,10 +77,8 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
7277
return
7378
}
7479

75-
let conf = DEFUALT_CONF
76-
if (args.conf) {
77-
conf = loadProviderConf(resolve(args.conf))
78-
}
80+
const confPath = resolveProviderConf(args.provider, args.conf)
81+
const conf = loadProviderConf(confPath) || DEFUALT_CONF
7982

8083
try {
8184
const locales = args.locales?.split(',').filter(p => p) as Locale[] || []

src/commands/push.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Arguments, Argv } from 'yargs'
22

3-
import { resolve, loadProvider, loadProviderConf, DEFUALT_CONF } from '../utils'
3+
import {
4+
resolve,
5+
resolveProviderConf,
6+
loadProvider,
7+
loadProviderConf,
8+
DEFUALT_CONF
9+
} from '../utils'
410
import path from 'path'
511
import glob from 'glob'
612

@@ -37,7 +43,7 @@ export const builder = (args: Argv): Argv<PushOptions> => {
3743
.option('conf', {
3844
type: 'string',
3945
alias: 'c',
40-
describe: 'the json file configration of localization service provider'
46+
describe: 'the json file configration of localization service provider. If omitted, use the suffix file name with `-conf` for provider name of --provider (e.g. <provider>-conf.json).'
4147
})
4248
.option('target', {
4349
type: 'string',
@@ -76,17 +82,15 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
7682
return
7783
}
7884

79-
let conf = DEFUALT_CONF
80-
if (args.conf) {
81-
conf = loadProviderConf(resolve(args.conf))
82-
}
83-
8485
if (!args.target && !args.targetPaths) {
8586
// TODO: should refactor console message
8687
console.log('You need to specify either --target or --target-paths')
8788
return
8889
}
8990

91+
const confPath = resolveProviderConf(args.provider, args.conf)
92+
const conf = loadProviderConf(confPath) || DEFUALT_CONF
93+
9094
let resource
9195
try {
9296
resource = getProviderPushResource(args, conf.pushMode)

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ function resolveGlob (target: string) {
125125

126126
export const DEFUALT_CONF = { provider: {}, pushMode: 'locale-message' } as ProviderConfiguration
127127

128+
export function resolveProviderConf (provider: string, conf?: string) {
129+
if (conf) {
130+
return resolve(conf)
131+
} else {
132+
const parsed = path.parse(provider)
133+
return resolve(process.cwd(), `${parsed.base}-conf.json`)
134+
}
135+
}
136+
128137
export function loadProvider (provider: string): ProviderFactory | null {
129138
let mod: ProviderFactory | null = null
130139
try {

test/commands/__mocks__/@scope/l10n-omit-service-provider.js

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"provider": {
3+
"token": "yyy"
4+
},
5+
"pushMode": "file-path"
6+
}
7+

test/commands/pull.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ jest.mock('@scope/l10n-service-provider', () => {
1313
})
1414
import L10nServiceProvider from '@scope/l10n-service-provider'
1515

16+
jest.mock('@scope/l10n-omit-service-provider', () => {
17+
return jest.fn().mockImplementation(() => {
18+
return { pull: jest.fn()
19+
.mockImplementation(locales => {
20+
return Promise.resolve({ ja: { hello: 'hello' }, en: { hello: 'こんにちわわわ!' }})
21+
})
22+
}
23+
})
24+
})
25+
import L10nOmitServiceProvider from '@scope/l10n-omit-service-provider'
26+
1627
// fs module
1728
jest.mock('fs', () => ({
1829
...jest.requireActual('fs'),
@@ -24,17 +35,23 @@ import fs from 'fs'
2435
// --------------------
2536
// setup/teadown hooks
2637

38+
const PROCESS_CWD_TARGET_PATH = path.resolve(__dirname)
39+
40+
let orgCwd // for process.cwd mock
2741
let spyLog
2842
let spyError
2943
beforeEach(() => {
3044
spyLog = jest.spyOn(global.console, 'log')
3145
spyError = jest.spyOn(global.console, 'error')
46+
orgCwd = process.cwd
47+
process.cwd = jest.fn(() => PROCESS_CWD_TARGET_PATH) // mock: process.cwd
3248
})
3349

3450
afterEach(() => {
3551
spyError.mockRestore()
3652
spyLog.mockRestore()
3753
jest.clearAllMocks()
54+
process.cwd = orgCwd
3855
})
3956

4057
// -----------
@@ -88,6 +105,24 @@ test('--conf option', async () => {
88105
})
89106
})
90107

108+
test('--conf option omit', async () => {
109+
// run
110+
const pull = await import('../../src/commands/pull')
111+
const cmd = yargs.command(pull)
112+
await new Promise((resolve, reject) => {
113+
cmd.parse(`pull --provider=@scope/l10n-omit-service-provider \
114+
--output=./test/fixtures/locales \
115+
--dry-run`, (err, argv, output) => {
116+
err ? reject(err) : resolve(output)
117+
})
118+
})
119+
120+
expect(L10nOmitServiceProvider).toHaveBeenCalledWith({
121+
provider: { token: 'yyy' },
122+
pushMode: 'file-path'
123+
})
124+
})
125+
91126
test('--locales option', async () => {
92127
// setup mocks
93128
mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}}))

test/commands/push.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,33 @@ jest.mock('@scope/l10n-service-provider', () => {
1212
})
1313
import L10nServiceProvider from '@scope/l10n-service-provider'
1414

15+
jest.mock('@scope/l10n-omit-service-provider', () => {
16+
return jest.fn().mockImplementation(() => {
17+
return { push: jest.fn() }
18+
})
19+
})
20+
import L10nOmitServiceProvider from '@scope/l10n-omit-service-provider'
21+
1522
// -------------------
1623
// setup/teadown hooks
1724

25+
const PROCESS_CWD_TARGET_PATH = path.resolve(__dirname)
26+
27+
let orgCwd // for process.cwd mock
1828
let spyLog
1929
let spyError
2030
beforeEach(() => {
2131
spyLog = jest.spyOn(global.console, 'log')
2232
spyError = jest.spyOn(global.console, 'error')
33+
orgCwd = process.cwd
34+
process.cwd = jest.fn(() => PROCESS_CWD_TARGET_PATH) // mock: process.cwd
2335
})
2436

2537
afterEach(() => {
2638
spyError.mockRestore()
2739
spyLog.mockRestore()
2840
jest.clearAllMocks()
41+
process.cwd = orgCwd
2942
})
3043

3144
// ----------
@@ -136,6 +149,23 @@ test('--conf option', async () => {
136149
}, false)
137150
})
138151

152+
test('--conf option omit', async () => {
153+
// run
154+
const TARGET_LOCALE = './test/fixtures/locales/en.json'
155+
const push = await import('../../src/commands/push')
156+
const cmd = yargs.command(push)
157+
await new Promise((resolve, reject) => {
158+
cmd.parse(`push --provider=@scope/l10n-omit-service-provider --target=${TARGET_LOCALE}`, (err, argv, output) => {
159+
err ? reject(err) : resolve(output)
160+
})
161+
})
162+
163+
expect(L10nOmitServiceProvider).toHaveBeenCalledWith({
164+
provider: { token: 'yyy' },
165+
pushMode: 'file-path'
166+
})
167+
})
168+
139169
test('--target-paths option', async () => {
140170
// setup mocks
141171
mockPush.mockImplementation(resource => true)

0 commit comments

Comments
 (0)