Skip to content

Commit

Permalink
chore: changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Nov 19, 2024
1 parent 2d53e1b commit 893accf
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .changeset/thirty-clocks-jump.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'astro': patch
'astro': minor
---

Changes the default content config location from `src/content/config.*` to `src/content.config.*`.
Expand Down
14 changes: 12 additions & 2 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ type GlobResult = Record<string, LazyImport>;
type CollectionToEntryMap = Record<string, GlobResult>;
type GetEntryImport = (collection: string, lookupId: string) => Promise<LazyImport>;

export function getImporterFilename() {
// The 4th line in the stack trace should be the importer filename
const stackLine = new Error().stack?.split('\n')?.[3];
if (!stackLine) {
return null;
}
// Extract the relative path from the stack line
const match = /\/(src\/.*?):\d+:\d+/.exec(stackLine);
return match?.[1] ?? null;
}

export function defineCollection(config: any) {
if ('loader' in config) {
if (config.type && config.type !== CONTENT_LAYER_TYPE) {
throw new AstroUserError(
'Collections that use the Content Layer API must have a `loader` defined and no `type` set.',
"Check your collection definitions in `src/content.config.*`.'",
`Collections that use the Content Layer API must have a \`loader\` defined and no \`type\` set. Check your collection definitions in ${getImporterFilename() ?? 'your content config file'}.`,
);
}
config.type = CONTENT_LAYER_TYPE;
Expand Down
13 changes: 6 additions & 7 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,10 @@ export type ContentPaths = {
};

export function getContentPaths(
{ srcDir }: Pick<AstroConfig, 'root' | 'srcDir'>,
{ srcDir, legacy }: Pick<AstroConfig, 'root' | 'srcDir' | 'legacy'>,
fs: typeof fsMod = fsMod,
): ContentPaths {
const configStats = search(fs, srcDir);
const configStats = search(fs, srcDir, legacy?.collections);
const pkgBase = new URL('../../', import.meta.url);
return {
contentDir: new URL('./content/', srcDir),
Expand All @@ -728,12 +728,11 @@ export function getContentPaths(
config: configStats,
};
}
function search(fs: typeof fsMod, srcDir: URL) {
function search(fs: typeof fsMod, srcDir: URL, legacy?: boolean) {
const paths = [
'content.config.mjs',
'content.config.js',
'content.config.mts',
'content.config.ts',
...(legacy
? []
: ['content.config.mjs', 'content.config.js', 'content.config.mts', 'content.config.ts']),
'content/config.mjs',
'content/config.js',
'content/config.mts',
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,8 @@ export const GenerateContentTypesError = {
title: 'Failed to generate content types.',
message: (errorMessage: string) =>
`\`astro sync\` command failed to generate content collection types: ${errorMessage}`,
hint: 'This error is often caused by a syntax error inside your content, or your content configuration file. Check your `src/content.config.*` file for typos.',
hint: (fileName?: string) =>
`This error is often caused by a syntax error inside your content, or your content configuration file. Check your ${fileName ?? 'content config'} file for typos.`,
} satisfies ErrorData;
/**
* @docs
Expand Down
15 changes: 14 additions & 1 deletion packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,20 @@ async function syncContentCollections(
if (isAstroError(e)) {
throw e;
}
const hint = AstroUserError.is(e) ? e.hint : AstroErrorData.GenerateContentTypesError.hint;
let configFile
try {
const contentPaths = getContentPaths(settings.config, fs);
if(contentPaths.config.exists) {
const matches = /\/(src\/.+)/.exec(contentPaths.config.url.href);
if (matches) {
configFile = matches[1]
}
}
} catch {
// ignore
}

const hint = AstroUserError.is(e) ? e.hint : AstroErrorData.GenerateContentTypesError.hint(configFile);
throw new AstroError(
{
...AstroErrorData.GenerateContentTypesError,
Expand Down

0 comments on commit 893accf

Please sign in to comment.