Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 7e801c0

Browse files
kovalchukqINTERLOGIC\yeko
and
INTERLOGIC\yeko
authored
Implemented import command (#10)
Co-authored-by: INTERLOGIC\yeko <[email protected]>
1 parent 6f90a8a commit 7e801c0

File tree

7 files changed

+85
-11
lines changed

7 files changed

+85
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
/package-lock.json
77
/tmp
88
node_modules
9+
.idea/.idea.fluent-vue-cli.dir/.idea/indexLayout.xml
10+
.idea/.idea.fluent-vue-cli.dir/.idea/workspace.xml
11+
.idea

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@vue/compiler-dom": "^3.2.6",
1313
"@vue/compiler-sfc": "^3.2.6",
1414
"cac": "^6.7.12",
15-
"fast-glob": "^3.2.11"
15+
"fast-glob": "^3.2.11",
16+
"unixify": "^1.0.0"
1617
},
1718
"files": [
1819
"/bin",
@@ -33,6 +34,7 @@
3334
"devDependencies": {
3435
"@antfu/eslint-config-basic": "0.16.1",
3536
"@types/node": "10.17.60",
37+
"@types/unixify": "^1.0.0",
3638
"eslint": "8.9.0",
3739
"tsup": "5.11.13",
3840
"typescript": "4.5.5",

pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/commands/export.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import { stream } from 'fast-glob'
22
import { promises as fs, existsSync } from 'fs'
3-
import { resolve, dirname } from 'path'
4-
3+
import {resolve, dirname, join, relative} from 'path'
4+
import unixify from 'unixify'
55
import { getVueMessages, mergeFtl } from '../..'
66

77
interface Options {
8+
inDir: string
89
outDir: string
910
clean: boolean
1011
}
1112

1213
const log = console.log.bind(console)
1314

14-
export const run = async (argv: any[], flags: Options) => {
15+
export const run = async (flags: Options) => {
1516
let count = 0
16-
for await (const file of stream(argv)) {
17+
for await (const file of stream(unixify(flags.inDir) + '/**', {ignore: ['**/node_modules']})) {
1718
count ++
1819
const data = await fs.readFile(file)
1920

2021
const vueMessages = getVueMessages(data.toString())
21-
2222
for (const { locale, source, messages } of vueMessages) {
23-
const outputPath = resolve(flags.outDir, locale, `${file}.ftl`)
24-
23+
const outputPath = join(flags.outDir, locale, relative(flags.inDir, `${file}.ftl`))
2524
await fs.mkdir(dirname(outputPath), { recursive: true })
26-
2725
if (flags.clean || !existsSync(outputPath)) {
2826
await fs.writeFile(outputPath, source)
2927
} else {

src/cli/commands/import.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { stream } from 'fast-glob'
2+
import { promises as fs } from 'fs'
3+
import { resolve, dirname, basename, extname, sep, relative, join } from 'path'
4+
import unixify from 'unixify'
5+
import { getFtlMessages, mergeVue } from '../..'
6+
7+
interface Options {
8+
outDir: string,
9+
inDir: string
10+
}
11+
12+
const log = console.log.bind(console)
13+
14+
export const run = async (flags: Options) => {
15+
let count = 0
16+
for await (const file of stream(unixify(flags.inDir) + '/**', {ignore: ['**/node_modules']})) {
17+
count ++
18+
const fileString = file.toString()
19+
const fileDirName= dirname(file.toString())
20+
21+
const vueComponentName = basename(fileString).replace(extname(fileString), '')
22+
const outputPath = join(relative(flags.inDir, fileDirName), vueComponentName)
23+
const [locale, ...rest] = outputPath.split(sep)
24+
const realOutputPath = join(flags.outDir, rest.join(sep))
25+
26+
const vueFile = await fs.readFile(realOutputPath)
27+
const data = await fs.readFile(file)
28+
const ftlMessages = getFtlMessages(data.toString())
29+
const newData = mergeVue(vueFile.toString(), locale, ftlMessages)
30+
31+
await fs.writeFile(realOutputPath, newData)
32+
}
33+
34+
log(`Imported messages to ${count} files`)
35+
}
36+

src/cli/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import cac from 'cac'
22

33
import { run as runExport } from './commands/export'
44

5+
import { run as runImport } from './commands/import'
6+
57
const cli = cac('fluent-vue')
68

79
cli
8-
.command('export <files>', 'Exports translation from Vue.js SFC files into ftl files.')
10+
.command('export', 'Exports translation from Vue.js SFC files into ftl files.')
11+
.option('--in-dir', 'Input directory with vue files', { default: 'example/' })
912
.option('--out-dir', 'Output directory for extracted ftl files', { default: 'translations/' })
1013
.option('--clean', 'Whether to clean output directory', { default: true })
1114
.action(runExport)
1215

16+
cli
17+
.command('import', 'Import translation from ftl files into Vue.js SFC')
18+
.option('--in-dir', 'Input directory with ftl files', { default: 'translations/' })
19+
.option('--out-dir', 'Output directory for extracted vue files', { default: 'example/' })
20+
.action(runImport)
21+
1322
cli.help()
1423

1524
cli.parse()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Comment
22
user-name = World
33
aria-key = Aria value
4-
greeting = Hello, {$name}
4+
greeting = Hello, { $name }
55
.aria-label = Label value

0 commit comments

Comments
 (0)