Skip to content

Commit 86964cd

Browse files
authored
feat: add api for diff command (#172)
* feat: add api for diff command * fix: README
1 parent 233c95a commit 86964cd

File tree

7 files changed

+75
-50
lines changed

7 files changed

+75
-50
lines changed

README.md

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

src/commands/diff.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { Arguments, Argv } from 'yargs'
2-
const { diffString } = require('json-diff') // NOTE: not provided type definition ...
32
import { DiffError, fail } from './fails/diff'
43

5-
import {
6-
resolveProviderConf,
7-
loadProvider,
8-
loadProviderConf,
9-
DEFUALT_CONF,
10-
getLocaleMessages,
11-
PushableOptions
12-
} from '../utils'
4+
import { returnDiff } from '../utils'
135

14-
import { Locale } from '../../types'
6+
import { PushableOptions } from '../../types'
157

168
type DiffOptions = {
179
provider: string
@@ -65,31 +57,11 @@ export const builder = (args: Argv): Argv<DiffOptions> => {
6557
}
6658

6759
export const handler = async (args: Arguments<DiffOptions>): Promise<unknown> => {
68-
const { normalize } = args
69-
const format = 'json'
60+
const { provider, conf, normalize, target, locale, targetPaths, filenameMatch } = args
7061

71-
const ProviderFactory = loadProvider(args.provider)
72-
73-
if (ProviderFactory === null) {
74-
return Promise.reject(new Error(`Not found ${args.provider} provider`))
75-
}
76-
77-
if (!args.target && !args.targetPaths) {
78-
// TODO: should refactor console message
79-
return Promise.reject(new Error('You need to specify either --target or --target-paths'))
80-
}
81-
82-
const confPath = resolveProviderConf(args.provider, args.conf)
83-
const conf = loadProviderConf(confPath) || DEFUALT_CONF
84-
85-
const localeMessages = getLocaleMessages(args)
86-
87-
const provider = ProviderFactory(conf)
88-
const locales = Object.keys(localeMessages) as Locale[]
89-
const serviceMessages = await provider.pull({ locales, dryRun: false, normalize, format })
90-
91-
const ret = diffString(serviceMessages, localeMessages)
92-
console.log(ret)
62+
const ret = await returnDiff({
63+
provider, conf, normalize, target, locale, targetPaths, filenameMatch
64+
})
9365

9466
if (ret) {
9567
return Promise.reject(new DiffError('There are differences!'))

src/commands/import.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import {
55
loadProvider,
66
loadProviderConf,
77
DEFUALT_CONF,
8-
getRawLocaleMessages,
9-
PushableOptions
8+
getRawLocaleMessages
109
} from '../utils'
1110

11+
import { PushableOptions } from '../../types'
12+
1213
type ImportOptions = {
1314
provider: string
1415
conf?: string

src/commands/push.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
loadProvider,
77
loadProviderConf,
88
DEFUALT_CONF,
9-
getLocaleMessages,
10-
PushableOptions
9+
getLocaleMessages
1110
} from '../utils'
1211

12+
import { PushableOptions } from '../../types'
13+
1314
type PushOptions = {
1415
provider: string
1516
conf?: string

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import squeeze from './squeezer'
22
import infuse from './infuser'
33
import { getTranslationStatus as status } from './utils'
4+
import { returnDiff as diff } from './utils'
45

56
export {
67
squeeze,
78
infuse,
8-
status
9+
status,
10+
diff
911
}

src/utils.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
TranslationStatusOptions,
1616
TranslationStatus,
1717
RawLocaleMessage,
18-
NamespaceDictionary
18+
NamespaceDictionary,
19+
PushableOptions,
20+
DiffOptions
1921
} from '../types'
2022

2123
// import modules
@@ -29,21 +31,13 @@ import yaml from 'js-yaml'
2931
import deepmerge from 'deepmerge'
3032
import { promisify } from 'util'
3133
import type { Ignore } from 'ignore'
34+
const { diffString } = require('json-diff') // NOTE: not provided type definition ...
3235

3336
import { debug as Debug } from 'debug'
3437
const debug = Debug('vue-i18n-locale-message:utils')
3538

3639
const readFile = promisify(fs.readFile)
3740

38-
// define types
39-
export type PushableOptions = {
40-
target?: string
41-
locale?: string
42-
targetPaths?: string
43-
filenameMatch?: string
44-
format?: string
45-
}
46-
4741
const ESC: { [key in string]: string } = {
4842
'<': '&lt;',
4943
'>': '&gt;',
@@ -198,7 +192,7 @@ export function loadProviderConf (confPath: string): ProviderConfiguration {
198192
return conf
199193
}
200194

201-
export function getLocaleMessages (args: Arguments<PushableOptions>): LocaleMessages {
195+
export function getLocaleMessages (args: Arguments<PushableOptions> | PushableOptions): LocaleMessages {
202196
let messages = {} as LocaleMessages
203197

204198
if (args.target) {
@@ -453,3 +447,35 @@ export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
453447
ig.add(ignoreRule)
454448
})
455449
}
450+
451+
export async function returnDiff (options: DiffOptions): Promise<boolean> {
452+
const format = 'json'
453+
const ProviderFactory = loadProvider(options.provider)
454+
455+
if (ProviderFactory === null) {
456+
return Promise.reject(new Error(`Not found ${options.provider} provider`))
457+
}
458+
459+
if (!options.target && !options.targetPaths) {
460+
// TODO: should refactor console message
461+
return Promise.reject(new Error('You need to specify either --target or --target-paths'))
462+
}
463+
464+
const confPath = resolveProviderConf(options.provider, options.conf)
465+
const conf = loadProviderConf(confPath) || DEFUALT_CONF
466+
467+
const localeMessages = getLocaleMessages(options)
468+
469+
const provider = ProviderFactory(conf)
470+
const locales = Object.keys(localeMessages) as Locale[]
471+
const serviceMessages = await provider.pull({ locales, dryRun: false, normalize: options.normalize, format })
472+
473+
const ret = diffString(serviceMessages, localeMessages)
474+
console.log(ret)
475+
476+
if (ret) {
477+
return Promise.resolve(true)
478+
} else {
479+
return Promise.resolve(false)
480+
}
481+
}

types/index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ export type TranslationStatusOptions = {
192192

193193
declare function status (options: TranslationStatusOptions): Promise<TranslationStatus[]>
194194

195+
/**
196+
* pushable options
197+
*/
198+
export type PushableOptions = {
199+
target?: string
200+
locale?: string
201+
targetPaths?: string
202+
filenameMatch?: string
203+
format?: string
204+
}
205+
206+
/**
207+
* Diff
208+
*/
209+
export type DiffOptions = {
210+
provider: string
211+
conf?: string
212+
normalize?: string
213+
} & PushableOptions
214+
215+
declare function diff (options: DiffOptions): Promise<boolean>
216+
195217
/**
196218
* Provider factory function
197219
*/

0 commit comments

Comments
 (0)