Skip to content

Commit 04c434c

Browse files
authored
feat: infuse formatting with @intlify/cli formatter (#192)
1 parent 70bfe3a commit 04c434c

File tree

5 files changed

+361
-15
lines changed

5 files changed

+361
-15
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
}
2626
},
2727
"dependencies": {
28+
"@intlify/cli": "^0.10.0",
2829
"@vue/component-compiler-utils": "^3.0.0",
30+
"cosmiconfig": "^7.0.1",
2931
"debug": "^4.1.1",
3032
"deep-diff": "^1.0.2",
3133
"deepmerge": "^4.2.2",

src/commands/infuse.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
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'
26

37
import {
48
resolve,
@@ -7,15 +11,12 @@ import {
711
loadNamespaceDictionary,
812
splitLocaleMessages,
913
readIgnoreFile,
10-
returnIgnoreInstance
14+
returnIgnoreInstance,
15+
getPrettierConfig
1116
} from '../utils'
1217

1318
import infuse from '../infuser'
1419
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'
1920
import {
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

94101
export 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+
265292
export default {
266293
command,
267294
aliases,

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { promisify } from 'util'
3535
import type { Ignore } from 'ignore'
3636
import querystring from 'query-string'
3737
import { flatten, unflatten } from 'flat'
38+
import { cosmiconfig } from 'cosmiconfig'
3839
const jsonDiff = require('json-diff') // NOTE: not provided type definition ...
3940

4041
import { debug as Debug } from 'debug'
@@ -532,3 +533,8 @@ export async function pushFunc (options: PushOptions): Promise<unknown> {
532533
}
533534
return Promise.resolve()
534535
}
536+
537+
export async function getPrettierConfig (filepath: string) {
538+
const explorer = cosmiconfig('prettier')
539+
return await explorer.load(filepath)
540+
}

test/commands/infuse.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ jest.mock('path', () => ({
6363
}))
6464
import path from 'path'
6565

66+
// mock: `@intlify/cli
67+
// jest.mock('@intlify/cli', () => ({
68+
// __esModule: true,
69+
// ...jest.requireActual('@intlify/cli')
70+
// }))
71+
// import '@intlify/cli'
72+
6673
// -------------------
6774
// setup/teadown hooks
6875

0 commit comments

Comments
 (0)