@@ -15,7 +15,9 @@ import {
15
15
TranslationStatusOptions ,
16
16
TranslationStatus ,
17
17
RawLocaleMessage ,
18
- NamespaceDictionary
18
+ NamespaceDictionary ,
19
+ PushableOptions ,
20
+ DiffOptions
19
21
} from '../types'
20
22
21
23
// import modules
@@ -29,21 +31,13 @@ import yaml from 'js-yaml'
29
31
import deepmerge from 'deepmerge'
30
32
import { promisify } from 'util'
31
33
import type { Ignore } from 'ignore'
34
+ const { diffString } = require ( 'json-diff' ) // NOTE: not provided type definition ...
32
35
33
36
import { debug as Debug } from 'debug'
34
37
const debug = Debug ( 'vue-i18n-locale-message:utils' )
35
38
36
39
const readFile = promisify ( fs . readFile )
37
40
38
- // define types
39
- export type PushableOptions = {
40
- target ?: string
41
- locale ?: string
42
- targetPaths ?: string
43
- filenameMatch ?: string
44
- format ?: string
45
- }
46
-
47
41
const ESC : { [ key in string ] : string } = {
48
42
'<' : '<' ,
49
43
'>' : '>' ,
@@ -198,7 +192,7 @@ export function loadProviderConf (confPath: string): ProviderConfiguration {
198
192
return conf
199
193
}
200
194
201
- export function getLocaleMessages ( args : Arguments < PushableOptions > ) : LocaleMessages {
195
+ export function getLocaleMessages ( args : Arguments < PushableOptions > | PushableOptions ) : LocaleMessages {
202
196
let messages = { } as LocaleMessages
203
197
204
198
if ( args . target ) {
@@ -453,3 +447,35 @@ export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
453
447
ig . add ( ignoreRule )
454
448
} )
455
449
}
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
+ }
0 commit comments