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

Implemented import command #10

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
/package-lock.json
/tmp
node_modules
.idea/.idea.fluent-vue-cli.dir/.idea/indexLayout.xml
.idea/.idea.fluent-vue-cli.dir/.idea/workspace.xml
.idea
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"@vue/compiler-dom": "^3.2.6",
"@vue/compiler-sfc": "^3.2.6",
"cac": "^6.7.12",
"fast-glob": "^3.2.11"
"fast-glob": "^3.2.11",
"unixify": "^1.0.0"
},
"files": [
"/bin",
Expand All @@ -33,6 +34,7 @@
"devDependencies": {
"@antfu/eslint-config-basic": "0.16.1",
"@types/node": "10.17.60",
"@types/unixify": "^1.0.0",
"eslint": "8.9.0",
"tsup": "5.11.13",
"typescript": "4.5.5",
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions src/cli/commands/export.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { stream } from 'fast-glob'
import { promises as fs, existsSync } from 'fs'
import { resolve, dirname } from 'path'

import {resolve, dirname, join, relative} from 'path'
import unixify from 'unixify'
import { getVueMessages, mergeFtl } from '../..'

interface Options {
inDir: string
outDir: string
clean: boolean
}

const log = console.log.bind(console)

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

const vueMessages = getVueMessages(data.toString())

for (const { locale, source, messages } of vueMessages) {
const outputPath = resolve(flags.outDir, locale, `${file}.ftl`)

const outputPath = join(flags.outDir, locale, relative(flags.inDir, `${file}.ftl`))
await fs.mkdir(dirname(outputPath), { recursive: true })

if (flags.clean || !existsSync(outputPath)) {
await fs.writeFile(outputPath, source)
} else {
Expand Down
36 changes: 36 additions & 0 deletions src/cli/commands/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { stream } from 'fast-glob'
import { promises as fs } from 'fs'
import { resolve, dirname, basename, extname, sep, relative, join } from 'path'
import unixify from 'unixify'
import { getFtlMessages, mergeVue } from '../..'

interface Options {
outDir: string,
inDir: string
}

const log = console.log.bind(console)

export const run = async (flags: Options) => {
let count = 0
for await (const file of stream(unixify(flags.inDir) + '/**', {ignore: ['**/node_modules']})) {
count ++
const fileString = file.toString()
const fileDirName= dirname(file.toString())

const vueComponentName = basename(fileString).replace(extname(fileString), '')
const outputPath = join(relative(flags.inDir, fileDirName), vueComponentName)
const [locale, ...rest] = outputPath.split(sep)
const realOutputPath = join(flags.outDir, rest.join(sep))

const vueFile = await fs.readFile(realOutputPath)
const data = await fs.readFile(file)
const ftlMessages = getFtlMessages(data.toString())
const newData = mergeVue(vueFile.toString(), locale, ftlMessages)

await fs.writeFile(realOutputPath, newData)
}

log(`Imported messages to ${count} files`)
}

11 changes: 10 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import cac from 'cac'

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

import { run as runImport } from './commands/import'

const cli = cac('fluent-vue')

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

cli
.command('import', 'Import translation from ftl files into Vue.js SFC')
.option('--in-dir', 'Input directory with ftl files', { default: 'translations/' })
.option('--out-dir', 'Output directory for extracted vue files', { default: 'example/' })
.action(runImport)

cli.help()

cli.parse()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Comment
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
greeting = Hello, { $name }
.aria-label = Label value