This commit is contained in:
Eemeli Aro 2021-09-10 15:48:47 +03:00
Родитель f093b3431c
Коммит 50b8eeed9f
3 изменённых файлов: 9 добавлений и 90 удалений

85
cli.js
Просмотреть файл

@ -1,43 +1,11 @@
#!/usr/bin/env node
import { parse as parseFluent, serialize } from '@fluent/syntax'
import { existsSync } from 'fs'
import { readFile, writeFile } from 'fs/promises'
import yargs from 'yargs'
//import { buildFTL } from './lib/build-fluent-message.js'
import { forEachPropertiesFile, getInfo, getVars } from './lib/get-info.js'
import { parseProperties } from './lib/parse-properties.js'
import { forEachPropertiesFile, getInfo } from './lib/get-info.js'
import { transformJs } from './lib/transform-js.js'
yargs(process.argv.slice(2))
.options({
ftl: {
alias: 'f',
desc: 'Target Fluent file. If empty, stdout is used.',
requiresArg: true,
type: 'string'
},
props: {
alias: 'p',
desc: 'Source properties file',
requiresArg: true,
type: 'string'
},
globals: {
alias: 'g',
default: [],
desc: 'Global variables that are presumed to be bundles.',
requiresArg: true,
type: 'array'
},
prefix: {
alias: 'k',
default: '',
desc: 'Prefix for FTL message keys',
requiresArg: true,
type: 'string'
},
dryRun: {
alias: 'n',
desc: 'Do not write changes to disk',
@ -45,52 +13,17 @@ yargs(process.argv.slice(2))
},
root: {
alias: 'r',
desc: 'Root of mozilla-central, if not the current working directory',
desc: 'Root of mozilla-central (usually autodetected)',
requiresArg: true,
type: 'string'
},
include: {
alias: 'i',
default: [],
desc: 'Keys to include. If empty, all are included.',
requiresArg: true,
type: 'array'
},
exclude: {
alias: 'e',
default: [],
desc: 'Keys to exclude. If empty, all are included.',
requiresArg: true,
type: 'array'
}
})
//.command(
// '$0',
// 'Convert .properties to .ftl',
// { props: { demandOption: true } },
// async ({ ftl, props, prefix, include, exclude }) => {
// const src = await readFile(props, 'utf8')
// const ast = parseProperties(src, include, exclude)
// /** @type {import('@fluent/syntax').Resource} */
// let res = null
// if (ftl && existsSync(ftl)) {
// const ftlSrc = await readFile(ftl, 'utf8')
// res = parseFluent(ftlSrc, { withSpans: true })
// }
// res = buildFTL(res, ast, { prefix })
// if (ftl) await writeFile(ftl, serialize(res))
// else console.log(serialize(res))
// }
//)
.command(
'js <jsPath>',
'$0 <jsPath>',
'Convert JS files to use Fluent Localization rather than string bundles',
{},
({ jsPath, dryRun }) => transformJs(jsPath, { dryRun })
({ jsPath, dryRun, root }) => transformJs(jsPath, { dryRun, root })
)
.command(
@ -100,12 +33,8 @@ yargs(process.argv.slice(2))
(args) => forEachPropertiesFile(args.filename, getInfo)
)
.command(
'vars [filename..]',
'Show variables used in .properties files',
{},
(args) => forEachPropertiesFile(args.filename, getVars)
)
.help()
.epilogue(
'For more information, see: https://github.com/eemeli/properties-to-ftl'
)
.parse()

Просмотреть файл

@ -13,17 +13,6 @@ export async function getInfo(path) {
return info
}
export async function getVars(path) {
const src = await readFile(path, 'utf8')
const vars = {}
for (const msg of Object.values(parse(src))) {
const m = msg.match(/(%(\d\$)?|\$)([^\WS0-9]+|{.+?})/g)
if (m) for (const v of m) vars[v] = true
}
const keys = Object.keys(vars)
return keys.length > 0 ? keys : null
}
export async function forEachPropertiesFile(roots, cb) {
for (const root of roots) {
const path = resolve(root)

Просмотреть файл

@ -146,7 +146,7 @@ function setLocalizationCall(path, key, ftlMsg, fmtArgs) {
* @param {string} jsPath
*/
export async function transformJs(jsPath, { dryRun, root } = {}) {
if (dryRun) console.warn('DRY RUN: Not writing changes to disk.')
if (dryRun) console.warn('--- DRY RUN: Not writing changes to disk.\n')
if (!root) root = await findRoot()
if (!root) {
console.error('Error: Project root not found!')
@ -359,6 +359,7 @@ export async function transformJs(jsPath, { dryRun, root } = {}) {
` !!! Fix L10N-FIXME issues manually near lines:`,
Array.from(fixmeLines)
)
if (dryRun) console.warn('\n--- DRY RUN: Not writing changes to disk.')
}
async function findRoot(dir = process.cwd()) {