Skip to content

Commit

Permalink
⭐ new: format options for infusing (#17) by @antfu
Browse files Browse the repository at this point in the history
* feat: format options for infusing

* fix: correct eol as eof
  • Loading branch information
antfu authored and kazupon committed Nov 18, 2019
1 parent b0e3a80 commit c6d1ef3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/infuser.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
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)

const blocks: SFCBlock[] = getBlocks(descriptor)
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}`)

Expand All @@ -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[] = []
Expand All @@ -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++
Expand All @@ -81,15 +81,15 @@ 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)
}

return contents.join('')
}

function buildI18nTag (i18nBlock: SFCI18nBlock): string {
function buildI18nTag (i18nBlock: SFCI18nBlock, options?: FormatOptions): string {
const { locale, lang, messages } = i18nBlock
let tag = '<i18n'
if (locale) {
Expand All @@ -102,5 +102,5 @@ function buildI18nTag (i18nBlock: SFCI18nBlock): string {

return `\n
${tag}
${stringifyContent(locale ? messages[locale] : messages, lang)}</i18n>`
${stringifyContent(locale ? messages[locale] : messages, lang, options)}</i18n>`
}
23 changes: 18 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 eof = options?.eof || '\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(eof)) {
result += eof
}

return result
}

export function readSFC (target: string): SFCFileInfo[] {
Expand Down
10 changes: 7 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Locale, LocaleMessage>
Expand Down Expand Up @@ -80,6 +80,10 @@ export type MetaLocaleMessage = {
target: string,
components: Record<string, SFCI18nBlock[]>
}
export type FormatOptions = {
intend?: number,
eof?: string
}

/**
* SFC (Single-file component) file info
Expand Down Expand Up @@ -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' {
Expand Down

0 comments on commit c6d1ef3

Please sign in to comment.