From 7897f430575570c611cead7e493435dcb875957d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 18 Nov 2019 13:36:10 +0800 Subject: [PATCH 1/2] feat: format options for infusing --- src/infuser.ts | 20 ++++++++++---------- src/utils.ts | 23 ++++++++++++++++++----- types/index.d.ts | 10 +++++++--- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/infuser.ts b/src/infuser.ts index f70dd67..17fe67b 100644 --- a/src/infuser.ts +++ b/src/infuser.ts @@ -1,23 +1,23 @@ import { SFCDescriptor, SFCBlock } from 'vue-template-compiler' -import { Locale, MetaLocaleMessage, SFCI18nBlock, SFCFileInfo } from '../types' +import { Locale, MetaLocaleMessage, SFCI18nBlock, SFCFileInfo, FormatOptions } from '../types' import { escape, reflectSFCDescriptor, parseContent, stringifyContent } from './utils' import { debug as Debug } from 'debug' const debug = Debug('vue-i18n-locale-message:infuser') -export default function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage): SFCFileInfo[] { +export default function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage, options?: FormatOptions): SFCFileInfo[] { const descriptors = reflectSFCDescriptor(basePath, sources) return descriptors.map(descriptor => { return { - content: generate(meta, descriptor), + content: generate(meta, descriptor, options), path: descriptor.contentPath } as SFCFileInfo }) } -function generate (meta: MetaLocaleMessage, descriptor: SFCDescriptor): string { +function generate (meta: MetaLocaleMessage, descriptor: SFCDescriptor, options?: FormatOptions): string { const i18nBlocks = meta.components[descriptor.contentPath] debug('target i18n blocks\n', i18nBlocks) @@ -25,7 +25,7 @@ function generate (meta: MetaLocaleMessage, descriptor: SFCDescriptor): string { blocks.forEach(b => debug(`block: type=${b.type}, start=${b.start}, end=${b.end}`)) const { raw } = descriptor - const content = buildContent(i18nBlocks, raw, blocks) + const content = buildContent(i18nBlocks, raw, blocks, options) debug(`build content:\n${content}`) debug(`content size: raw=${raw.length}, content=${content.length}`) @@ -41,7 +41,7 @@ function getBlocks (descriptor: SFCDescriptor): SFCBlock[] { return blocks } -function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock[]): string { +function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock[], options?: FormatOptions): string { let offset = 0 let i18nBlockCounter = 0 let contents: string[] = [] @@ -67,7 +67,7 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock } contents = contents.concat(raw.slice(offset, block.start)) - const serialized = `\n${stringifyContent(messages, lang)}` + const serialized = `\n${stringifyContent(messages, lang, options)}` contents = contents.concat(serialized) offset = block.end as number i18nBlockCounter++ @@ -81,7 +81,7 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock if (i18nBlocks.length > i18nBlockCounter) { i18nBlocks.slice(i18nBlockCounter).reduce((contents, i18nBlock) => { - contents.push(buildI18nTag(i18nBlock)) + contents.push(buildI18nTag(i18nBlock, options)) return contents }, contents) } @@ -89,7 +89,7 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock return contents.join('') } -function buildI18nTag (i18nBlock: SFCI18nBlock): string { +function buildI18nTag (i18nBlock: SFCI18nBlock, options?: FormatOptions): string { const { locale, lang, messages } = i18nBlock let tag = '` +${stringifyContent(locale ? messages[locale] : messages, lang, options)}` } diff --git a/src/utils.ts b/src/utils.ts index a015c3d..4ee1292 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import { SFCDescriptor } from 'vue-template-compiler' -import { SFCFileInfo } from '../types' +import { SFCFileInfo, FormatOptions } from '../types' import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types' import { parse } from '@vue/component-compiler-utils' @@ -77,17 +77,30 @@ export function parseContent (content: string, lang: string): any { } } -export function stringifyContent (content: any, lang: string): string { +export function stringifyContent (content: any, lang: string, options?: FormatOptions): string { + const indent = options?.intend || 2 + const eol = options?.eol || '\n' + + let result = '' switch (lang) { case 'yaml': case 'yml': - return yaml.safeDump(content, { indent: 2 }) + result = yaml.safeDump(content, { indent }) + break case 'json5': - return JSON5.stringify(content, null, 2) + result = JSON5.stringify(content, null, indent) + break case 'json': default: - return JSON.stringify(content, null, 2) + result = JSON.stringify(content, null, indent) + break + } + + if (!result.endsWith(eol)) { + result += eol } + + return result } export function readSFC (target: string): SFCFileInfo[] { diff --git a/types/index.d.ts b/types/index.d.ts index fe23b3b..6cf464e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -26,8 +26,8 @@ import { SFCDescriptor } from 'vue-template-compiler' */ export type Locale = string -export type LocaleMessage = - | string +export type LocaleMessage = + | string | { [property: string]: LocaleMessage } | LocaleMessage[] export type LocaleMessages = Record @@ -80,6 +80,10 @@ export type MetaLocaleMessage = { target: string, components: Record } +export type FormatOptions = { + intend?: number, + eol?: string +} /** * SFC (Single-file component) file info @@ -153,7 +157,7 @@ export interface SFCFileInfo { */ declare function squeeze (basePath: string, files: SFCFileInfo[]): MetaLocaleMessage -declare function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage): SFCFileInfo[] +declare function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage, options?: FormatOptions): SFCFileInfo[] // extend for vue-i18n-locale-message declare module 'vue-template-compiler' { From c1c4724c6d92a0e49ed34dff12b3cc3f8c862a83 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 18 Nov 2019 13:44:00 +0800 Subject: [PATCH 2/2] fix: correct eol as eof --- src/utils.ts | 6 +++--- types/index.d.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 4ee1292..f33692c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -79,7 +79,7 @@ export function parseContent (content: string, lang: string): any { export function stringifyContent (content: any, lang: string, options?: FormatOptions): string { const indent = options?.intend || 2 - const eol = options?.eol || '\n' + const eof = options?.eof || '\n' let result = '' switch (lang) { @@ -96,8 +96,8 @@ export function stringifyContent (content: any, lang: string, options?: FormatOp break } - if (!result.endsWith(eol)) { - result += eol + if (!result.endsWith(eof)) { + result += eof } return result diff --git a/types/index.d.ts b/types/index.d.ts index 6cf464e..9e57e4d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -82,7 +82,7 @@ export type MetaLocaleMessage = { } export type FormatOptions = { intend?: number, - eol?: string + eof?: string } /**