generated from florian-lefebvre/astro-integration-template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathadd-dts.ts
134 lines (122 loc) · 3.56 KB
/
add-dts.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, relative } from "node:path";
import { fileURLToPath } from "node:url";
import type { AstroIntegrationLogger } from "astro";
import { parse, prettyPrint } from "recast";
import typescriptParser from "recast/parsers/typescript.js";
import { defineUtility } from "../core/define-utility.js";
const injectEnvDTS = ({
srcDir,
logger,
specifier,
}: {
srcDir: URL;
logger: AstroIntegrationLogger;
specifier: URL | string;
}) => {
const envDTsPath = fileURLToPath(new URL("env.d.ts", srcDir));
if (specifier instanceof URL) {
specifier = fileURLToPath(specifier);
specifier = relative(fileURLToPath(srcDir), specifier);
specifier = specifier.replaceAll("\\", "/");
}
const envDTsContents = readFileSync(envDTsPath, "utf8");
if (
envDTsContents.includes(`/// <reference types='${specifier}' />`) ||
envDTsContents.includes(`/// <reference types="${specifier}" />`)
) {
return;
}
const data: { singleQuotes: boolean; hasClient: boolean } =
envDTsContents.includes(`/// <reference types='astro/client' />`)
? {
singleQuotes: true,
hasClient: true,
}
: envDTsContents.includes(`/// <reference types="astro/client" />`)
? { singleQuotes: false, hasClient: true }
: envDTsContents.includes(
`/// <reference path="../.astro/types.d.ts" />`,
)
? {
singleQuotes: false,
hasClient: false,
}
: {
singleQuotes: true,
hasClient: false,
};
const referenceToReplace = `/// <reference ${
data.hasClient ? "types" : "path"
}=${data.singleQuotes ? `'` : `"`}${
data.hasClient ? "astro/client" : "../.astro/types.d.ts"
}${data.singleQuotes ? `'` : `"`} />`;
const newEnvDTsContents = envDTsContents.replace(
referenceToReplace,
`${referenceToReplace}\n/// <reference types=${
data.singleQuotes ? `'` : `"`
}${specifier}${data.singleQuotes ? `'` : `"`} />`,
);
// the odd case where the user changed the reference to astro/client
if (newEnvDTsContents === envDTsContents) {
return;
}
writeFileSync(envDTsPath, newEnvDTsContents);
logger.info("Updated env.d.ts types");
};
/**
* @deprecated
* This utility will be removed in a future minor release. Bump your Astro peer dependency to ^4.14.0
* and use [injectTypes](https://docs.astro.build/en/reference/integrations-reference/#injecttypes-options).
*
* @description
* Allows to inject .d.ts file in users project. It will create a file inside `.astro`
* and reference it from `src/env.d.ts`.
*
* @param {import("astro").HookParameters<"astro:config:setup">} params
* @param {object} options
* @param {string} options.name - The name of the .d.ts file. Eg `test` will generate `.astro/test.d.ts`
* @param {string} options.content
*
* @example
* ```ts
* addDts(params, {
* name: "my-integration",
* content: `declare module "virtual:my-integration" {}`,
* })
* ```
*
* @see https://astro-integration-kit.netlify.app/utilities/add-dts/
*/
export const addDts = defineUtility("astro:config:setup")(
(
{ config: { root, srcDir }, logger },
{
name,
content,
}: {
name: string;
content: string;
},
) => {
const dtsURL = new URL(`.astro/${name}.d.ts`, root);
const filePath = fileURLToPath(dtsURL);
injectEnvDTS({
srcDir,
logger,
specifier: dtsURL,
});
mkdirSync(dirname(filePath), { recursive: true });
writeFileSync(
filePath,
// TODO: extract to helper to use with core injectTypes
prettyPrint(
parse(content, {
parser: typescriptParser,
}),
{ tabWidth: 4 },
).code,
"utf-8",
);
},
);