Skip to content

Commit

Permalink
fix: don't generate useless empty files if no translatable strings fo…
Browse files Browse the repository at this point in the history
…und (#270)
  • Loading branch information
amcgee authored and varl committed Jan 17, 2020
1 parent 190e7aa commit 27a8ab8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
22 changes: 18 additions & 4 deletions cli/src/commands/i18n.js
@@ -1,4 +1,4 @@
const { namespace } = require('@dhis2/cli-helpers-engine')
const { namespace, reporter } = require('@dhis2/cli-helpers-engine')
const i18n = require('../lib/i18n')

const generate = {
Expand All @@ -19,15 +19,21 @@ const generate = {
namespace: {
alias: 'n',
description: 'Namespace for app locale separation',
required: true,
default: 'default',
},
},
handler: async argv => {
await i18n.generate({
const result = await i18n.generate({
input: argv.path,
output: argv.output,
namespace: argv.namespace,
})

if (!result) {
reporter.error('Failed to generate i18n code.')
process.exit(1)
}
reporter.info(`Done!`)
},
}
const extract = {
Expand All @@ -46,7 +52,15 @@ const extract = {
},
},
handler: async argv => {
await i18n.extract({ input: argv.path, output: argv.output })
const result = await i18n.extract({
input: argv.path,
output: argv.output,
})
if (!result) {
reporter.error('Failed to extract i18n strings.')
process.exit(1)
}
reporter.info(`Done!`)
},
}

Expand Down
41 changes: 28 additions & 13 deletions cli/src/lib/i18n/extract.js
@@ -1,22 +1,26 @@
const fs = require('fs-extra')
const path = require('path')
const {
ensureDirectoryExists,
walkDirectory,
arrayEqual,
} = require('./helpers')
const { checkDirectoryExists, walkDirectory, arrayEqual } = require('./helpers')
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const scanner = require('i18next-scanner')
const { i18nextToPot, gettextToI18next } = require('i18next-conv')

const extract = async ({ input, output }) => {
ensureDirectoryExists(input)
const relativeInput = './' + path.relative(process.cwd(), input)
if (!checkDirectoryExists(input)) {
reporter.error(
`I18n source directory ${chalk.bold(relativeInput)} does not exist.`
)
return false
}

reporter.debug(`[i18n-extract] Reading ${input}...`)
reporter.debug(`[i18n-extract] Reading ${chalk.bold(relativeInput)}...`)

const files = walkDirectory(input)
if (files.length === 0) {
reporter.error(`Directory ${input} has no files to translate.`)
reporter.error(
`Directory ${chalk.bold(relativeInput)} has no files to translate.`
)
process.exit(1)
}

Expand All @@ -35,9 +39,19 @@ const extract = async ({ input, output }) => {

var parsed = parser.get()
var en = {}

Object.keys(parsed.en.translation).forEach(str => (en[str] = ''))

if (Object.keys(en).length === 0) {
reporter.print(
chalk.dim(
`No translatable strings found in directory ${chalk.bold(
relativeInput
)}`
)
)
return true
}

var targetPath = path.join(output, 'en.pot')

if (fs.existsSync(targetPath)) {
Expand All @@ -50,22 +64,23 @@ const extract = async ({ input, output }) => {

if (arrayEqual(newMsgIds, msgIds)) {
reporter.print(chalk.dim('No i18n updates found!'))
return
return true
}
}

reporter.print(
chalk.dim(
`Writing ${
Object.keys(en).length
} language strings to ${targetPath}...`
`Writing ${Object.keys(en).length} language strings to ${chalk.bold(
'./' + targetPath
)}...`
)
)
const result = await i18nextToPot('en', JSON.stringify(en))

fs.ensureFileSync(targetPath)
fs.writeFileSync(targetPath, result + '\n')
reporter.debug('[i18n-extract] complete')
return true
}

module.exports = extract
15 changes: 12 additions & 3 deletions cli/src/lib/i18n/generate.js
@@ -1,10 +1,10 @@
const fs = require('fs-extra')
const path = require('path')
const { reporter } = require('@dhis2/cli-helpers-engine')
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const { gettextToI18next } = require('i18next-conv')
const handlebars = require('handlebars')

const { ensureDirectoryExists } = require('./helpers')
const { checkDirectoryExists } = require('./helpers')
const { langToLocale } = require('./locales')

const writeTemplate = (outFile, data) => {
Expand All @@ -17,7 +17,15 @@ const writeTemplate = (outFile, data) => {
}

const generate = async ({ input, output, namespace }) => {
ensureDirectoryExists(input)
if (!checkDirectoryExists(input)) {
const relativeInput = './' + path.relative(process.cwd(), input)
reporter.debug(
`Source directory ${chalk.bold(
relativeInput
)} does not exist, skipping i18n generation`
)
return false
}

// clean-up and create destination dir.
const dst = path.normalize(output)
Expand Down Expand Up @@ -58,6 +66,7 @@ const generate = async ({ input, output, namespace }) => {
})

await Promise.all(promises)
return true
}

module.exports = generate
11 changes: 6 additions & 5 deletions cli/src/lib/i18n/helpers.js
Expand Up @@ -4,21 +4,22 @@ const path = require('path')

const supportedExtensions = ['.js', '.jsx', '.ts', '.tsx']

module.exports.ensureDirectoryExists = dir => {
module.exports.checkDirectoryExists = dir => {
const dirPath = path.normalize(dir)
try {
const stat = fs.lstatSync(dirPath)

if (!stat.isDirectory()) {
reporter.error(
reporter.debugErr(
`Directory ${chalk.bold(dirPath)} is not a directory.`
)
process.exit(1)
return false
}
} catch (e) {
reporter.error(`Directory ${chalk.bold(dirPath)} does not exist.`)
process.exit(1)
reporter.debugErr(`Directory ${chalk.bold(dirPath)} does not exist.`)
return false
}
return true
}

// src from component/array-equal
Expand Down

0 comments on commit 27a8ab8

Please sign in to comment.