Skip to content

Commit 57c0107

Browse files
authored
improvement: infuse formatting with vue template compiler v2 (#194)
* feat: infuse formatting with @intlify/cli formatter * improvement: infuse formatting with vue template compiler
1 parent bd98d75 commit 57c0107

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"dependencies": {
28-
"@intlify/cli": "^0.10.0",
28+
"@intlify/cli": "^0.11.0",
2929
"@vue/component-compiler-utils": "^3.0.0",
3030
"cosmiconfig": "^7.0.1",
3131
"debug": "^4.1.1",

src/commands/infuse.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type InfuseOptions = {
3838
unbundleMatch?: string
3939
namespace?: string
4040
prettier?: string
41+
vue?: number
4142
dryRun: boolean
4243
ignoreFileName?: string
4344
}
@@ -96,9 +97,15 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
9697
alias: 'p',
9798
describe: 'the config file path of prettier'
9899
})
100+
.option('vue', {
101+
type: 'number',
102+
alias: 'v',
103+
describe: 'the vue template compiler version'
104+
})
99105
}
100106

101107
export const handler = async (args: Arguments<InfuseOptions>) => {
108+
const vue = args.vue || 2
102109
const targetPath = resolve(args.target)
103110
const messagesPath = resolve(args.locales)
104111
const ig = ignore()
@@ -135,7 +142,7 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
135142
const newSources = infuse(targetPath, sources, meta)
136143

137144
if (!args.dryRun) {
138-
writeSFC(newSources, format, prettierConfig)
145+
writeSFC(newSources, format, prettierConfig, vue)
139146
}
140147

141148
if (!args.dryRun && external) {
@@ -262,14 +269,15 @@ function getTargetLocaleMessages (messages: LocaleMessages, hierarchy: string[])
262269
}
263270

264271
// eslint-disable-next-line @typescript-eslint/no-explicit-any
265-
async function writeSFC (sources: SFCFileInfo[], format?: any, prettier?: any) {
266-
// TODO: async implementation
267-
sources.forEach(({ path, content }) => {
272+
async function writeSFC (sources: SFCFileInfo[], format: any, prettier: any, vue: number) {
273+
for (const { path, content } of sources) {
274+
let _content = content
268275
if (format && prettier) {
269-
content = format(content, path, { prettier })
276+
_content = await format(content, path, { prettier, vue })
270277
}
271-
fs.writeFileSync(path, content)
272-
})
278+
// TODO: async implementation
279+
fs.writeFileSync(path, _content)
280+
}
273281
}
274282

275283
function writeExternalLocaleMessages (meta: MetaExternalLocaleMessages[]) {

test/commands/infuse.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ 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-
7366
// -------------------
7467
// setup/teadown hooks
7568

@@ -305,7 +298,7 @@ test('ignore option', async () => {
305298
mockFS.writeFileSync.mockImplementation((path, data) => {
306299
writeFiles[path as string] = data.toString()
307300
})
308-
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES);
301+
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES)
309302
const mockGlob = glob as jest.Mocked<typeof glob>
310303
mockGlob.sync.mockImplementationOnce(p => [`${TARGET_PATH}/src/App.vue`])
311304
const mockPath = path as jest.Mocked<typeof path>

yarn.lock

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,16 @@
340340
source-map "^0.6.1"
341341
yaml-eslint-parser "^0.3.2"
342342

343-
"@intlify/cli@^0.10.0":
344-
version "0.10.0"
345-
resolved "https://registry.yarnpkg.com/@intlify/cli/-/cli-0.10.0.tgz#586ae6505b27a536096e08658482a6fbeb87a826"
346-
integrity sha512-mQ9y4+09y/JZPgGNkHvnbxIfWuiVn0YS3VHEOiS9b1QHSYSyDiD9eExMLaUkalmFbcM3ZMQIHhapIed14edY1A==
343+
"@intlify/cli@^0.11.0":
344+
version "0.11.0"
345+
resolved "https://registry.yarnpkg.com/@intlify/cli/-/cli-0.11.0.tgz#c346b36d3ce9eec7a5507ec3030de66e0d5f6054"
346+
integrity sha512-Z/F2Ml5khyTpU4QzvNqixPftQQvDKb21N9lYULBLzXerceDGenDZWIkX6jfjhKtYGQ7yQl8cN4mR4hLzYQ0FyA==
347347
dependencies:
348348
"@intlify/bundle-utils" next
349349
"@intlify/core" beta
350350
"@intlify/shared" beta
351-
"@vue/compiler-sfc" "^3.2.20"
352-
chalk "^4.1.0"
351+
"@vue/compiler-sfc" latest
352+
colorette "^2.0.16"
353353
cosmiconfig "^7.0.1"
354354
debug "^4.3.2"
355355
diff-match-patch "^1.0.5"
@@ -359,6 +359,7 @@
359359
pathe "^0.2.0"
360360
prettier "^2.4.1"
361361
prettier-plugin-sort-json "^0.0.2"
362+
vue-template-compiler latest
362363
yaml-eslint-parser "^0.4.1"
363364
yargs "^17.2.0"
364365

@@ -1086,7 +1087,7 @@
10861087
"@vue/compiler-core" "3.2.21"
10871088
"@vue/shared" "3.2.21"
10881089

1089-
"@vue/compiler-sfc@^3.2.20":
1090+
"@vue/compiler-sfc@latest":
10901091
version "3.2.21"
10911092
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.2.21.tgz#42639ee49e725afb7d8f1d1940e75dc17a56002c"
10921093
integrity sha512-+yDlUSebKpz/ovxM2vLRRx7w/gVfY767pOfYTgbIhAs+ogvIV2BsIt4fpxlThnlCNChJ+yE0ERUNoROv2kEGEQ==
@@ -1930,6 +1931,11 @@ colorette@^1.2.2:
19301931
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.2.tgz#cbcc79d5e99caea2dbf10eb3a26fd8b3e6acfa94"
19311932
integrity sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==
19321933

1934+
colorette@^2.0.16:
1935+
version "2.0.16"
1936+
resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.16.tgz#713b9af84fdb000139f04546bd4a93f62a5085da"
1937+
integrity sha512-hUewv7oMjCp+wkBv5Rm0v87eJhq4woh5rSR+42YSQJKecCqgIqNkZ6lAlQms/BwHPJA5NKMRlpxPRv0n8HQW6g==
1938+
19331939
combined-stream@^1.0.8:
19341940
version "1.0.8"
19351941
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
@@ -6889,7 +6895,7 @@ vue-eslint-parser@^5.0.0:
68896895
esquery "^1.0.1"
68906896
lodash "^4.17.11"
68916897

6892-
vue-template-compiler@^2.6.10:
6898+
vue-template-compiler@^2.6.10, vue-template-compiler@latest:
68936899
version "2.6.14"
68946900
resolved "https://registry.yarnpkg.com/vue-template-compiler/-/vue-template-compiler-2.6.14.tgz#a2f0e7d985670d42c9c9ee0d044fed7690f4f763"
68956901
integrity sha512-ODQS1SyMbjKoO1JBJZojSw6FE4qnh9rIpUZn2EUT86FKizx9uH5z6uXiIrm4/Nb/gwxTi/o17ZDEGWAXHvtC7g==

0 commit comments

Comments
 (0)