Skip to content

Commit c9b8c5a

Browse files
authored
feat: json formatting (#196)
* feat: support json formatting * fix: typo
1 parent b5615c0 commit c9b8c5a

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"js-yaml": "^3.13.1",
3838
"json-diff": "^0.5.4",
3939
"json5": "^2.1.0",
40+
"prettier": "^2.4.1",
41+
"prettier-plugin-sort-json": "^0.0.2",
4042
"query-string": "^7.0.1",
4143
"vue-template-compiler": "^2.6.10",
4244
"yargs": "^15.1.0"

src/commands/infuse.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs'
33
import path from 'path'
44
import { applyDiff } from 'deep-diff'
55
import glob from 'glob'
6+
import { format as prettierFormat } from 'prettier'
67

78
import {
89
resolve,
@@ -146,7 +147,7 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
146147
}
147148

148149
if (!args.dryRun && external) {
149-
writeExternalLocaleMessages(external)
150+
writeExternalLocaleMessages(external, prettierConfig)
150151
}
151152
}
152153

@@ -280,11 +281,27 @@ async function writeSFC (sources: SFCFileInfo[], format: any, prettier: any, vue
280281
}
281282
}
282283

283-
function writeExternalLocaleMessages (meta: MetaExternalLocaleMessages[]) {
284-
// TODO: async implementation
285-
meta.forEach(({ path, messages }) => {
286-
fs.writeFileSync(path, JSON.stringify(messages, null, 2))
287-
})
284+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
285+
function writeExternalLocaleMessages (meta: MetaExternalLocaleMessages[], prettierConfig: any) {
286+
const config = prettierConfig
287+
? Object.assign(
288+
{},
289+
prettierConfig,
290+
{
291+
parser: 'json',
292+
plugins: ['./node_modules/prettier-plugin-sort-json'],
293+
pluginSearchDirs: ['./node_modules'],
294+
jsonRecursiveSort: true
295+
})
296+
: null
297+
for (const { path, messages } of meta) {
298+
let _messages = JSON.stringify(messages, null, 2)
299+
if (config) {
300+
_messages = prettierFormat(_messages, config)
301+
}
302+
// TODO: async implementation
303+
fs.writeFileSync(path, _messages)
304+
}
288305
}
289306

290307
function loadFormat () {

0 commit comments

Comments
 (0)