@@ -17,14 +17,26 @@ import {
1717} from "./wrangler/config" ;
1818import type { C3Context , PackageJson } from "types" ;
1919
20+ export async function addTypes ( ctx : C3Context ) {
21+ if ( ! usesTypescript ( ctx ) || ctx . template . workersTypes === "none" ) {
22+ return ;
23+ }
24+ const { npm } = detectPackageManager ( ) ;
25+
26+ if ( ctx . template . workersTypes === "installed" ) {
27+ await installWorkersTypes ( npm ) ;
28+ } else if ( ctx . template . workersTypes === "generated" ) {
29+ await generateWorkersTypes ( ctx , npm ) ;
30+ }
31+ const usesNodeCompat = await maybeInstallNodeTypes ( ctx , npm ) ;
32+ await updateTsConfig ( ctx , { usesNodeCompat } ) ;
33+ }
34+
2035/**
2136 * Generate types using the `cf-typegen` script and update tsconfig
2237 */
2338
24- export async function generateWorkersTypes ( ctx : C3Context ) {
25- if ( ! usesTypescript ( ctx ) ) {
26- return ;
27- }
39+ async function generateWorkersTypes ( ctx : C3Context , npm : string ) {
2840 const packageJsonPath = join ( ctx . project . path , "package.json" ) ;
2941 if ( ! existsSync ( packageJsonPath ) ) {
3042 return ;
@@ -34,8 +46,6 @@ export async function generateWorkersTypes(ctx: C3Context) {
3446 return ;
3547 }
3648
37- const { npm } = detectPackageManager ( ) ;
38-
3949 const typesCmd = [ npm , "run" , "cf-typegen" ] ;
4050
4151 await runCommand ( typesCmd , {
@@ -45,12 +55,10 @@ export async function generateWorkersTypes(ctx: C3Context) {
4555 doneText : `${ brandColor ( "generated" ) } ${ dim ( `to \`${ ctx . template . typesPath } \` via \`${ typesCmd . join ( " " ) } \`` ) } ` ,
4656 } ) ;
4757
48- const usesNodeCompat = await maybeInstallNodeTypes ( ctx , npm ) ;
49-
50- delete packageManifest [ "devDependencies" ] ?. [ "@cloudflare/workers-types" ] ;
51-
52- writeFile ( packageJsonPath , JSON . stringify ( packageManifest , null , 2 ) ) ;
53- await updateTsConfig ( ctx , { usesNodeCompat } ) ;
58+ if ( packageManifest [ "devDependencies" ] ?. [ "@cloudflare/workers-types" ] ) {
59+ delete packageManifest [ "devDependencies" ] ?. [ "@cloudflare/workers-types" ] ;
60+ writeFile ( packageJsonPath , JSON . stringify ( packageManifest , null , 2 ) ) ;
61+ }
5462}
5563
5664const maybeInstallNodeTypes = async ( ctx : C3Context , npm : string ) => {
@@ -169,17 +177,10 @@ export async function updateTsConfig(
169177 * Installs the latest version of the `@cloudflare/workers-types` package
170178 * and updates the .tsconfig file to use the latest entrypoint version.
171179 */
172- export async function installWorkersTypes ( ctx : C3Context ) {
173- if ( ! usesTypescript ( ctx ) ) {
174- return ;
175- }
176- const { npm } = detectPackageManager ( ) ;
177-
180+ async function installWorkersTypes ( npm : string ) {
178181 await installPackages ( [ "@cloudflare/workers-types" ] , {
179182 dev : true ,
180183 startText : "Installing @cloudflare/workers-types" ,
181184 doneText : `${ brandColor ( "installed" ) } ${ dim ( `via ${ npm } ` ) } ` ,
182185 } ) ;
183- const usesNodeCompat = await maybeInstallNodeTypes ( ctx , npm ) ;
184- await updateTsConfig ( ctx , { usesNodeCompat } ) ;
185186}
0 commit comments