Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove obsolete files when extracting messages #235

Merged
merged 2 commits into from
Jun 30, 2018
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
18 changes: 11 additions & 7 deletions packages/babel-plugin-extract-messages/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,30 @@ export default function({ types: t }) {
*/
const localeDir = this.opts.localeDir || opts.localeDir
const { filename } = file.opts
const [basename] = fsPath.basename(filename).split(".", 2)
const baseDir = fsPath.dirname(fsPath.relative(optsBaseDir, filename))
const targetDir = fsPath.join(localeDir, "_build", baseDir)

const messages = file.get(MESSAGES)
const catalog = {}
const catalogFilename = fsPath.join(targetDir, `${basename}.json`)

// no messages, skip file
if (!messages.size) return
if (!messages.size) {
// clean any existing catalog
if (fs.existsSync(catalogFilename)) {
fs.writeFileSync(catalogFilename, JSON.stringify({}))
}

return
}

messages.forEach((value, key) => {
catalog[key] = value
})

mkdirp.sync(targetDir)
const [basename] = fsPath.basename(filename).split(".", 2)

fs.writeFileSync(
fsPath.join(targetDir, `${basename}.json`),
JSON.stringify(catalog, null, 2)
)
fs.writeFileSync(catalogFilename, JSON.stringify(catalog, null, 2))
}
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/api/formats/utils/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export type LocaleObject = {
* 4. country code
* @type {RegExp}
*/
export const localeRe = new RegExp(/(([a-z]+)([_-]([a-zA-Z]+))?)(\\|\/)?/)
export const localeRe = new RegExp(/(([a-z]+)([_-]([a-zA-Z]+))?)[\\/]?/)

export function isValid(locale: string): boolean {
const match = localeRe.exec(locale)
return (
match &&
match !== null &&
match[0] === locale && // locale has valid format
match[2] in plurals // language is valid (we have plurals for it)
)
Expand Down