Skip to content

Commit

Permalink
fix(generate): replace isLocale check with user defined locales to pr…
Browse files Browse the repository at this point in the history
…event nested folders generation

fixes #56
  • Loading branch information
yassinedoghri committed Nov 6, 2022
1 parent 761f02f commit a598e2e
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 99 deletions.
31 changes: 0 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
"fdir": "^5.3.0",
"fs-extra": "^10.1.0",
"husky": "^8.0.1",
"iso-3166-1-alpha-2": "^1.0.0",
"lint-staged": "^13.0.3",
"prettier": "2.7.1",
"prettier-plugin-astro": "^0.7.0",
Expand Down
39 changes: 0 additions & 39 deletions src/__tests__/cli/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,9 @@ import {
doesStringIncludeFrontmatter,
extractFrontmatterFromAstroSource,
isFileHidden,
isLocale,
overwriteAstroFrontmatter,
} from "../../cli/utils";

describe("isLocale(...)", () => {
it("with correct iso6391 only", () => {
expect(isLocale("en")).toBe(true);
expect(isLocale("fr")).toBe(true);
expect(isLocale("es")).toBe(true);
expect(isLocale("de")).toBe(true);
});

it("with correct iso6391 and iso33661a2", () => {
expect(isLocale("en-US")).toBe(true);
expect(isLocale("fr-FR")).toBe(true);
expect(isLocale("fr-BE")).toBe(true);
expect(isLocale("pt-BR")).toBe(true);
});

it("with wrong format / random strings", () => {
expect(isLocale("en_US")).toBe(false);
expect(isLocale("frFR")).toBe(false);
expect(isLocale("foo")).toBe(false);
expect(isLocale("bar")).toBe(false);
});

it("with incorrect iso6391 only", () => {
expect(isLocale("cc")).toBe(false);
expect(isLocale("zz")).toBe(false);
});

it("with correct iso33661a2 but incorrect iso6391", () => {
expect(isLocale("cc-FR")).toBe(false);
expect(isLocale("cc-FR")).toBe(false);
});

it("with correct iso6391 but incorrect iso33661a2", () => {
expect(isLocale("en-AA")).toBe(false);
expect(isLocale("en-AA")).toBe(false);
});
});

describe("doesStringIncludeFrontmatter(...)", () => {
it("with frontmatter in source", () => {
expect(
Expand Down
4 changes: 2 additions & 2 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const generate = (

// default locale page paths
const astroPagesPaths = showDefaultLocale
? getAstroPagesPath(inputPath, defaultLocale)
: getAstroPagesPath(inputPath);
? getAstroPagesPath(inputPath, defaultLocale, locales)
: getAstroPagesPath(inputPath, undefined, locales);

const filesToGenerate: FileToGenerate[] = [];
astroPagesPaths.forEach(async function (astroFilePath: string) {
Expand Down
29 changes: 3 additions & 26 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { fdir, PathsOutput } from "fdir";
import fsExtra from "fs-extra";
import ISO6391 from "iso-639-1";
import ISO33661a2 from "iso-3166-1-alpha-2";
import path from "path";
import fs from "fs";
import ts from "typescript";
Expand All @@ -13,28 +11,6 @@ export interface FileToGenerate {
source: string;
}

export const isLocale = (code: string): boolean => {
const REGEX = /^(?<iso6391>[a-z]{2})(-(?<iso33661a2>[A-Z]{2}))?$/;

if (!REGEX.test(code)) {
return false;
}

const {
groups: { iso6391, iso33661a2 },
} = REGEX.exec(code);

if (iso33661a2 && !ISO33661a2.getCountry(iso33661a2)) {
return false;
}

if (!ISO6391.validate(iso6391)) {
return false;
}

return true;
};

export const doesStringIncludeFrontmatter = (source: string): boolean =>
/---.*---/s.test(source);

Expand Down Expand Up @@ -124,14 +100,15 @@ export const generateLocalizedFrontmatter = (
*/
export const getAstroPagesPath = (
pagesDirectoryPath: string,
childDirToCrawl = undefined as AstroI18nextConfig["defaultLocale"] | undefined
childDirToCrawl: AstroI18nextConfig["defaultLocale"] | undefined = undefined,
locales: AstroI18nextConfig["locales"] = []
): PathsOutput => {
// eslint-disable-next-line new-cap
const api = new fdir()
.filter(
(filepath) => !isFileHidden(filepath) && filepath.endsWith(".astro")
)
.exclude((dirName) => isLocale(dirName))
.exclude((dirName) => locales.includes(dirName))
.withRelativePaths();

return childDirToCrawl
Expand Down

0 comments on commit a598e2e

Please sign in to comment.