Skip to content

Commit

Permalink
fix: @lingui/cli type error when catalog is missing (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
capaj committed Mar 1, 2021
1 parent 18d985d commit 8c44af2
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions packages/cli/src/api/catalog.ts
Expand Up @@ -21,7 +21,7 @@ const LOCALE = "{locale}"
const LOCALE_SUFFIX_RE = /\{locale\}.*$/
const PATHSEP = "/" // force posix everywhere

type MessageOrigin = [string, number];
type MessageOrigin = [string, number]

export type ExtractedMessageType = {
message?: string
Expand Down Expand Up @@ -146,7 +146,7 @@ export class Catalog {
try {
let paths = this.sourcePaths
if (options.files) {
options.files = options.files.map(p => normalize(p, false))
options.files = options.files.map((p) => normalize(p, false))
const regex = new RegExp(options.files.join("|"), "i")
paths = paths.filter((path: string) => regex.test(path))
}
Expand Down Expand Up @@ -178,7 +178,11 @@ export class Catalog {
.filter(Boolean)
.reduce(
(catalog, messages) =>
R.mergeWithKey(mergeOriginsAndExtractedComments, catalog, messages),
R.mergeWithKey(
mergeOriginsAndExtractedComments,
catalog,
messages
),
{}
)
})(tmpDir)
Expand Down Expand Up @@ -248,7 +252,7 @@ export class Catalog {
getTranslations(locale: string, options: GetTranslationsOptions) {
const catalogs = this.readAll()
return R.mapObjIndexed(
(value, key) => this.getTranslation(catalogs, locale, key, options),
(_value, key) => this.getTranslation(catalogs, locale, key, options),
catalogs[locale]
)
}
Expand All @@ -265,24 +269,27 @@ export class Catalog {

const getTranslation = (locale) => {
const configLocales = this.config.locales.join('", "')

if (!catalogs[locale].hasOwnProperty(key)) {
console.error(`Message with key ${key} is missing in locale ${locale}`);
return null;
}


if (catalogs[locale]) {
return catalogs[locale][key].translation
}
const localeCatalog = catalogs[locale]

console.warn(`
if (!localeCatalog) {
console.warn(`
Catalog "${locale}" isn't present in locales config parameter
Add "${locale}" to your lingui.config.js:
{
locales: ["${configLocales}", "${locale}"]
}
`)
return null
}
if (!localeCatalog.hasOwnProperty(key)) {
console.error(`Message with key ${key} is missing in locale ${locale}`)
return null
}

if (catalogs[locale]) {
return catalogs[locale][key].translation
}

return null
}

Expand Down Expand Up @@ -354,8 +361,12 @@ export class Catalog {
poFormat.write(filename, messages, options)
}

writeCompiled(locale: string, compiledCatalog: string, namespace?: CompiledCatalogNamespace) {
let ext;
writeCompiled(
locale: string,
compiledCatalog: string,
namespace?: CompiledCatalogNamespace
) {
let ext: string
if (namespace === "es") {
ext = "mjs"
} else if (namespace === "ts") {
Expand Down Expand Up @@ -397,22 +408,23 @@ export class Catalog {
}

get sourcePaths() {
const includeGlobs = this.include.map(
(includePath) => {
const isDir = fs.existsSync(includePath) && fs.lstatSync(includePath).isDirectory()
/**
* glob library results from absolute patterns such as /foo/* are mounted onto the root setting using path.join.
* On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
*/
return isDir ? normalize(
path.resolve(
process.cwd(),
includePath === "/" ? "" : includePath,
"**/*.*"
const includeGlobs = this.include.map((includePath) => {
const isDir =
fs.existsSync(includePath) && fs.lstatSync(includePath).isDirectory()
/**
* glob library results from absolute patterns such as /foo/* are mounted onto the root setting using path.join.
* On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
*/
return isDir
? normalize(
path.resolve(
process.cwd(),
includePath === "/" ? "" : includePath,
"**/*.*"
)
)
) : includePath
}
)
: includePath
})

const patterns =
includeGlobs.length > 1 ? `{${includeGlobs.join(",")}}` : includeGlobs[0]
Expand Down Expand Up @@ -504,7 +516,7 @@ export function getCatalogs(config: LinguiConfig) {
patterns.length > 1 ? `{${patterns.join(",")}` : patterns[0],
{
ignore: exclude,
mark: true
mark: true,
}
)

Expand Down Expand Up @@ -533,7 +545,7 @@ export function getCatalogForFile(file: string, catalogs: Array<Catalog>) {
const catalogGlob = catalogFile.replace(LOCALE, "*")
const match = micromatch.capture(
normalizeRelativePath(path.relative(catalog.config.rootDir, catalogGlob)),
normalizeRelativePath(file),
normalizeRelativePath(file)
)
if (match) {
return {
Expand Down Expand Up @@ -631,8 +643,12 @@ export function normalizeRelativePath(sourcePath: string): string {
return normalize(sourcePath, false)
}

const isDir = fs.existsSync(sourcePath) && fs.lstatSync(sourcePath).isDirectory()
return normalize(path.relative(process.cwd(), sourcePath), false) + (isDir ? "/" : "")
const isDir =
fs.existsSync(sourcePath) && fs.lstatSync(sourcePath).isDirectory()
return (
normalize(path.relative(process.cwd(), sourcePath), false) +
(isDir ? "/" : "")
)
}

export const cleanObsolete = R.filter(
Expand Down

0 comments on commit 8c44af2

Please sign in to comment.