Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit af5c18e

Browse files
committed
Merge branch 'next' into bugfix/201-cli-shows-not-logged-in-after-successful-login-to-us-region
2 parents 2dd5889 + caa1985 commit af5c18e

2 files changed

Lines changed: 10 additions & 58 deletions

File tree

src/commands/types/generate/actions.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,10 @@ describe('generateStoryblokTypes', () => {
611611
expect(readFileSync).toHaveBeenCalledWith('/mocked/path', 'utf-8');
612612

613613
// Verify that saveToFile was called with the correct parameters
614-
expect(saveToFile).toHaveBeenCalledWith('/mocked/joined/path', expect.stringContaining('// This file was generated by the storyblok CLI.'));
615-
expect(saveToFile).toHaveBeenCalledWith('/mocked/joined/path', expect.stringContaining('export type StoryblokPropertyType'));
616-
expect(saveToFile).toHaveBeenCalledWith('/mocked/joined/path', expect.stringContaining('export interface StoryblokText'));
614+
expect(saveToFile).toHaveBeenCalledWith(
615+
'/mocked/joined/path',
616+
expect.stringMatching(/^\/\/ This file was generated by the Storyblok CLI\.\n\/\/ DO NOT MODIFY THIS FILE BY HAND\.\n\n.*Storyblok.*/),
617+
);
617618
});
618619

619620
it('should generate Storyblok types with custom filename', async () => {
@@ -647,7 +648,7 @@ describe('generateStoryblokTypes', () => {
647648
const savedContent = vi.mocked(saveToFile).mock.calls[0][1];
648649

649650
// Verify that all expected type definitions are included
650-
expect(savedContent).toContain('export type StoryblokPropertyType');
651+
expect(savedContent).toContain('export type StoryblokPropertyType = \'text\' | \'textarea\' | \'number\' | \'boolean\' | \'multilink\' | \'bloks\' | \'custom\';');
651652
expect(savedContent).toContain('export interface StoryblokText');
652653
expect(savedContent).toContain('export interface StoryblokTextarea');
653654
expect(savedContent).toContain('export interface StoryblokNumber');

src/commands/types/generate/actions.ts

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { compile, type JSONSchema } from 'json-schema-to-typescript';
22
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';
44
import type { GenerateTypesOptions } from './constants';
55
import type { StoryblokPropertyType } from '../../../types/storyblok';
66
import { storyblokSchemas } from '../../../utils/storyblok-schemas';
@@ -231,7 +231,7 @@ export const generateTypes = async (
231231
// Get the component type name with proper handling of numbers at the start
232232
const type = getComponentType(component.name, options);
233233
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(
235235
(acc, [key, value]) => {
236236
if (value.required) {
237237
return [...acc, key];
@@ -332,65 +332,16 @@ export const generateStoryblokTypes = async (options: SaveTypesOptions = {}) =>
332332

333333
try {
334334
// Get the path to the storyblok.ts file
335-
const storyblokTypesPath = resolve(process.cwd(), 'src', 'types', 'storyblok.ts');
336335

336+
const storyblokTypesPath = resolve(__dirname, './index.d.ts');
337337
// Read the content of the storyblok.ts file
338338
const storyblokTypesContent = readFileSync(storyblokTypesPath, 'utf-8');
339339

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-
388340
// Define the content of the d.ts file
389341
const typeDefs = [
390-
'// This file was generated by the storyblok CLI.',
342+
'// This file was generated by the Storyblok CLI.',
391343
'// DO NOT MODIFY THIS FILE BY HAND.',
392-
'',
393-
...typeDefinitions,
344+
storyblokTypesContent,
394345
].join('\n');
395346

396347
// Determine the path to save the file

0 commit comments

Comments
 (0)