summaryrefslogtreecommitdiff
path: root/packages/astro/src/vite-plugin-inject-env-ts/index.ts
blob: 0f0fbb86d01ab13fff83801c97ae78e7f41f43e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { bold } from 'kleur/colors';
import type fsMod from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { normalizePath, type Plugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import { getContentPaths, getDotAstroTypeReference } from '../content/index.js';
import { info, type LogOptions } from '../core/logger/core.js';

export function getEnvTsPath({ srcDir }: { srcDir: URL }) {
	return new URL('env.d.ts', srcDir);
}

export function astroInjectEnvTsPlugin({
	settings,
	logging,
	fs,
}: {
	settings: AstroSettings;
	logging: LogOptions;
	fs: typeof fsMod;
}): Plugin {
	return {
		name: 'astro-inject-env-ts',
		// Use `post` to ensure project setup is complete
		// Ex. `.astro` types have been written
		enforce: 'post',
		async config() {
			await setUpEnvTs({ settings, logging, fs });
		},
	};
}

export async function setUpEnvTs({
	settings,
	logging,
	fs,
}: {
	settings: AstroSettings;
	logging: LogOptions;
	fs: typeof fsMod;
}) {
	const envTsPath = getEnvTsPath(settings.config);
	const dotAstroDir = getContentPaths(settings.config).cacheDir;
	const dotAstroTypeReference = getDotAstroTypeReference(settings.config);
	const envTsPathRelativetoRoot = normalizePath(
		path.relative(fileURLToPath(settings.config.root), fileURLToPath(envTsPath))
	);

	if (fs.existsSync(envTsPath)) {
		let typesEnvContents = await fs.promises.readFile(envTsPath, 'utf-8');

		if (!fs.existsSync(dotAstroDir))
			// Add `.astro` types reference if none exists
			return;
		const expectedTypeReference = getDotAstroTypeReference(settings.config);

		if (!typesEnvContents.includes(expectedTypeReference)) {
			typesEnvContents = `${expectedTypeReference}\n${typesEnvContents}`;
			await fs.promises.writeFile(envTsPath, typesEnvContents, 'utf-8');
			info(logging, 'content', `Added ${bold(envTsPathRelativetoRoot)} types`);
		}
	} else {
		// Otherwise, inject the `env.d.ts` file
		let referenceDefs: string[] = [];
		referenceDefs.push('/// <reference types="astro/client" />');

		if (fs.existsSync(dotAstroDir)) {
			referenceDefs.push(dotAstroTypeReference);
		}

		await fs.promises.mkdir(settings.config.srcDir, { recursive: true });
		await fs.promises.writeFile(envTsPath, referenceDefs.join('\n'), 'utf-8');
		info(logging, 'astro', `Added ${bold(envTsPathRelativetoRoot)} types`);
	}
}
'>+12 2022-02-01[ci] release (next) (#2492)astro@0.23.0-next.0@astrojs/test-static-build-pkg@0.0.2@astrojs/markdown-remark@0.6.1-next.0Gravatar github-actions[bot] 31-43/+93 2022-02-01[ci] collect statsGravatar FredKSchott 1-0/+1 2022-01-31update congratsbot format againGravatar Fred K. Schott 1-1/+1 2022-01-31update congratsbot againGravatar Fred K. Schott 1-1/+1 2022-01-31Remove SVG animation on GitHub/NPM (#2512)Gravatar Nate Moore 1-21/+0 2022-01-31[ci] yarn formatGravatar natemoo-re 2-4/+6 2022-01-31Add Shiki as an alternative to Prism (#2497)Gravatar Juan Martín Seery 26-9/+356 2022-01-31Deprecate unescaped HTML inside of expressions (#2489)Gravatar Nate Moore 9-31/+74