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
|
import { existsSync } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { AstroIntegrationLogger } from 'astro';
import { bold } from 'kleur/colors';
import { normalizePath } from 'vite';
import { DB_TYPES_FILE } from '../consts.js';
import type { VitePlugin } from '../utils.js';
export function vitePluginInjectEnvTs(
{ srcDir, root }: { srcDir: URL; root: URL },
logger: AstroIntegrationLogger,
): VitePlugin {
return {
name: 'db-inject-env-ts',
enforce: 'post',
async config() {
await setUpEnvTs({ srcDir, root, logger });
},
};
}
export async function setUpEnvTs({
srcDir,
root,
logger,
}: {
srcDir: URL;
root: URL;
logger: AstroIntegrationLogger;
}) {
const envTsPath = getEnvTsPath({ srcDir });
const envTsPathRelativetoRoot = normalizePath(
path.relative(fileURLToPath(root), fileURLToPath(envTsPath)),
);
if (existsSync(envTsPath)) {
let typesEnvContents = await readFile(envTsPath, 'utf-8');
const dotAstroDir = new URL('.astro/', root);
if (!existsSync(dotAstroDir)) return;
const dbTypeReference = getDBTypeReference({ srcDir, dotAstroDir });
if (!typesEnvContents.includes(dbTypeReference)) {
typesEnvContents = `${dbTypeReference}\n${typesEnvContents}`;
await writeFile(envTsPath, typesEnvContents, 'utf-8');
logger.info(`Added ${bold(envTsPathRelativetoRoot)} types`);
}
}
}
function getDBTypeReference({ srcDir, dotAstroDir }: { srcDir: URL; dotAstroDir: URL }) {
const dbTypesFile = new URL(DB_TYPES_FILE, dotAstroDir);
const contentTypesRelativeToSrcDir = normalizePath(
path.relative(fileURLToPath(srcDir), fileURLToPath(dbTypesFile)),
);
return `/// <reference path=${JSON.stringify(contentTypesRelativeToSrcDir)} />`;
}
function getEnvTsPath({ srcDir }: { srcDir: URL }) {
return new URL('env.d.ts', srcDir);
}
|