Skip to content

Commit 1972dca

Browse files
authored
breaking: change squeeze bundle related option name (#80)
* refactoring * breaking: change squeeze command optiona name
1 parent 6de4b93 commit 1972dca

File tree

8 files changed

+51
-47
lines changed

8 files changed

+51
-47
lines changed

src/commands/infuse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
resolve,
55
parsePath,
66
readSFC,
7-
NamespaceDictionary,
87
loadNamespaceDictionary,
98
splitLocaleMessages
109
} from '../utils'
@@ -20,6 +19,7 @@ import {
2019
LocaleMessages,
2120
SFCFileInfo,
2221
MetaLocaleMessage,
22+
NamespaceDictionary,
2323
MetaExternalLocaleMessages
2424
} from '../../types'
2525

src/commands/squeeze.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ import {
44
parsePath,
55
readSFC,
66
loadNamespaceDictionary,
7-
getExternalLocaleMessages,
8-
NamespaceDictionary
7+
getExternalLocaleMessages
98
} from '../utils'
109
import squeeze from '../squeezer'
1110
import fs from 'fs'
1211
import deepmerge from 'deepmerge'
1312

14-
import { LocaleMessages, MetaLocaleMessage, Locale } from '../../types'
13+
import {
14+
Locale,
15+
LocaleMessages,
16+
MetaLocaleMessage,
17+
NamespaceDictionary
18+
} from '../../types'
1519

1620
import { debug as Debug } from 'debug'
1721
const debug = Debug('vue-i18n-locale-message:commands:squeeze')
1822

