11/** generate completion data from nitropack and Nuxt starter repo */
22
3+ import { existsSync } from 'node:fs'
34import { mkdir , writeFile } from 'node:fs/promises'
45import { dirname , join } from 'node:path'
56import process from 'node:process'
67import { pathToFileURL } from 'node:url'
78import { resolveModulePath } from 'exsolve'
9+ import { isCI } from 'std-env'
810
911import { fetchTemplates } from '../packages/nuxt-cli/src/utils/starter-templates.ts'
1012
@@ -14,21 +16,32 @@ interface PresetMeta {
1416
1517const dataDir = new URL ( '../packages/nuxt-cli/src/data/' , import . meta. url )
1618
19+ const templatesFile = new URL ( 'templates.ts' , dataDir )
20+
1721export async function generateCompletionData ( ) {
1822 const [ nitroPresets , templates ] = await Promise . all ( [
1923 getNitroPresets ( ) ,
20- fetchTemplates ( ) ,
24+ fetchTemplates ( ) . catch ( keepExistingTemplates ) ,
2125 ] )
2226
2327 await mkdir ( dataDir , { recursive : true } )
2428 await writeFile (
2529 new URL ( 'nitro-presets.ts' , dataDir ) ,
2630 `export const nitroPresets = ${ JSON . stringify ( nitroPresets , null , 2 ) } as const` ,
2731 )
28- await writeFile (
29- new URL ( 'templates.ts' , dataDir ) ,
30- `export const templates = ${ JSON . stringify ( templates , null , 2 ) } as const` ,
31- )
32+ if ( templates ) {
33+ await writeFile (
34+ templatesFile ,
35+ `export const templates = ${ JSON . stringify ( templates , null , 2 ) } as const` ,
36+ )
37+ }
38+ }
39+
40+ function keepExistingTemplates ( error : unknown ) : undefined {
41+ if ( isCI || ! existsSync ( templatesFile ) ) {
42+ throw error
43+ }
44+ console . warn ( `Could not fetch starter templates, keeping the ones already generated: ${ error } ` )
3245}
3346
3447async function getNitroPresets ( ) {
0 commit comments