Home / Function/ createPreferences() — astro Function Reference

createPreferences() — astro Function Reference

Architecture documentation for the createPreferences() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e5de314d_a01f_f2cf_1a35_e440abb1adca["createPreferences()"]
  942f8bed_424f_0805_6c0b_154ed0869816["index.ts"]
  e5de314d_a01f_f2cf_1a35_e440abb1adca -->|defined in| 942f8bed_424f_0805_6c0b_154ed0869816
  184aa0ef_9217_6c97_b371_4e61ea2042b7["getGlobalPreferenceDir()"]
  e5de314d_a01f_f2cf_1a35_e440abb1adca -->|calls| 184aa0ef_9217_6c97_b371_4e61ea2042b7
  style e5de314d_a01f_f2cf_1a35_e440abb1adca fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/preferences/index.ts lines 83–132

export default function createPreferences(
	config: Record<string, any>,
	dotAstroDir: URL,
): AstroPreferences {
	const global = new PreferenceStore(getGlobalPreferenceDir());
	const project = new PreferenceStore(fileURLToPath(dotAstroDir));
	const stores: Record<PreferenceLocation, PreferenceStore> = { global, project };

	return {
		async get(key, { location } = {}) {
			if (!location) return project.get(key) ?? global.get(key) ?? dget(DEFAULT_PREFERENCES, key);
			return stores[location].get(key);
		},
		async set(key, value, { location = 'project', reloadServer = true } = {}) {
			stores[location].set(key, value);

			if (!reloadServer) {
				this.ignoreNextPreferenceReload = true;
			}
		},
		async getAll() {
			const allPrefs = Object.assign(
				{},
				DEFAULT_PREFERENCES,
				stores['global'].getAll(),
				stores['project'].getAll(),
			);

			const { _variables, ...prefs } = allPrefs;

			return prefs;
		},
		async list() {
			const { _variables, ...defaultPrefs } = DEFAULT_PREFERENCES;
			return {
				global: stores['global'].getAll(),
				project: stores['project'].getAll(),
				fromAstroConfig: mapFrom(DEFAULT_PREFERENCES, config),
				defaults: defaultPrefs,
			};

			function mapFrom(defaults: Preferences, astroConfig: Record<string, any>) {
				return Object.fromEntries(
					Object.entries(defaults).map(([key, _]) => [key, astroConfig[key]]),
				);
			}
		},
		ignoreNextPreferenceReload: false,
	};
}

Domain

Subdomains

Frequently Asked Questions

What does createPreferences() do?
createPreferences() is a function in the astro codebase, defined in packages/astro/src/preferences/index.ts.
Where is createPreferences() defined?
createPreferences() is defined in packages/astro/src/preferences/index.ts at line 83.
What does createPreferences() call?
createPreferences() calls 1 function(s): getGlobalPreferenceDir.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free