Skip to content

Commit

Permalink
⚡️ simplified normalizeConfigPath
Browse files Browse the repository at this point in the history
  • Loading branch information
loucyx committed Jan 24, 2024
1 parent 17872ed commit 84f8d68
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,25 +363,19 @@ function invalidateVirtualMod(viteServer: ViteDevServer) {
}

/**
* Takes a configPath and returns a normalized relative version:
* Takes the source (`from`) and destination (`to`) of a config path and
* returns a normalized relative version:
* - If is not relative, it adds `./` to the beginning.
* - If it ends with `.ts`, it removes the extension.
* - It adds `.js` to the end.
* - It stringifies the result (adds `""` around it).
* @param from Config path from path.
* @param to Config path to path.
* - If it ends with `.ts`, it replaces it with `.js`.
* - It adds `""` around the string.
* @param from Config path source.
* @param to Config path destination.
* @returns Normalized config path.
*/
function normalizeConfigPath(from: string, to: string) {
const normalizedConfigPath = path.relative(from, to);

return JSON.stringify(
`${isRelativePath(normalizedConfigPath) ? '' : './'}${
normalizedConfigPath.endsWith('.ts')
? normalizedConfigPath.replace(/\.ts$/, '')
: normalizedConfigPath
}.js`
);
const configPath = path.relative(from, to).replace(/\.ts$/, '.js');

return `"${isRelativePath(configPath) ? '' : './'}${configPath}"` as const;
}

async function writeContentFiles({
Expand Down

0 comments on commit 84f8d68

Please sign in to comment.