Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/storyblok/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, writeFileSync } from 'node:fs';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { default as path } from 'node:path';
import { fileURLToPath } from 'node:url';

Expand Down Expand Up @@ -58,9 +58,11 @@ async function convertDsAgency(): Promise<void> {
}
});

const existingConfig = JSON.parse(readFileSync(`resources/config.json`, 'utf-8'));

mkdirSync('dist/agency', { recursive: true });

const configString = configuration(convertedObjects);
const configString = configuration(convertedObjects, existingConfig);
writeFileSync(`dist/agency/components.123456.json`, configString);
}

Expand Down
1 change: 1 addition & 0 deletions tools/jsonschema2storyblok/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@kickstartds/jsonschema-utils": "workspace:*",
"ajv": "^8.12.0",
"es-toolkit": "^1.26.1",
"object-traversal": "^1.0.1",
"uuid": "~9.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions tools/jsonschema2storyblok/src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Storyblok types stem from here: https://github.com/dohomi/storyblok-generate-ts/blob/master/src/typings.ts
// Related Storyblok docs:
// - https://www.storyblok.com/docs/api/management#core-resources/components/components
// - https://www.storyblok.com/docs/api/management/core-resources/components/the-component-schema-field-object
// - https://www.storyblok.com/docs/schema-configuration

export type GenericType =
Expand Down
45 changes: 39 additions & 6 deletions tools/jsonschema2storyblok/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
IConvertParams,
getSchemaName
} from '@kickstartds/jsonschema-utils';
import { mergeWith } from 'es-toolkit';
import { type JSONSchema, TypeName } from 'json-schema-typed/draft-07';
import { traverse } from 'object-traversal';
import { v4 as uuidv4 } from 'uuid';

import { GenericType, ITypeMapping, IStoryblokSchemaElement, IStoryblokBlock } from './@types/index.js';
Expand Down Expand Up @@ -87,13 +89,44 @@ export function configuration(
components: [],
templates: [],
globals: []
}
},
existingConfig?: { components: IStoryblokBlock[] }
): string {
return JSON.stringify(
{ components: [...converted.components, ...converted.templates, ...converted.globals] },
null,
2
);
const config = { components: [...converted.components, ...converted.templates, ...converted.globals] };

if (existingConfig) {
traverse(existingConfig, ({ key, parent }) => {
if (key?.startsWith('tab-') && parent) {
delete parent[key];
}
});

for (const component of config.components) {
const existingComponent = existingConfig.components.find((c) => c.name === component.name);
if (existingComponent) {
mergeWith(existingComponent, component, (targetValue, sourceValue, key) => {
if (['id', '_uid', 'pos', 'component_group_uuid', 'image', 'created_at'].includes(key))
return targetValue ?? sourceValue;

if (key === 'options' && Array.isArray(targetValue) && Array.isArray(sourceValue)) {
return sourceValue.map((option) => {
const existingOption = targetValue.find((o) => o.name === option.name);
if (existingOption) {
return { ...option, _uid: existingOption._uid };
}
return option;
});
}
});
} else {
existingConfig.components.push(component);
}
}

return JSON.stringify(existingConfig, null, 2);
}

return JSON.stringify(config, null, 2);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tools/jsonschema2storyblok/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./node_modules/@rushstack/heft-node-rig/profiles/default/tsconfig-base.json",
"compilerOptions": {
"lib": ["es2021"],
"lib": ["es2021", "dom"],
"target": "es2021",
"module": "node16",
"moduleResolution": "node16",
Expand Down
Loading