1
1
import { 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'
2
6
3
7
import {
4
8
resolve ,
@@ -7,15 +11,12 @@ import {
7
11
loadNamespaceDictionary ,
8
12
splitLocaleMessages ,
9
13
readIgnoreFile ,
10
- returnIgnoreInstance
14
+ returnIgnoreInstance ,
15
+ getPrettierConfig
11
16
} from '../utils'
12
17
13
18
import infuse from '../infuser'
14
19
import squeeze from '../squeezer'
15
- import fs from 'fs'
16
- import path from 'path'
17
- import { applyDiff } from 'deep-diff'
18
- import glob from 'glob'
19
20
import {
20
21
Locale ,
21
22
LocaleMessages ,
@@ -36,6 +37,7 @@ type InfuseOptions = {
36
37
unbundleTo ?: string
37
38
unbundleMatch ?: string
38
39
namespace ?: string
40
+ prettier ?: string
39
41
dryRun : boolean
40
42
ignoreFileName ?: string
41
43
}
@@ -89,6 +91,11 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
89
91
alias : 'i' ,
90
92
describe : 'dot ignore file name, i.e. .ignore-i18n'
91
93
} )
94
+ . option ( 'prettier' , {
95
+ type : 'string' ,
96
+ alias : 'p' ,
97
+ describe : 'the config file path of prettier'
98
+ } )
92
99
}
93
100
94
101
export const handler = async ( args : Arguments < InfuseOptions > ) => {
@@ -100,6 +107,12 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
100
107
returnIgnoreInstance ( ig , ignoreFiles )
101
108
}
102
109
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
+
103
116
let nsDictionary = { } as NamespaceDictionary
104
117
try {
105
118
if ( args . namespace ) {
@@ -122,7 +135,7 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
122
135
const newSources = infuse ( targetPath , sources , meta )
123
136
124
137
if ( ! args . dryRun ) {
125
- writeSFC ( newSources )
138
+ writeSFC ( newSources , format , prettierConfig )
126
139
}
127
140
128
141
if ( ! args . dryRun && external ) {
@@ -248,9 +261,13 @@ function getTargetLocaleMessages (messages: LocaleMessages, hierarchy: string[])
248
261
} , { } as LocaleMessages )
249
262
}
250
263
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 ) {
252
266
// TODO: async implementation
253
267
sources . forEach ( ( { path, content } ) => {
268
+ if ( format && prettier ) {
269
+ content = format ( content , path , { prettier } )
270
+ }
254
271
fs . writeFileSync ( path , content )
255
272
} )
256
273
}
@@ -262,6 +279,16 @@ function writeExternalLocaleMessages (meta: MetaExternalLocaleMessages[]) {
262
279
} )
263
280
}
264
281
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
+
265
292
export default {
266
293
command,
267
294
aliases,
0 commit comments