1923
type SqueezeOptions = {
2024
target: string
2125
split?: boolean
22-
withBundle?: string
23-
withBundleMatch?: string
26+
bundleWith?: string
27+
bundleMatch?: string
2428
namespace?: string
2529
output: string
2630
}
@@ -44,12 +48,12 @@ export const builder = (args: Argv): Argv<SqueezeOptions> => {
4448
default: false,
4549
describe: 'split squeezed locale messages with locale'
4650
})
47-
.option('withBundle', {
51+
.option('bundleWith', {
4852
type: 'string',
4953
alias: 'b',
5054
describe: 'target path of external locale messages that it will bundle together, can also be specified multi paths with comma delimiter'
5155
})
52-
.option('withBundleMatch', {
56+
.option('bundleMatch', {
5357
type: 'string',
5458
alias: 'm',
5559
describe: `option should be accepted regex filename of external locale messages, must be specified if it's directory path of external locale messages with --with-bundle`
@@ -76,7 +80,7 @@ export const handler = async (args: Arguments<SqueezeOptions>) => {
7680
if (args.namespace) {
7781
nsDictionary = await loadNamespaceDictionary(args.namespace)
7882
}
79-
externalMessages = getExternalLocaleMessages(nsDictionary, args.withBundle, args.withBundleMatch)
83+
externalMessages = getExternalLocaleMessages(nsDictionary, args.bundleWith, args.bundleMatch)
8084
} catch (e) {
8185
console.warn('cannot load external locale messages failed')
8286
}

src/utils.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
ProviderConfiguration,
1313
TranslationStatusOptions,
1414
TranslationStatus,
15-
RawLocaleMessage
15+
RawLocaleMessage,
16+
NamespaceDictionary
1617
} from '../types'
1718

1819
// import modules
@@ -39,7 +40,6 @@ export type PushableOptions = {
3940
filenameMatch?: string
4041
format?: string
4142
}
42-
export type NamespaceDictionary = { [path: string]: string }
4343

4444
const ESC: { [key in string]: string } = {
4545
'<': '&lt;',
@@ -330,30 +330,31 @@ function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): Pars
330330
}
331331

332332
export function getExternalLocaleMessages (
333-
dictionary: NamespaceDictionary, withBundle?: string, withBundleMatch?: string
333+
dictionary: NamespaceDictionary, bundleWith?: string, bundleMatch?: string
334334
) {
335-
if (!withBundle) { return {} }
335+
if (!bundleWith) { return {} }
336336

337-
const bundleTargetPaths = withBundle.split(',').filter(p => p)
337+
const bundleTargetPaths = bundleWith.split(',').filter(p => p)
338338
return bundleTargetPaths.reduce((messages, targetPath) => {
339339
const namespace = dictionary[targetPath] || ''
340340
const globedPaths = glob.sync(targetPath).map(p => resolve(p))
341341
return globedPaths.reduce((messages, fullPath) => {
342-
const { locale, filename } = getLocaleMessagePathInfo(fullPath, withBundleMatch)
342+
const { locale, filename } = getLocaleMessagePathInfo(fullPath, bundleMatch)
343343
if (!locale) { return messages }
344344
const externalMessages = JSON.parse(fs.readFileSync(fullPath).toString())
345-
let workMessags = externalMessages
345+
let workMessages = externalMessages
346346
if (filename) {
347-
workMessags = Object.assign({}, { [filename]: workMessags })
347+
workMessages = Object.assign({}, { [filename]: workMessages })
348348
}
349349
if (namespace) {
350-
workMessags = Object.assign({}, { [namespace]: workMessags })
350+
workMessages = Object.assign({}, { [namespace]: workMessages })
351351
}
352-
debug('getExternalLocaleMessages: workMessages', workMessags)
352+
debug('getExternalLocaleMessages: workMessages', workMessages)
353353
if (messages[locale]) {
354-
messages[locale] = deepmerge(messages[locale] as any, workMessags) // eslint-disable-line @typescript-eslint/no-explicit-any
354+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
355+
messages[locale] = deepmerge(messages[locale] as any, workMessages)
355356
} else {
356-
messages = Object.assign(messages, { [locale]: workMessags })
357+
messages = Object.assign(messages, { [locale]: workMessages })
357358
}
358359
debug('getExternalLocaleMessages: messages (processing)', messages)
359360
return messages
@@ -369,7 +370,8 @@ type ExternalLocaleMessagesParseInfo = {
369370
}
370371

371372
// TODO: should be selected more other library ...
372-
function deepCopy (obj: any): any { // eslint-disable-line @typescript-eslint/no-explicit-any
373+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
374+
function deepCopy (obj: any): any {
373375
return JSON.parse(JSON.stringify(obj))
374376
}
375377

@@ -397,8 +399,10 @@ export function splitLocaleMessages (
397399

398400
debug(`splitLocaleMessages: messages (before) = ${JSON.stringify(messages)}`)
399401
const metaExternalLocaleMessages = externalLocaleMessagesParseInfo.reduce((meta, { path, locale, namespace, filename }) => {
400-
const stack = [] as { key: string, ref: any }[] // eslint-disable-line @typescript-eslint/no-explicit-any
401-
let targetLocaleMessage = messages[locale] as any // eslint-disable-line @typescript-eslint/no-explicit-any
402+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
403+
const stack = [] as { key: string, ref: any }[]
404+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
405+
let targetLocaleMessage = messages[locale] as any
402406
if (namespace && targetLocaleMessage[namespace]) {
403407
const ref1 = targetLocaleMessage
404408
targetLocaleMessage = targetLocaleMessage[namespace]

test/__snapshots__/cli.test.ts.snap

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,21 @@ exports[`squeeze command output help 1`] = `
3131
squeeze locale messages from single-file components
3232
3333
Options:
34-
--version Show version number [boolean]
35-
--help Show help [boolean]
36-
--target, -t target path that single-file components is stored
34+
--version Show version number [boolean]
35+
--help Show help [boolean]
36+
--target, -t target path that single-file components is stored
3737
[string] [required]
38-
--split, -s split squeezed locale messages with locale
38+
--split, -s split squeezed locale messages with locale
3939
[boolean] [default: false]
40-
--withBundle, -b target path of external locale messages that it will
41-
bundle together, can also be specified multi paths with
42-
comma delimiter [string]
43-
--withBundleMatch, -m option should be accepted regex filename of external
44-
locale messages, must be specified if it's directory
45-
path of external locale messages with --with-bundle
46-
[string]
47-
--namespace, -n file path that defines the namespace for external
48-
locale messages bundled together [string]
49-
--output, -o path to output squeezed locale messages
40+
--bundleWith, -b target path of external locale messages that it will bundle
41+
together, can also be specified multi paths with comma
42+
delimiter [string]
43+
--bundleMatch, -m option should be accepted regex filename of external locale
44+
messages, must be specified if it's directory path of
45+
external locale messages with --with-bundle [string]
46+
--namespace, -n file path that defines the namespace for external locale
47+
messages bundled together [string]
48+
--output, -o path to output squeezed locale messages
5049
[string] [default: \\"/path/to/project1/messages.json\\"]"
5150
`;
5251

test/commands/infuse.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ const SFC_FILES = [
1818
]
1919
const LOCALE_FILES = [
2020
`${TARGET_PATH}/src/locales/ja.json`,
21-
`${TARGET_PATH}/src/locales/en.json`/*,
22-
path.resolve('test/fixtures/packages/package1/locales/en/common.json'),
23-
path.resolve('test/fixtures/packages/package1/locales/ja/common.json'),
24-
path.resolve('test/fixtures/packages/package2/locales/en/profile.json'),
25-
path.resolve('test/fixtures/packages/package2/locales/ja/profile.json')
26-
*/
21+
`${TARGET_PATH}/src/locales/en.json`
2722
]
2823
const MOCK_FILES = SFC_FILES.reduce((files, file) => {
2924
const meta = jsonMetaInfo.find(meta => meta.contentPath === file)

test/fixtures/external.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export default {
99
},
1010
package2: {
1111
profile: {
12-
display_name: 'Display Name', // eslint-disable-line @typescript-eslint/camelcase
12+
// eslint-disable-next-line @typescript-eslint/camelcase
13+
display_name: 'Display Name',
1314
email: 'E-Mail'
1415
}
1516
}

test/utils.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SFCFileInfo } from '../types'
1+
import { SFCFileInfo, NamespaceDictionary } from '../types'
22
import deepmerge from 'deepmerge'
33

44
import jsonFiles from './fixtures/file/json'
@@ -9,7 +9,6 @@ import squeezeLocaleMessages from './fixtures/squeeze.json'
99
import {
1010
reflectSFCDescriptor,
1111
getTranslationStatus,
12-
NamespaceDictionary,
1312
getExternalLocaleMessages,
1413
splitLocaleMessages
1514
} from '../src/utils'

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ export interface SFCFileInfo {
156156
declare function squeeze (basePath: string, files: SFCFileInfo[]): MetaLocaleMessage
157157
declare function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage, options?: FormatOptions): SFCFileInfo[]
158158

159+
export type NamespaceDictionary = { [path: string]: string }
160+
159161
export type MetaExternalLocaleMessages = {
160162
path: string
161163
messages?: LocaleMessages

0 commit comments

Comments
 (0)