From f217cb29b603593e0dfef300fac668aa5f0141a5 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 5 Sep 2024 15:54:48 +0200 Subject: [PATCH] feat(create-astro): ts-check comment --- .changeset/nasty-dogs-sort.md | 5 +++++ packages/create-astro/src/actions/typescript.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .changeset/nasty-dogs-sort.md diff --git a/.changeset/nasty-dogs-sort.md b/.changeset/nasty-dogs-sort.md new file mode 100644 index 000000000000..225012a70036 --- /dev/null +++ b/.changeset/nasty-dogs-sort.md @@ -0,0 +1,5 @@ +--- +'create-astro': patch +--- + +Updates the default Astro config with `// @ts-check` if the Typescript preset is `strict` or `strictest` diff --git a/packages/create-astro/src/actions/typescript.ts b/packages/create-astro/src/actions/typescript.ts index c0034fe4f83a..169aef7089fe 100644 --- a/packages/create-astro/src/actions/typescript.ts +++ b/packages/create-astro/src/actions/typescript.ts @@ -140,6 +140,21 @@ const FILES_TO_UPDATE = { } } }, + 'astro.config.mjs': async (file: string, options: { value: string }) => { + if (!(options.value === 'strict' || options.value === 'strictest')) { + return; + } + + try { + let data = await readFile(file, { encoding: 'utf-8' }); + data = `// @ts-check\n${data}`; + await writeFile(file, data, { encoding: 'utf-8' }); + } catch (err) { + // if there's no astro.config.mjs (which is very unlikely), then do nothing + if (err && (err as any).code === 'ENOENT') return; + if (err instanceof Error) throw new Error(err.message); + } + }, }; export async function setupTypeScript(value: string, ctx: PickedTypeScriptContext) {