Skip to content

Commit 2cd283e

Browse files
shota-kizawakazupon
authored andcommitted
feat: add the normalize option to the diff API. (#183)
* fix: change the return value of the diff API to an object * feat: add the normalize option to the diff API. * fix: diffInfo type definition * fix: types of return value of diff API * fix: return value of diff * fix: delete warning of eslint
1 parent a0a568a commit 2cd283e

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/commands/diff.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Arguments, Argv } from 'yargs'
22
import { DiffError, fail } from './fails/diff'
33

4-
import { isDifferent } from '../utils'
4+
import { returnDiff } from '../utils'
55

66
import { PushableOptions } from '../../types'
77

@@ -60,7 +60,7 @@ export const handler = async (args: Arguments<DiffOptions>): Promise<unknown> =>
6060
const { provider, conf, normalize, target, locale, targetPaths, filenameMatch } = args
6161

6262
try {
63-
const ret = await isDifferent({
63+
const ret = await returnDiff({
6464
provider, conf, normalize, target, locale, targetPaths, filenameMatch
6565
})
6666

@@ -69,7 +69,6 @@ export const handler = async (args: Arguments<DiffOptions>): Promise<unknown> =>
6969
}
7070
return Promise.resolve()
7171
} catch (e) {
72-
console.error(e.message)
7372
return Promise.reject(e)
7473
}
7574
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import squeeze from './squeezer'
22
import infuse from './infuser'
33
import {
44
getTranslationStatus as status,
5-
isDifferent as diff,
5+
returnDiff as diff,
66
pushFunc as push
77
} from './utils'
88

src/utils.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
NamespaceDictionary,
1818
PushableOptions,
1919
DiffOptions,
20-
PushOptions,
21-
diffInfo
20+
DiffInfo,
21+
PushOptions
2222
} from '../types'
2323

2424
// import modules
@@ -32,6 +32,7 @@ import deepmerge from 'deepmerge'
3232
import { promisify } from 'util'
3333
import type { Ignore } from 'ignore'
3434
import querystring from 'query-string'
35+
import { flatten, unflatten } from 'flat'
3536
const jsonDiff = require('json-diff') // NOTE: not provided type definition ...
3637

3738
import { debug as Debug } from 'debug'
@@ -444,7 +445,7 @@ export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
444445
})
445446
}
446447

447-
export async function isDifferent (options: DiffOptions): Promise<diffInfo> {
448+
export async function returnDiff (options: DiffOptions): Promise<DiffInfo> {
448449
const format = 'json'
449450
const ProviderFactory = loadProvider(options.provider)
450451

@@ -470,6 +471,18 @@ export async function isDifferent (options: DiffOptions): Promise<diffInfo> {
470471
console.log(ret)
471472

472473
if (ret) {
474+
if (options.normalize === 'flat') {
475+
const flattenedServiceMessages = flatten(serviceMessages)
476+
const flattenedlocaleMessages = flatten(localeMessages)
477+
const flattenedDiffObj = jsonDiff.diff(flattenedServiceMessages, flattenedlocaleMessages)
478+
return Promise.resolve(flattenedDiffObj)
479+
}
480+
if (options.normalize === 'hierarchy') {
481+
const unflattenedServiceMessages = unflatten(serviceMessages, { object: true })
482+
const unflattenedlocaleMessages = unflatten(localeMessages, { object: true })
483+
const unflattenedDiffObj = jsonDiff.diff(unflattenedServiceMessages, unflattenedlocaleMessages)
484+
return Promise.resolve(unflattenedDiffObj)
485+
}
473486
const diffObj = jsonDiff.diff(serviceMessages, localeMessages)
474487
return Promise.resolve(diffObj)
475488
} else {

types/index.d.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,9 @@ export type DiffOptions = {
224224
normalize?: string
225225
} & PushableOptions
226226

227-
export type diffInfo = {
228-
__old?: any
229-
__new?: any
230-
}
227+
export type DiffInfo = Record<string, any>
231228

232-
declare function diff (options: DiffOptions): Promise<diffInfo>
229+
declare function diff (options: DiffOptions): Promise<DiffInfo>
233230

234231
/**
235232
* Provider factory function

0 commit comments

Comments
 (0)