Skip to content

Commit

Permalink
fix: add levels to Astro.global pattern and scripts' import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinedoghri committed Jan 8, 2023
1 parent 1998309 commit 9d88d79
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 27 deletions.
10 changes: 10 additions & 0 deletions src/__tests__/cli/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ describe("transformer(...)", () => {
expected:
'import { changeLanguage } from "i18next";\nimport Foo from "../../Foo.astro";\nimport Bar from "../../../Bar.astro";\nimport { baz } from "../baz";\n\nchangeLanguage("fr");\n\n',
},
{
name: "with Astro.glob relative path",
actual: `const astroGlob = Astro.glob("../foo/bar/*.mdx");`,
expected: `import { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\nconst astroGlob = Astro.glob("../../foo/bar/*.mdx");\n`,
},
{
name: "with multiple Astro.glob relative paths",
actual: `const astroGlob = Astro.glob("../foo/bar/*.mdx");\nconst astroGlob2 = Astro.glob("../baz/*.mdx");`,
expected: `import { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\nconst astroGlob = Astro.glob("../../foo/bar/*.mdx");\nconst astroGlob2 = Astro.glob("../../baz/*.mdx");\n`,
},
];

codeStringTests.forEach((codeStringTest) => {
Expand Down
63 changes: 37 additions & 26 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
overwriteAstroFrontmatter,
parseFrontmatter,
resolveTranslatedAstroPath,
addDepthToRelativePath,
} from "./utils";

/**
Expand Down Expand Up @@ -39,40 +40,50 @@ export const generate = (
: [inputPath, astroFilePath].join("/");

const fileContents = fs.readFileSync(inputFilePath);
const fileContentsString = fileContents.toString();
let fileContentsString = fileContents.toString();

const parsedFrontmatter = parseFrontmatter(fileContentsString);

locales.forEach((locale) => {
const isOtherLocale = locale !== defaultLocale;
if (astroFilePath === "blog/index.astro") {
locales.forEach((locale) => {
const isOtherLocale = locale !== defaultLocale;
const fileDepth = showDefaultLocale ? 0 : Number(isOtherLocale);

const frontmatterCode = generateLocalizedFrontmatter(
parsedFrontmatter,
locale,
// If showDefaultLocale then we want to have 0 depth since 1 depth was
// already added by the defaultLocale folder
// Else we add depth only when the locale is not the default one
showDefaultLocale ? 0 : Number(isOtherLocale)
);
const frontmatterCode = generateLocalizedFrontmatter(
parsedFrontmatter,
locale,
// If showDefaultLocale then we want to have 0 depth since 1 depth was
// already added by the defaultLocale folder
// Else we add depth only when the locale is not the default one
fileDepth
);

// get the astro file contents
const newFileContents = overwriteAstroFrontmatter(
fileContentsString,
frontmatterCode
);
// edit sources' script relative imports
fileContentsString = fileContentsString.replace(
/import\s+["'](\..*)["']/g,
(_, relativePath) =>
`import "${addDepthToRelativePath(relativePath, fileDepth)}"`
);

const createLocaleFolder = showDefaultLocale ? true : isOtherLocale;
// get the astro file contents
const newFileContents = overwriteAstroFrontmatter(
fileContentsString,
frontmatterCode
);

filesToGenerate.push({
path: resolveTranslatedAstroPath(
astroFilePath,
createLocaleFolder ? locale : undefined,
outputPath,
flatRoutes
),
source: newFileContents,
const createLocaleFolder = showDefaultLocale ? true : isOtherLocale;

filesToGenerate.push({
path: resolveTranslatedAstroPath(
astroFilePath,
createLocaleFolder ? locale : undefined,
outputPath,
flatRoutes
),
source: newFileContents,
});
});
});
}
});

createFiles(filesToGenerate);
Expand Down
24 changes: 24 additions & 0 deletions src/cli/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =
);
}

// update Astro glob relative paths
if (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) &&
ts.isStringLiteral(node.arguments[0]) &&
node.expression.expression.escapedText === "Astro" &&
node.expression.name.escapedText === "glob"
) {
return factory.updateCallExpression(
node,
node.expression,
node.typeArguments,
[
factory.createStringLiteral(
addDepthToRelativePath(
node.arguments[0].text,
fileDepth as number
)
),
]
);
}

// remove any occurrence of changeLanguage() call
if (
ts.isExpressionStatement(node) &&
Expand Down
2 changes: 1 addition & 1 deletion src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const addDepthToRelativePath = (
relativePath: string,
depth: number = 1
): string => {
if (relativePath.startsWith("./")) {
if (relativePath.startsWith("./") && depth > 0) {
// remove "./" from relativePath
relativePath = relativePath.slice(2);
}
Expand Down
19 changes: 19 additions & 0 deletions website/src/pages/blog/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import { changeLanguage } from "i18next";
import { hello } from "../hello";
changeLanguage("en");
hello();
const frAstroFiles = Astro.glob("../fr/*.astro");
console.log(frAstroFiles);
---

<script>
import "../scripts/pageBackgroundChanger.ts";
import "../scripts/secondBackgroundChanger.ts";
import "./foo/hello/scripts/third.ts";
import "https://cool.com/foo/hello/scripts/third.ts";
</script>

<div>hello</div>
19 changes: 19 additions & 0 deletions website/src/pages/fr/blog/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import { changeLanguage } from "i18next";
import { hello } from "../../hello";
changeLanguage("fr");
hello();
const frAstroFiles = Astro.glob("../../fr/*.astro");
console.log(frAstroFiles);
---

<script>
import "../../scripts/pageBackgroundChanger.ts";
import "../../scripts/secondBackgroundChanger.ts";
import "../foo/hello/scripts/third.ts";
import "https://cool.com/foo/hello/scripts/third.ts";
</script>

<div>hello</div>
1 change: 1 addition & 0 deletions website/src/pages/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hello = () => console.log("hello");

0 comments on commit 9d88d79

Please sign in to comment.