11import { Arguments , Argv } from 'yargs'
2+ import fs from 'fs'
3+ import path from 'path'
4+ import { applyDiff } from 'deep-diff'
5+ import glob from 'glob'
26
37import {
48 resolve ,
@@ -7,15 +11,12 @@ import {
711 loadNamespaceDictionary ,
812 splitLocaleMessages ,
913 readIgnoreFile ,
10- returnIgnoreInstance
14+ returnIgnoreInstance ,
15+ getPrettierConfig
1116} from '../utils'
1217
1318import infuse from '../infuser'
1419import squeeze from '../squeezer'
15- import fs from 'fs'
16- import path from 'path'
17- import { applyDiff } from 'deep-diff'
18- import glob from 'glob'
1920import {
2021 Locale ,
2122 LocaleMessages ,
@@ -36,6 +37,7 @@ type InfuseOptions = {
3637 unbundleTo ?: string
3738 unbundleMatch ?: string
3839 namespace ?: string
40+ prettier ?: string
3941 dryRun : boolean
4042 ignoreFileName ?: string
4143}
@@ -89,6 +91,11 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
8991 alias : 'i' ,
9092 describe : 'dot ignore file name, i.e. .ignore-i18n'
9193 } )
94+ . option ( 'prettier' , {
95+ type : 'string' ,
96+ alias : 'p' ,
97+ describe : 'the config file path of prettier'
98+ } )
9299}
93100
94101export const handler = async ( args : Arguments < InfuseOptions > ) => {
@@ -100,6 +107,12 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
100107 returnIgnoreInstance ( ig , ignoreFiles )
101108 }
102109
110+ const prettierConfig = args . prettier
111+ ? await getPrettierConfig ( path . resolve ( process . cwd ( ) , args . prettier ) )
112+ : undefined
113+ debug ( 'prettier config' , prettierConfig )
114+ const format = loadFormat ( )
115+
103116 let nsDictionary = { } as NamespaceDictionary
104117 try {
105118 if ( args . namespace ) {
@@ -122,7 +135,7 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
122135 const newSources = infuse ( targetPath , sources , meta )
123136
124137 if ( ! args . dryRun ) {
125- writeSFC ( newSources )
138+ writeSFC ( newSources , format , prettierConfig )
126139 }
127140
128141 if ( ! args . dryRun && external ) {
@@ -248,9 +261,13 @@ function getTargetLocaleMessages (messages: LocaleMessages, hierarchy: string[])
248261 } , { } as LocaleMessages )
249262}
250263
251- function writeSFC ( sources : SFCFileInfo [ ] ) {
264+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
265+ async function writeSFC ( sources : SFCFileInfo [ ] , format ?: any , prettier ?: any ) {
252266 // TODO: async implementation
253267 sources . forEach ( ( { path, content } ) => {
268+ if ( format && prettier ) {
269+ content = format ( content , path , { prettier } )
270+ }
254271 fs . writeFileSync ( path , content )
255272 } )
256273}
@@ -262,6 +279,16 @@ function writeExternalLocaleMessages (meta: MetaExternalLocaleMessages[]) {
262279 } )
263280}
264281
282+ function loadFormat ( ) {
283+ let format
284+ try {
285+ format = require ( '@intlify/cli' ) . format
286+ } catch ( e ) {
287+ debug ( '@intlify/cli format loading error' , e )
288+ }
289+ return format
290+ }
291+
265292export default {
266293 command,
267294 aliases,
0 commit comments