Skip to content

Commit

Permalink
fix(generate): ignore any directories/files that begin with an unders…
Browse files Browse the repository at this point in the history
…core

fixes #43
  • Loading branch information
yassinedoghri committed Sep 24, 2022
1 parent c17ba01 commit a7e6f08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
62 changes: 27 additions & 35 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import {
crawlInputDirectory,
getAstroPagesPath,
createFiles,
FileToGenerate,
generateLocalizedFrontmatter,
Expand All @@ -21,46 +21,38 @@ export const generate = (
supportedLanguages: string[],
outputPath: string = inputPath
): FileToGenerate[] => {
const files = crawlInputDirectory(inputPath);
const astroPagesPaths = getAstroPagesPath(inputPath);

const filesToGenerate: FileToGenerate[] = [];

files.forEach(async function (file) {
astroPagesPaths.forEach(async function (file: string) {
const inputFilePath = [inputPath, file].join("/");
const extension = file.split(".").pop();

// only parse astro files
if (extension === "astro") {
const fileContents = fs.readFileSync(inputFilePath);
const fileContentsString = fileContents.toString();

const parsedFrontmatter = parseFrontmatter(fileContentsString);

supportedLanguages.forEach((language) => {
const frontmatterCode = generateLocalizedFrontmatter(
parsedFrontmatter,
language,
language === defaultLanguage ? 0 : 1
);

// get the astro file contents
const newFileContents = overwriteAstroFrontmatter(
fileContentsString,
frontmatterCode
);

filesToGenerate.push({
path: [
outputPath,
language === defaultLanguage ? null : language,
file,
]
.filter(Boolean)
.join("/"),
source: newFileContents,
});
const fileContents = fs.readFileSync(inputFilePath);
const fileContentsString = fileContents.toString();

const parsedFrontmatter = parseFrontmatter(fileContentsString);

supportedLanguages.forEach((language) => {
const frontmatterCode = generateLocalizedFrontmatter(
parsedFrontmatter,
language,
language === defaultLanguage ? 0 : 1
);

// get the astro file contents
const newFileContents = overwriteAstroFrontmatter(
fileContentsString,
frontmatterCode
);

filesToGenerate.push({
path: [outputPath, language === defaultLanguage ? null : language, file]
.filter(Boolean)
.join("/"),
source: newFileContents,
});
}
});
});

createFiles(filesToGenerate);
Expand Down
14 changes: 11 additions & 3 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,20 @@ export const generateLocalizedFrontmatter = (
);
};

export const crawlInputDirectory = (directoryPath: string): PathsOutput => {
/**
* Crawls pages directory and returns all Astro pages
* except for locale folders and excluded pages / directories (starting with underscore).
* (https://docs.astro.build/en/core-concepts/routing/#excluding-pages)
*
* @param pagesDirectoryPath
*/
export const getAstroPagesPath = (pagesDirectoryPath: string): PathsOutput => {
// eslint-disable-next-line new-cap
const api = new fdir()
.exclude((dirName) => isLocale(dirName))
.filter((filename) => !path.basename(filename).startsWith("_"))
.exclude((dirName) => isLocale(dirName) || dirName.startsWith("_"))
.withRelativePaths()
.crawl(directoryPath);
.crawl(pagesDirectoryPath);

return api.sync() as PathsOutput;
};
Expand Down

0 comments on commit a7e6f08

Please sign in to comment.