From 5d3958638913d5f6b6d318bf21ce4f3019a69e88 Mon Sep 17 00:00:00 2001 From: Sergio M Date: Mon, 2 Nov 2020 18:11:18 +0100 Subject: [PATCH] fix: accept catalog paths without ending slash (#812) --- .../cli/src/api/__snapshots__/catalog.test.ts.snap | 2 +- packages/cli/src/api/catalog.test.ts | 13 +++++++++++++ packages/cli/src/api/catalog.ts | 5 +++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap index 1d0cdb67c..9ed17aded 100644 --- a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap +++ b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap @@ -363,7 +363,7 @@ Object { } `; -exports[`getCatalogs should warn about missing {name} pattern in catalog path 1`] = `Catalog with path "./locales/{locale}" doesn't have a {name} pattern in it, but one of source directories uses it: "{name}/". Either add {name} pattern to "./locales/{locale}" or remove it from all source directories.`; +exports[`getCatalogs should warn about missing {name} pattern in catalog path 1`] = `Catalog with path "./locales/{locale}" doesn't have a {name} pattern in it, but one of source directories uses it: "{name}". Either add {name} pattern to "./locales/{locale}" or remove it from all source directories.`; exports[`getCatalogs should warn if catalogPath is a directory 1`] = `Remove trailing slash from "./locales/{locale}/". Catalog path isn't a directory, but translation file without extension. For example, catalog path "./locales/{locale}" results in translation file "./locales/en.po".`; diff --git a/packages/cli/src/api/catalog.test.ts b/packages/cli/src/api/catalog.test.ts index d4f1096ed..a552a9780 100644 --- a/packages/cli/src/api/catalog.test.ts +++ b/packages/cli/src/api/catalog.test.ts @@ -786,6 +786,19 @@ describe("normalizeRelativePath", function () { absolute.split("\\").join("/") ) }) + + it("directories without ending slash are correctly treaten as dirs", function() { + mockFs({ + componentA: { + "index.js": mockFs.file(), + }, + "componentB": mockFs.file(), + }) + // checked correctly that is a dir, cuz added that ending slash + expect(normalizeRelativePath("./componentA")).toEqual("componentA/") + // ComponentB is a file shouldn't add ending slash + expect(normalizeRelativePath("./componentB")).toEqual("componentB") + }) }) describe("cleanObsolete", function () { diff --git a/packages/cli/src/api/catalog.ts b/packages/cli/src/api/catalog.ts index bc797b3d3..71428db81 100644 --- a/packages/cli/src/api/catalog.ts +++ b/packages/cli/src/api/catalog.ts @@ -288,7 +288,7 @@ export class Catalog { ) const patterns = includeGlobs.length > 1 ? `{${includeGlobs.join(",")}}` : includeGlobs[0] - return glob.sync(patterns, { ignore: this.exclude }) + return glob.sync(patterns, { ignore: this.exclude, mark: true }) } get localeDir() { @@ -372,6 +372,7 @@ export function getCatalogs(config: LinguiConfig) { patterns.length > 1 ? `{${patterns.join(",")}` : patterns[0], { ignore: exclude, + mark: true } ) @@ -497,7 +498,7 @@ export function normalizeRelativePath(sourcePath: string): string { return normalize(sourcePath, false) } - const isDir = normalize(sourcePath, false).endsWith(PATHSEP) + const isDir = fs.existsSync(sourcePath) && fs.lstatSync(sourcePath).isDirectory() return ( normalize(path.relative(process.cwd(), path.resolve(sourcePath))) + (isDir ? PATHSEP : "")