Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(next): codegenDir and update .astro paths #11963

Open
wants to merge 18 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/lazy-oranges-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'astro': major
---

Updates where files are generated by `astro sync`.

To avoid collisions with files generated by integrations, any file generated by Astro in `/.astro/` is now moved to `/.astro/astro/`. It should only break usage of JSON Schemas for legacy data collections:

```diff
{
- "$schema": "../../../.astro/collections/authors.schema.json",
+ "$schema": "../../../.astro/astro/collections/authors.schema.json",
"name": "Armand",
"skills": ["Astro", "Starlight"]
}
```
6 changes: 4 additions & 2 deletions packages/astro/src/content/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const CONTENT_FLAGS = [
export const CONTENT_TYPES_FILE = 'astro/content.d.ts';

export const DATA_STORE_FILE = 'data-store.json';
export const ASSET_IMPORTS_FILE = 'assets.mjs';
export const MODULES_IMPORTS_FILE = 'modules.mjs';
export const ASSET_IMPORTS_FILE = 'astro/content-assets.mjs';
export const MODULES_IMPORTS_FILE = 'astro/content-modules.mjs';
export const COLLECTIONS_MANIFEST_FILE = 'astro/collections/collections.json';
export const COLLECTIONS_DIR = 'astro/collections/'

export const CONTENT_LAYER_TYPE = 'content_layer';
19 changes: 11 additions & 8 deletions packages/astro/src/content/content-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { AstroSettings } from '../types/astro.js';
import type { ContentEntryType, RefreshContentOptions } from '../types/public/content.js';
import {
ASSET_IMPORTS_FILE,
COLLECTIONS_MANIFEST_FILE,
CONTENT_LAYER_TYPE,
DATA_STORE_FILE,
MODULES_IMPORTS_FILE,
Expand All @@ -18,6 +19,8 @@ import {
getEntryDataAndImages,
globalContentConfigObserver,
} from './utils.js';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

export interface ContentLayerOptions {
store: MutableDataStore;
Expand Down Expand Up @@ -213,15 +216,12 @@ export class ContentLayer {
return collection.loader.load(context);
}),
);
if (!existsSync(this.#settings.config.cacheDir)) {
await fs.mkdir(this.#settings.config.cacheDir, { recursive: true });
}
await fs.mkdir(this.#settings.config.cacheDir, { recursive: true });
const cacheFile = getDataStoreFile(this.#settings);
await this.#store.writeToDisk(cacheFile);
if (!existsSync(this.#settings.dotAstroDir)) {
await fs.mkdir(this.#settings.dotAstroDir, { recursive: true });
}
const assetImportsFile = new URL(ASSET_IMPORTS_FILE, this.#settings.dotAstroDir);
const dotAstroAstroDir = dirname(fileURLToPath(assetImportsFile));
await fs.mkdir(dotAstroAstroDir, { recursive: true });
await this.#store.writeAssetImports(assetImportsFile);
const modulesImportsFile = new URL(MODULES_IMPORTS_FILE, this.#settings.dotAstroDir);
await this.#store.writeModuleImports(modulesImportsFile);
Expand All @@ -232,7 +232,7 @@ export class ContentLayer {
}

async regenerateCollectionFileManifest() {
const collectionsManifest = new URL('collections/collections.json', this.#settings.dotAstroDir);
const collectionsManifest = new URL(COLLECTIONS_MANIFEST_FILE, this.#settings.dotAstroDir);
this.#logger.debug('content', 'Regenerating collection file manifest');
if (existsSync(collectionsManifest)) {
try {
Expand Down Expand Up @@ -283,7 +283,10 @@ export async function simpleLoader<TData extends { id: string }>(
*/
export function getDataStoreFile(settings: AstroSettings, isDev?: boolean) {
isDev ??= process?.env.NODE_ENV === 'development';
return new URL(DATA_STORE_FILE, isDev ? settings.dotAstroDir : settings.config.cacheDir);
return new URL(
DATA_STORE_FILE,
isDev ? new URL('./astro/', settings.dotAstroDir) : settings.config.cacheDir,
);
}

function contentLayerSingleton() {
Expand Down
17 changes: 9 additions & 8 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import type { Logger } from '../core/logger/core.js';
import { isRelativePath } from '../core/path.js';
import type { AstroSettings } from '../types/astro.js';
import type { ContentEntryType } from '../types/public/content.js';
import { CONTENT_LAYER_TYPE, CONTENT_TYPES_FILE, VIRTUAL_MODULE_ID } from './consts.js';
import {
COLLECTIONS_DIR,
CONTENT_LAYER_TYPE,
CONTENT_TYPES_FILE,
VIRTUAL_MODULE_ID,
} from './consts.js';
import {
type CollectionConfig,
type ContentConfig,
Expand Down Expand Up @@ -428,10 +433,8 @@ async function writeContentFiles({
let contentTypesStr = '';
let dataTypesStr = '';

const collectionSchemasDir = new URL('./collections/', settings.dotAstroDir);
if (!fs.existsSync(collectionSchemasDir)) {
fs.mkdirSync(collectionSchemasDir, { recursive: true });
}
const collectionSchemasDir = new URL(COLLECTIONS_DIR, settings.dotAstroDir);
fs.mkdirSync(collectionSchemasDir, { recursive: true });

for (const [collection, config] of Object.entries(contentConfig?.collections ?? {})) {
collectionEntryMap[JSON.stringify(collection)] ??= {
Expand Down Expand Up @@ -568,9 +571,7 @@ async function writeContentFiles({
);
}

if (!fs.existsSync(settings.dotAstroDir)) {
fs.mkdirSync(settings.dotAstroDir, { recursive: true });
}
fs.mkdirSync(settings.dotAstroDir, { recursive: true });

const configPathRelativeToCacheDir = normalizeConfigPath(
new URL('astro', settings.dotAstroDir).pathname,
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/dev/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { createSafeError } from '../errors/index.js';
import { formatErrorMessage } from '../messages.js';
import type { Container } from './container.js';
import { createContainer, startContainer } from './container.js';
import { SETTINGS_FILE } from '../../preferences/constants.js';

async function createRestartedContainer(
container: Container,
Expand Down Expand Up @@ -50,7 +51,7 @@ function shouldRestartContainer(
else {
shouldRestart = configRE.test(normalizedChangedFile);
const settingsPath = vite.normalizePath(
fileURLToPath(new URL('settings.json', settings.dotAstroDir)),
fileURLToPath(new URL(SETTINGS_FILE, settings.dotAstroDir)),
);
if (settingsPath.endsWith(normalizedChangedFile)) {
shouldRestart = settings.preferences.ignoreNextPreferenceReload ? false : true;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/preferences/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SETTINGS_FILE = 'astro/settings.json';
3 changes: 2 additions & 1 deletion packages/astro/src/preferences/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import fs from 'node:fs';
import path from 'node:path';
import dget from 'dlv';
import { dset } from 'dset';
import { SETTINGS_FILE } from './constants.js';

export class PreferenceStore {
private file: string;

constructor(
private dir: string,
filename = 'settings.json',
filename = SETTINGS_FILE,
) {
this.file = path.join(this.dir, filename);
}
Expand Down
Loading