Skip to content

Commit

Permalink
fix: add isFileHidden function + tests to prevent missing hidden files
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinedoghri committed Nov 6, 2022
1 parent 8106b23 commit 7dcd0aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/cli/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
addDepthToRelativePath,
doesStringIncludeFrontmatter,
extractFrontmatterFromAstroSource,
isFileHidden,
isLocale,
overwriteAstroFrontmatter,
} from "../../cli/utils";
Expand Down Expand Up @@ -132,3 +133,18 @@ describe("addDepthToRelativePath(...)", () => {
);
});
});

describe("isFileVisible(...)", () => {
it("with hidden files", () => {
expect(isFileHidden("_foo.ts")).toBe(true);
expect(isFileHidden("_foo/bar.ts")).toBe(true);
expect(isFileHidden("_foo/bar/baz.ts")).toBe(true);
expect(isFileHidden("foo/_bar/baz.ts")).toBe(true);
});

it("with visible files", () => {
expect(isFileHidden("foo.ts")).toBe(false);
expect(isFileHidden("foo/bar.ts")).toBe(false);
expect(isFileHidden("foo/bar/baz.ts")).toBe(false);
});
});
12 changes: 9 additions & 3 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export const addDepthToRelativePath = (
return relativePath.padStart(relativePath.length + depth * 3, "../");
};

/**
* file is hidden if its name or any of its parent folders start with an underscore
*/
export const isFileHidden = (filepath: string): boolean => {
return /((^_)|(\/_))/.test(filepath);
};

/* c8 ignore start */
/**
* parse frontmatter using typescript compiler
Expand Down Expand Up @@ -116,10 +123,9 @@ export const getAstroPagesPath = (pagesDirectoryPath: string): PathsOutput => {
// eslint-disable-next-line new-cap
const api = new fdir()
.filter(
(filename) =>
!path.basename(filename).startsWith("_") && filename.endsWith(".astro")
(filepath) => !isFileHidden(filepath) && filepath.endsWith(".astro")
)
.exclude((dirName) => isLocale(dirName) || dirName.startsWith("_"))
.exclude((dirName) => isLocale(dirName))
.withRelativePaths()
.crawl(pagesDirectoryPath);

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default (options?: AstroI18nextOptions): AstroIntegration => {
astroI18nextConfig.defaultLanguage === ""
) {
throw new Error(
"[astro-i18next]: you must set a `baseLanguage` in your astroI18nextConfig!"
"[astro-i18next]: you must set a `defaultLanguage` in your astroI18nextConfig!"
);
}

Expand Down

0 comments on commit 7dcd0aa

Please sign in to comment.