|
1 | 1 | import { compile, type JSONSchema } from 'json-schema-to-typescript'; |
2 | 2 | import type { SpaceComponent, SpaceData } from '../../../commands/components/constants'; |
3 | | -import { handleError, handleFileSystemError, toCamelCase, toPascalCase } from '../../../utils'; |
| 3 | +import { __dirname, handleError, handleFileSystemError, toCamelCase, toPascalCase } from '../../../utils'; |
4 | 4 | import type { GenerateTypesOptions } from './constants'; |
5 | 5 | import type { StoryblokPropertyType } from '../../../types/storyblok'; |
6 | 6 | import { storyblokSchemas } from '../../../utils/storyblok-schemas'; |
@@ -231,7 +231,7 @@ export const generateTypes = async ( |
231 | 231 | // Get the component type name with proper handling of numbers at the start |
232 | 232 | const type = getComponentType(component.name, options); |
233 | 233 | const componentPropertiesTypeAnnotations = await getComponentPropertiesTypeAnnotations(component, options, spaceData, customFieldsParser); |
234 | | - const requiredFields = Object.entries<Record<string, any>>(component.schema).reduce( |
| 234 | + const requiredFields = Object.entries<Record<string, any>>(component?.schema || {}).reduce( |
235 | 235 | (acc, [key, value]) => { |
236 | 236 | if (value.required) { |
237 | 237 | return [...acc, key]; |
@@ -332,65 +332,16 @@ export const generateStoryblokTypes = async (options: SaveTypesOptions = {}) => |
332 | 332 |
|
333 | 333 | try { |
334 | 334 | // Get the path to the storyblok.ts file |
335 | | - const storyblokTypesPath = resolve(process.cwd(), 'src', 'types', 'storyblok.ts'); |
336 | 335 |
|
| 336 | + const storyblokTypesPath = resolve(__dirname, './index.d.ts'); |
337 | 337 | // Read the content of the storyblok.ts file |
338 | 338 | const storyblokTypesContent = readFileSync(storyblokTypesPath, 'utf-8'); |
339 | 339 |
|
340 | | - // Extract the type definitions using a more robust approach |
341 | | - const lines = storyblokTypesContent.split('\n'); |
342 | | - const typeDefinitions: string[] = []; |
343 | | - let isCollecting = false; |
344 | | - let bracketCount = 0; |
345 | | - let currentType = ''; |
346 | | - |
347 | | - for (let i = 0; i < lines.length; i++) { |
348 | | - const line = lines[i]; |
349 | | - |
350 | | - // Check if this line starts a type definition |
351 | | - if (line.includes('export type StoryblokPropertyType') |
352 | | - || line.includes('export interface Storyblok')) { |
353 | | - // If we were already collecting a type, add it to our results |
354 | | - if (isCollecting) { |
355 | | - typeDefinitions.push(''); |
356 | | - } |
357 | | - |
358 | | - isCollecting = true; |
359 | | - typeDefinitions.push(line); |
360 | | - currentType = line.includes('type') ? 'type' : 'interface'; |
361 | | - |
362 | | - // Count opening and closing braces to handle nested structures |
363 | | - bracketCount += (line.match(/\{/g) || []).length; |
364 | | - bracketCount -= (line.match(/\}/g) || []).length; |
365 | | - |
366 | | - // For types, we don't need to collect more lines |
367 | | - if (currentType === 'type') { |
368 | | - isCollecting = false; |
369 | | - continue; |
370 | | - } |
371 | | - |
372 | | - // For interfaces, continue collecting lines until we've matched all braces |
373 | | - let j = i + 1; |
374 | | - while (j < lines.length && bracketCount > 0) { |
375 | | - const nextLine = lines[j]; |
376 | | - bracketCount += (nextLine.match(/\{/g) || []).length; |
377 | | - bracketCount -= (nextLine.match(/\}/g) || []).length; |
378 | | - typeDefinitions.push(nextLine); |
379 | | - j++; |
380 | | - } |
381 | | - |
382 | | - // Skip the lines we've already processed |
383 | | - i = j - 1; |
384 | | - isCollecting = false; |
385 | | - } |
386 | | - } |
387 | | - |
388 | 340 | // Define the content of the d.ts file |
389 | 341 | const typeDefs = [ |
390 | | - '// This file was generated by the storyblok CLI.', |
| 342 | + '// This file was generated by the Storyblok CLI.', |
391 | 343 | '// DO NOT MODIFY THIS FILE BY HAND.', |
392 | | - '', |
393 | | - ...typeDefinitions, |
| 344 | + storyblokTypesContent, |
394 | 345 | ].join('\n'); |
395 | 346 |
|
396 | 347 | // Determine the path to save the file |
|
0 commit comments