From a598e2ebb7b1e3c7a2073ed626aae39fe5ef580b Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Sun, 6 Nov 2022 17:40:00 +0000 Subject: [PATCH] fix(generate): replace isLocale check with user defined locales to prevent nested folders generation fixes #56 --- package-lock.json | 31 -------------------------- package.json | 1 - src/__tests__/cli/utils.test.ts | 39 --------------------------------- src/cli/generate.ts | 4 ++-- src/cli/utils.ts | 29 +++--------------------- 5 files changed, 5 insertions(+), 99 deletions(-) diff --git a/package-lock.json b/package-lock.json index 486bda7..4c26d6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,7 +47,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", @@ -7865,15 +7864,6 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, - "node_modules/iso-3166-1-alpha-2": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/iso-3166-1-alpha-2/-/iso-3166-1-alpha-2-1.0.0.tgz", - "integrity": "sha512-ffnjHqGTL67bPVuOf7NNl7NhPABR1Dfoue+tXMCSzyBbfmwo2Y2Xve9nL3zZZ8YXhV9H7yL1u+HDeuJd8aft0Q==", - "dev": true, - "dependencies": { - "mout": "^0.11.0" - } - }, "node_modules/iso-639-1": { "version": "2.1.15", "resolved": "https://registry.npmjs.org/iso-639-1/-/iso-639-1-2.1.15.tgz", @@ -9971,12 +9961,6 @@ "node": ">=0.10.0" } }, - "node_modules/mout": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/mout/-/mout-0.11.1.tgz", - "integrity": "sha512-pK9VNiLE3QgGBrC/3ICAscwOLU7oTNeK2l32uqNAioBYtB2tQAfSsGDNChUlk7CP23126mc5lUt6+na9FlN8JA==", - "dev": true - }, "node_modules/mri": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz", @@ -22481,15 +22465,6 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, - "iso-3166-1-alpha-2": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/iso-3166-1-alpha-2/-/iso-3166-1-alpha-2-1.0.0.tgz", - "integrity": "sha512-ffnjHqGTL67bPVuOf7NNl7NhPABR1Dfoue+tXMCSzyBbfmwo2Y2Xve9nL3zZZ8YXhV9H7yL1u+HDeuJd8aft0Q==", - "dev": true, - "requires": { - "mout": "^0.11.0" - } - }, "iso-639-1": { "version": "2.1.15", "resolved": "https://registry.npmjs.org/iso-639-1/-/iso-639-1-2.1.15.tgz", @@ -23944,12 +23919,6 @@ "integrity": "sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==", "dev": true }, - "mout": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/mout/-/mout-0.11.1.tgz", - "integrity": "sha512-pK9VNiLE3QgGBrC/3ICAscwOLU7oTNeK2l32uqNAioBYtB2tQAfSsGDNChUlk7CP23126mc5lUt6+na9FlN8JA==", - "dev": true - }, "mri": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz", diff --git a/package.json b/package.json index 7a7b870..1943ee5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/cli/utils.test.ts b/src/__tests__/cli/utils.test.ts index 89ac029..fcf890f 100644 --- a/src/__tests__/cli/utils.test.ts +++ b/src/__tests__/cli/utils.test.ts @@ -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( diff --git a/src/cli/generate.ts b/src/cli/generate.ts index 7d0ac2a..2c48bcf 100644 --- a/src/cli/generate.ts +++ b/src/cli/generate.ts @@ -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) { diff --git a/src/cli/utils.ts b/src/cli/utils.ts index b2f6057..2f8df87 100644 --- a/src/cli/utils.ts +++ b/src/cli/utils.ts @@ -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"; @@ -13,28 +11,6 @@ export interface FileToGenerate { source: string; } -export const isLocale = (code: string): boolean => { - const REGEX = /^(?[a-z]{2})(-(?[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); @@ -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