Skip to content

Commit bed25c6

Browse files
authored
feat: API for push (#175)
* feat: api for push * fix: README * refactor
1 parent 14e2ac6 commit bed25c6

File tree

7 files changed

+81
-62
lines changed

7 files changed

+81
-62
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ yarn global add vue-i18n-locale-message
5050
- infuse the meta of locale messages to `i18n` custom block
5151
- get translation status from localization service
5252
- diff locale messages between local and localization service
53+
- push the locale messages to localization service
5354
- CLI
5455
- squeeze: squeeze the locale messages from `i18n` custom block
5556
- infuse: infuse the locale messages to `i18n` custom block

src/commands/diff.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ export const builder = (args: Argv): Argv<DiffOptions> => {
5959
export const handler = async (args: Arguments<DiffOptions>): Promise<unknown> => {
6060
const { provider, conf, normalize, target, locale, targetPaths, filenameMatch } = args
6161

62-
const ret = await returnDiff({
63-
provider, conf, normalize, target, locale, targetPaths, filenameMatch
64-
})
62+
try {
63+
const ret = await returnDiff({
64+
provider, conf, normalize, target, locale, targetPaths, filenameMatch
65+
})
6566

66-
if (ret) {
67-
return Promise.reject(new DiffError('There are differences!'))
67+
if (ret) {
68+
return Promise.reject(new DiffError('There are differences!'))
69+
}
70+
return Promise.resolve()
71+
} catch (e) {
72+
console.error(e.message)
73+
return Promise.reject(e)
6874
}
69-
70-
return Promise.resolve()
7175
}
7276

7377
export default {

src/commands/push.ts

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

4-
import {
5-
resolveProviderConf,
6-
loadProvider,
7-
loadProviderConf,
8-
DEFUALT_CONF,
9-
getLocaleMessages
10-
} from '../utils'
11-
12-
import { PushableOptions } from '../../types'
13-
14-
type PushOptions = {
15-
provider: string
16-
conf?: string
17-
normalize?: string
18-
dryRun: boolean
19-
providerArgs?: string
20-
} & PushableOptions
3+
import { pushFunc } from '../utils'
4+
import { PushOptions } from '../../types'
215

226
export const command = 'push'
237
export const aliases = 'ph'
@@ -75,36 +59,14 @@ export const builder = (args: Argv): Argv<PushOptions> => {
7559
}
7660

7761
export const handler = async (args: Arguments<PushOptions>): Promise<unknown> => {
78-
const { dryRun, normalize, providerArgs } = args
79-
const ProviderFactory = loadProvider(args.provider)
80-
81-
if (ProviderFactory === null) {
82-
// TODO: should refactor console message
83-
console.log(`Not found ${args.provider} provider`)
84-
return
85-
}
86-
87-
if (!args.target && !args.targetPaths) {
88-
// TODO: should refactor console message
89-
console.log('You need to specify either --target or --target-paths')
90-
return
91-
}
92-
93-
const confPath = resolveProviderConf(args.provider, args.conf)
94-
const conf = loadProviderConf(confPath) || DEFUALT_CONF
62+
const { provider, conf, normalize, dryRun, providerArgs, target, locale, targetPaths, filenameMatch } = args
9563

9664
try {
97-
const messages = getLocaleMessages(args)
98-
const provider = ProviderFactory(conf)
99-
await provider.push({
100-
messages, dryRun, normalize,
101-
providerArgs: providerArgs !== undefined ? querystring.parse(providerArgs) : undefined
102-
})
65+
await pushFunc({ provider, conf, normalize, dryRun, providerArgs, target, locale, targetPaths, filenameMatch })
10366
// TODO: should refactor console message
10467
console.log('push success')
105-
} catch (e) {
106-
// TODO: should refactor console message
107-
console.error('push fail:', e.message)
68+
} catch {
69+
return
10870
}
10971
}
11072

src/commands/status.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@ export const builder = (args: Argv): Argv<StatusOptions> => {
3939
export const handler = async (args: Arguments<StatusOptions>): Promise<unknown> => {
4040
const { provider, conf, locales } = args
4141
debug(`status args: provider=${provider}, conf=${conf}, locales=${locales}`)
42-
const status = await getTranslationStatus({ provider, conf, locales })
43-
debug('raw status', status)
44-
console.table(status)
42+
try {
43+
const status = await getTranslationStatus({ provider, conf, locales })
44+
debug('raw status', status)
45+
console.table(status)
4546

46-
const completes = status.filter(st => st.percentage < 100)
47+
const completes = status.filter(st => st.percentage < 100)
4748

48-
return completes.length > 0
49-
? Promise.reject(new TranslationStatusError('Translation work in progress'))
50-
: Promise.resolve('Translation done')
49+
return completes.length > 0
50+
? Promise.reject(new TranslationStatusError('Translation work in progress'))
51+
: Promise.resolve('Translation done')
52+
} catch (e) {
53+
console.error(e.message)
54+
return Promise.reject(e)
55+
}
5156
}
5257

5358
export default {

src/utils.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
RawLocaleMessage,
1818
NamespaceDictionary,
1919
PushableOptions,
20-
DiffOptions
20+
DiffOptions,
21+
PushOptions
2122
} from '../types'
2223

2324
// import modules
@@ -31,6 +32,7 @@ import yaml from 'js-yaml'
3132
import deepmerge from 'deepmerge'
3233
import { promisify } from 'util'
3334
import type { Ignore } from 'ignore'
35+
import querystring from 'query-string'
3436
const { diffString } = require('json-diff') // NOTE: not provided type definition ...
3537

3638
import { debug as Debug } from 'debug'
@@ -479,3 +481,36 @@ export async function returnDiff (options: DiffOptions): Promise<boolean> {
479481
return Promise.resolve(false)
480482
}
481483
}
484+
485+
export async function pushFunc (options: PushOptions): Promise<unknown> {
486+
const ProviderFactory = loadProvider(options.provider)
487+
488+
if (ProviderFactory === null) {
489+
// TODO: should refactor message
490+
console.error(`Not found ${options.provider} provider`)
491+
return Promise.reject()
492+
}
493+
494+
if (!options.target && !options.targetPaths) {
495+
// TODO: should refactor message
496+
console.error(`You need to specify either --target or --target-paths`)
497+
return Promise.reject()
498+
}
499+
500+
const confPath = resolveProviderConf(options.provider, options.conf)
501+
const conf = loadProviderConf(confPath) || DEFUALT_CONF
502+
503+
try {
504+
const messages = getLocaleMessages(options)
505+
const provider = ProviderFactory(conf)
506+
await provider.push({
507+
messages, dryRun: options.dryRun, normalize: options.normalize,
508+
providerArgs: options.providerArgs !== undefined ? querystring.parse(options.providerArgs) : undefined
509+
})
510+
} catch (e) {
511+
// TODO: should refactor message
512+
console.error('push fail:', e.message)
513+
return Promise.reject()
514+
}
515+
return Promise.resolve()
516+
}

test/commands/push.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ test('--provider: not found', async () => {
6666
err ? reject(err) : resolve(output)
6767
})
6868
})
69-
expect(spyLog).toHaveBeenCalledWith('Not found ./404-provider.js provider')
69+
expect(spyError).toHaveBeenCalledWith('Not found ./404-provider.js provider')
7070
})
7171

7272
test('not specified --target and --targetPaths', async () => {
@@ -77,7 +77,7 @@ test('not specified --target and --targetPaths', async () => {
7777
err ? reject(err) : resolve(output)
7878
})
7979
})
80-
expect(spyLog).toHaveBeenCalledWith('You need to specify either --target or --target-paths')
80+
expect(spyError).toHaveBeenCalledWith('You need to specify either --target or --target-paths')
8181
})
8282

8383
test('--target option', async () => {

types/index.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,26 @@ declare function status (options: TranslationStatusOptions): Promise<Translation
195195
/**
196196
* pushable options
197197
*/
198-
export type PushableOptions = {
198+
export type PushableOptions = {
199199
target?: string
200200
locale?: string
201201
targetPaths?: string
202202
filenameMatch?: string
203203
format?: string
204204
}
205205

206+
/**
207+
* push locale Message to l10n service
208+
*/
209+
export type PushOptions = {
210+
provider: string
211+
conf?: string
212+
normalize?: string
213+
dryRun: boolean
214+
providerArgs?: string
215+
} & PushableOptions
216+
declare function push (options: PushOptions): Promise<unknown>
217+
206218
/**
207219
* Diff
208220
*/

0 commit comments

Comments
 (0)