diff --git a/package.json b/package.json index fc0d479..03f1b26 100644 --- a/package.json +++ b/package.json @@ -101,12 +101,12 @@ "@commitlint/config-conventional": "^16.2.1", "@kickstartds/eslint-config": "^1.2.0", "@kickstartds/style-dictionary": "^4.0.2", - "@kickstartds/jsonschema-utils": "^3.6.1", - "@kickstartds/jsonschema2staticcms": "^2.9.35", - "@kickstartds/jsonschema2stackbit": "^1.7.4", - "@kickstartds/jsonschema2storyblok": "^1.6.5", - "@kickstartds/jsonschema2types": "^1.2.3", - "@kickstartds/jsonschema2uniform": "^1.4.22", + "@kickstartds/jsonschema-utils": "^3.8.1", + "@kickstartds/jsonschema2staticcms": "^2.9.39", + "@kickstartds/jsonschema2stackbit": "^1.7.8", + "@kickstartds/jsonschema2storyblok": "^1.6.9", + "@kickstartds/jsonschema2types": "^1.2.7", + "@kickstartds/jsonschema2uniform": "^1.4.26", "@stackbit/types": "^0.10.14", "@types/byline": "^4.2.33", "@types/dockerode": "^3.3.9", @@ -135,11 +135,11 @@ "peerDependencies": { "@kickstartds/core": "^4.0.2", "@kickstartds/style-dictionary": "^4.0.2", - "@kickstartds/jsonschema-utils": "^3.6.1", - "@kickstartds/jsonschema2staticcms": "^2.9.35", - "@kickstartds/jsonschema2stackbit": "^1.7.4", - "@kickstartds/jsonschema2storyblok": "^1.6.5", - "@kickstartds/jsonschema2types": "^1.2.3", - "@kickstartds/jsonschema2uniform": "^1.4.22" + "@kickstartds/jsonschema-utils": "^3.8.1", + "@kickstartds/jsonschema2staticcms": "^2.9.39", + "@kickstartds/jsonschema2stackbit": "^1.7.8", + "@kickstartds/jsonschema2storyblok": "^1.6.9", + "@kickstartds/jsonschema2types": "^1.2.7", + "@kickstartds/jsonschema2uniform": "^1.4.26" } } diff --git a/src/commands/cms/stackbit.ts b/src/commands/cms/stackbit.ts index dff8939..3401bb4 100644 --- a/src/commands/cms/stackbit.ts +++ b/src/commands/cms/stackbit.ts @@ -7,13 +7,14 @@ const stackbit = new Command('stackbit') chalkTemplate`Generates {bold Stackbit} configuration from your {bold JSON Schema} component definitions` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--configuration-path ', @@ -67,8 +68,8 @@ const stackbit = new Command('stackbit') .action((options) => import('./../../tasks/cms/stackbit-task.js').then((runTask) => { runTask.default( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.configurationPath, options.updateConfig, options.templates, diff --git a/src/commands/cms/staticcms.ts b/src/commands/cms/staticcms.ts index c1d8a13..b4b51d3 100644 --- a/src/commands/cms/staticcms.ts +++ b/src/commands/cms/staticcms.ts @@ -7,13 +7,14 @@ const staticcms = new Command('staticcms') chalkTemplate`Generates {bold Static CMS} configuration from your {bold JSON Schema} component definitions` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--configuration-path ', @@ -69,8 +70,8 @@ const staticcms = new Command('staticcms') .option('--debug', 'show debugging output', false) .action((options) => { runTask( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.configurationPath, options.updateConfig, options.templates, diff --git a/src/commands/cms/storyblok.ts b/src/commands/cms/storyblok.ts index 46f55bb..51d6f3d 100644 --- a/src/commands/cms/storyblok.ts +++ b/src/commands/cms/storyblok.ts @@ -7,13 +7,14 @@ const storyblok = new Command('storyblok') chalkTemplate`Generates {bold Storyblok} configuration from your {bold JSON Schema} component definitions` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--configuration-path ', @@ -66,8 +67,8 @@ const storyblok = new Command('storyblok') .option('--debug', 'show debugging output', false) .action((options) => { runTask( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.configurationPath, options.updateConfig, options.templates, diff --git a/src/commands/cms/uniform.ts b/src/commands/cms/uniform.ts index b9ac45e..6a0e636 100644 --- a/src/commands/cms/uniform.ts +++ b/src/commands/cms/uniform.ts @@ -7,13 +7,14 @@ const uniform = new Command('uniform') chalkTemplate`Generates {bold Uniform} configuration from your {bold JSON Schema} component definitions` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--configuration-path ', @@ -44,8 +45,8 @@ const uniform = new Command('uniform') .option('--debug', 'show debugging output', false) .action((options) => { runTask( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.configurationPath, options.templates, options.globals, diff --git a/src/commands/schema/defaults.ts b/src/commands/schema/defaults.ts index 71401ba..438f769 100644 --- a/src/commands/schema/defaults.ts +++ b/src/commands/schema/defaults.ts @@ -7,13 +7,14 @@ const defaults = new Command('defaults') chalkTemplate`Creates default objects for {bold JSON Schema} component definitions` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--no-default-page-schema', @@ -38,8 +39,8 @@ const defaults = new Command('defaults') .option('--debug', 'show debugging output', false) .action((options) => { runTask( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.defaultPageSchema, options.layerKickstartdsComponents, options.rcOnly, diff --git a/src/commands/schema/dereference.ts b/src/commands/schema/dereference.ts index e3689d8..ed47d00 100644 --- a/src/commands/schema/dereference.ts +++ b/src/commands/schema/dereference.ts @@ -7,13 +7,14 @@ const dereference = new Command('dereference') chalkTemplate`Dereferences {bold JSON Schema} component definitions, (mainly) inlining all $ref references in the process` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--no-default-page-schema', @@ -38,8 +39,8 @@ const dereference = new Command('dereference') .option('--debug', 'show debugging output', false) .action((options) => { runTask( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.defaultPageSchema, options.layerKickstartdsComponents, options.rcOnly, diff --git a/src/commands/schema/layer.ts b/src/commands/schema/layer.ts index d795785..fe8bc51 100644 --- a/src/commands/schema/layer.ts +++ b/src/commands/schema/layer.ts @@ -7,13 +7,14 @@ const types = new Command('layer') chalkTemplate`Layers kickstartDS {bold TypeScript} type definitions using your {bold JSON Schema} component definitions` ) .option( - '--components-path ', - chalkTemplate`relative path from project root to your components directory, default {bold ./src/components}`, - 'src/components' + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--types-path ', @@ -48,8 +49,8 @@ const types = new Command('layer') .option('--debug', 'show debugging output', false) .action((options) => { runTask( - options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.typesPath, options.mergeSchemas, options.defaultPageSchema, diff --git a/src/commands/schema/types.ts b/src/commands/schema/types.ts index a32b09d..f408c77 100644 --- a/src/commands/schema/types.ts +++ b/src/commands/schema/types.ts @@ -12,8 +12,14 @@ const types = new Command('types') 'src/components' ) .option( - '--cms-path ', - chalkTemplate`relative path from project root to your cms specific components directory, default {bold ./src/components}` + '--schema-paths ', + chalkTemplate`paths to your JSON Schema component definitions, default {bold ./src/components}`, + ['src/components'] + ) + .option( + '--layer-order ', + chalkTemplate`order of layers in processing, default {bold ['cms', 'schema']}`, + ['cms', 'schema'] ) .option( '--merge-schemas', @@ -49,7 +55,8 @@ const types = new Command('types') .action((options) => { runTask( options.componentsPath, - options.cmsPath, + options.schemaPaths, + options.layerOrder, options.mergeSchemas, options.defaultPageSchema, options.layerKickstartdsComponents, diff --git a/src/tasks/cms/stackbit-task.ts b/src/tasks/cms/stackbit-task.ts index 17a7d85..88ddfeb 100644 --- a/src/tasks/cms/stackbit-task.ts +++ b/src/tasks/cms/stackbit-task.ts @@ -34,8 +34,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], configurationPath: string = 'src/cms', updateConfig: boolean = true, templates: string[] = ['page', 'blog-post'], @@ -75,16 +75,19 @@ const run = async ( const stackbit = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold stackbit} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.(schema|definitions|interface).json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const elements = await schemaToStackbit( globs, templates, globals, - components + components, + [...layerOrder, 'kickstartds'] ); shell.mkdir('-p', `${shell.pwd()}/${configurationPath}/`); diff --git a/src/tasks/cms/staticcms-task.ts b/src/tasks/cms/staticcms-task.ts index 4d94f7c..778e56f 100644 --- a/src/tasks/cms/staticcms-task.ts +++ b/src/tasks/cms/staticcms-task.ts @@ -35,8 +35,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], configurationPath: string = 'src/cms', updateConfig: boolean = true, templates: string[] = ['page', 'blog-post'], @@ -73,16 +73,19 @@ const run = async ( const staticcms = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold staticcms} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.(schema|definitions|interface).json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const elements = await schemaToStaticcms( globs, templates, globals, - components + components, + [...layerOrder, 'kickstartds'] ); shell.mkdir('-p', `${shell.pwd()}/${configurationPath}/`); diff --git a/src/tasks/cms/storyblok-task.ts b/src/tasks/cms/storyblok-task.ts index d4b7fb6..e445dca 100644 --- a/src/tasks/cms/storyblok-task.ts +++ b/src/tasks/cms/storyblok-task.ts @@ -34,8 +34,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], configurationPath: string = 'src/cms', updateConfig: boolean = true, templates: string[] = ['page', 'blog-post', 'blog-overview', 'settings'], @@ -72,16 +72,19 @@ const run = async ( const storyblok = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold storyblok} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.(schema|definitions|interface).json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const elements = await schemaToStoryblok( globs, templates, globals, - components + components, + [...layerOrder, 'kickstartds'] ); shell.mkdir('-p', `${shell.pwd()}/${configurationPath}/`); diff --git a/src/tasks/cms/uniform-task.ts b/src/tasks/cms/uniform-task.ts index a605265..6a36afb 100644 --- a/src/tasks/cms/uniform-task.ts +++ b/src/tasks/cms/uniform-task.ts @@ -29,8 +29,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], configurationPath: string = 'src/cms', templates: string[] = ['page', 'blog-post', 'blog-overview', 'settings'], globals: string[] = ['header', 'footer', 'seo'], @@ -53,16 +53,21 @@ const run = async ( const uniform = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold uniform} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.(schema|definitions).json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const { components: uniformComponents, templates: uniformTemplates, globals: uniformGlobals, - } = await schemaToUniform(globs, templates, globals); + } = await schemaToUniform(globs, templates, globals, [ + ...layerOrder, + 'kickstartds', + ]); shell.mkdir('-p', `${shell.pwd()}/${configurationPath}/`); diff --git a/src/tasks/schema/defaults-task.ts b/src/tasks/schema/defaults-task.ts index 4f5124d..80765e3 100644 --- a/src/tasks/schema/defaults-task.ts +++ b/src/tasks/schema/defaults-task.ts @@ -30,8 +30,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], defaultPageSchema: boolean = true, layerKickstartdsComponents: boolean = true, rcOnly: boolean, @@ -53,16 +53,19 @@ const run = async ( const defaults = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold defaults} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.schema.json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const customSchemaPaths = await fg(globs); const defaults = await schemaCreateDefaultObjects( globs, defaultPageSchema, - layerKickstartdsComponents + layerKickstartdsComponents, + [...layerOrder, 'kickstartds'] ); logger.info( @@ -77,13 +80,9 @@ const run = async ( (schemaPath) => schemaPath.endsWith( `/${schemaId.split('/').pop()}` || 'NO MATCH' - ) && - (schemaId.startsWith('http://cms.') - ? !schemaPath.includes('node_modules') - : true) + ) && !schemaPath.includes('node_modules') ); - if (!schemaPath) - throw new Error("Couldn't find matching schema path for schema $id"); + if (!schemaPath) return true; const base = basename(schemaPath, '.json'); const dir = dirname(schemaPath); diff --git a/src/tasks/schema/dereference-task.ts b/src/tasks/schema/dereference-task.ts index 08092cf..99f29a5 100644 --- a/src/tasks/schema/dereference-task.ts +++ b/src/tasks/schema/dereference-task.ts @@ -5,6 +5,7 @@ import chalkTemplate from 'chalk-template'; import createTask from '../task.js'; import { StepFunction } from '../../../types/index.js'; import fsExtra from 'fs-extra'; +import { getTopLayerSchemas } from '@kickstartds/jsonschema-utils'; const writeFile = fsExtra.writeFile; @@ -29,8 +30,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], defaultPageSchema: boolean = true, layerKickstartdsComponents: boolean = true, rcOnly: boolean, @@ -52,16 +53,19 @@ const run = async ( const dereference = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold dereference} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.schema.json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const customSchemaPaths = await fg(globs); const dereffed = await schemaDereferenceSchemas( globs, defaultPageSchema, - layerKickstartdsComponents + layerKickstartdsComponents, + [...layerOrder, 'kickstartds'] ); logger.info( @@ -76,13 +80,9 @@ const run = async ( (schemaPath) => schemaPath.endsWith( `/${schemaId.split('/').pop()}` || 'NO MATCH' - ) && - (schemaId.startsWith('http://cms.') - ? !schemaPath.includes('node_modules') - : true) + ) && !schemaPath.includes('node_modules') ); - if (!schemaPath) - throw new Error("Couldn't find matching schema path for schema $id"); + if (!schemaPath) return true; const base = basename(schemaPath, '.json'); const dir = dirname(schemaPath); diff --git a/src/tasks/schema/layer-task.ts b/src/tasks/schema/layer-task.ts index 89f77e2..f7710bb 100644 --- a/src/tasks/schema/layer-task.ts +++ b/src/tasks/schema/layer-task.ts @@ -31,8 +31,8 @@ const { } = taskUtilSchema; const run = async ( - componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], typesPath: string = 'src/types', mergeSchemas: boolean, defaultPageSchema: boolean = true, @@ -56,17 +56,19 @@ const run = async ( const layer = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold layer} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.(schema|definitions).json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } const layeredTypes = await schemaLayerComponentPropTypes( globs, mergeSchemas, defaultPageSchema, - layerKickstartdsComponents + layerKickstartdsComponents, + [...layerOrder, 'kickstartds'] ); shell.mkdir('-p', `${shell.pwd()}/${typesPath}/`); diff --git a/src/tasks/schema/types-task.ts b/src/tasks/schema/types-task.ts index 8835813..97aee27 100644 --- a/src/tasks/schema/types-task.ts +++ b/src/tasks/schema/types-task.ts @@ -31,7 +31,8 @@ const { const run = async ( componentsPath: string = 'src/components', - cmsPath: string, + schemaPaths: string[] = ['src/components'], + layerOrder: string[] = ['cms', 'schema'], mergeSchemas: boolean, defaultPageSchema: boolean = true, layerKickstartdsComponents: boolean = true, @@ -55,11 +56,13 @@ const run = async ( const types = async (logger: winston.Logger): Promise => { logger.info(chalkTemplate`running the {bold types} subtask`); - const customSchemaGlob = `${callingPath}/${componentsPath}/**/*.schema.json`; - const globs = [customSchemaGlob]; - if (cmsPath) { - globs.push(`${callingPath}/${cmsPath}/**/*.schema.json`); + const globs = []; + for (const schemaPath of schemaPaths) { + globs.push( + `${callingPath}/${schemaPath}/**/*.(schema|definitions|interface).json` + ); } + const customSchemaPaths = await fg(globs); const types = await schemaGenerateComponentPropTypes( globs, @@ -67,7 +70,8 @@ const run = async ( defaultPageSchema, layerKickstartdsComponents, typeNaming, - componentsPath + componentsPath, + [...layerOrder, 'kickstartds'] ); await Promise.all( @@ -76,13 +80,9 @@ const run = async ( (schemaPath) => schemaPath.endsWith( `/${schemaId.split('/').pop()}` || 'NO MATCH' - ) && - (schemaId.startsWith('http://cms.') - ? !schemaPath.includes('node_modules') - : true) + ) && !schemaPath.includes('node_modules') ); - if (!schemaPath) - throw new Error("Couldn't find matching schema path for schema $id"); + if (!schemaPath) return true; const base = basename(schemaPath, '.json'); const dir = dirname(schemaPath); diff --git a/src/util/schema.ts b/src/util/schema.ts index fb4f39b..8f3825e 100644 --- a/src/util/schema.ts +++ b/src/util/schema.ts @@ -14,6 +14,8 @@ import { getSchemaDefaults, IClassifierResult, processSchemaGlobs, + getTopLayerSchemas, + getSchemasForIds, } from '@kickstartds/jsonschema-utils'; import type { IStoryblokBlock } from '@kickstartds/jsonschema2storyblok'; import type { IStaticCmsField } from '@kickstartds/jsonschema2staticcms'; @@ -31,16 +33,31 @@ export default (logger: winston.Logger): SchemaUtil => { const dereferenceSchemas = async ( schemaGlobs: string[], defaultPageSchema = true, - layerKickstartdsComponents = true + layerKickstartdsComponents = true, + layerOrder: string[] ) => { const ajv = getSchemaRegistry(); const schemaIds = await processSchemaGlobs(schemaGlobs, ajv, { loadPageSchema: defaultPageSchema, layerRefs: layerKickstartdsComponents, + layerOrder, + typeResolution: false, }); const customSchemaIds = getCustomSchemaIds(schemaIds); + const customSchemas = getSchemasForIds(customSchemaIds, ajv); + const topLayerSchemas = getTopLayerSchemas(customSchemas, layerOrder); + const topLayerSchemaIds = []; + for (const schema of topLayerSchemas) { + if (schema.$id) { + topLayerSchemaIds.push(schema.$id); + } else { + subCmdLogger.warn( + chalkTemplate`Schema ${schema.title} has no $id, skipping` + ); + } + } - const dereffedSchemas = await dereference(customSchemaIds, ajv); + const dereffedSchemas = await dereference(topLayerSchemaIds, ajv); subCmdLogger.info( chalkTemplate`dereferencing {bold ${ @@ -89,7 +106,8 @@ export default (logger: winston.Logger): SchemaUtil => { defaultPageSchema = true, layerKickstartdsComponents = true, typeNaming = 'title', - componentsPath = 'src/components' + componentsPath = 'src/components', + layerOrder: string[] ) => { subCmdLogger.info( chalkTemplate`generating component prop types for component schemas` @@ -101,8 +119,21 @@ export default (logger: winston.Logger): SchemaUtil => { mergeAllOf: mergeAllOf, loadPageSchema: defaultPageSchema, layerRefs: layerKickstartdsComponents, + layerOrder, }); const customSchemaIds = getCustomSchemaIds(schemaIds); + const schemas = getSchemasForIds(customSchemaIds, ajv); + const topLayerSchemas = getTopLayerSchemas(schemas, layerOrder); + const topLayerSchemaIds = []; + for (const schema of topLayerSchemas) { + if (schema.$id) { + topLayerSchemaIds.push(schema.$id); + } else { + subCmdLogger.warn( + chalkTemplate`Schema ${schema.title} has no $id, skipping` + ); + } + } const renderImportStatement = (schemaId: string) => schemaId.includes('schema.kickstartds.com') @@ -131,7 +162,7 @@ export default (logger: winston.Logger): SchemaUtil => { const convertedTs = await ( await import('@kickstartds/jsonschema2types') ).createTypes( - customSchemaIds, + topLayerSchemaIds, renderImportName, renderImportStatement, ajv, @@ -146,7 +177,8 @@ export default (logger: winston.Logger): SchemaUtil => { schemaGlobs: string[], mergeAllOf: boolean, defaultPageSchema = true, - layerKickstartdsComponents = true + layerKickstartdsComponents = true, + layerOrder: string[] ) => { subCmdLogger.info( chalkTemplate`layering component prop types for component schemas` @@ -158,6 +190,7 @@ export default (logger: winston.Logger): SchemaUtil => { mergeAllOf: mergeAllOf, loadPageSchema: defaultPageSchema, layerRefs: layerKickstartdsComponents, + layerOrder, }); const kdsSchemaIds = schemaIds.filter((schemaId) => schemaId.includes('schema.kickstartds.com') @@ -167,12 +200,14 @@ export default (logger: winston.Logger): SchemaUtil => { (schemaId) => !customSchemaIds.includes(schemaId) ); const layeredSchemaIds = customSchemaIds.filter((schemaId) => - kdsSchemaIds.some((kdsSchemaId) => shouldLayer(schemaId, kdsSchemaId)) + kdsSchemaIds.some((kdsSchemaId) => + shouldLayer(schemaId, kdsSchemaId, layerOrder) + ) ); const renderImportStatement = (schemaId: string) => { - const layeredId = isLayering(schemaId, kdsSchemaIds) - ? layeredSchemaId(schemaId, kdsSchemaIds) + const layeredId = isLayering(schemaId, kdsSchemaIds, layerOrder) + ? layeredSchemaId(schemaId, kdsSchemaIds, layerOrder) : schemaId; // TODO remove, this is a hack needed because of "broken" conventions // in this case of automatic interface schema generation @@ -199,8 +234,8 @@ export default (logger: winston.Logger): SchemaUtil => { ajv ); for (const schemaId of Object.keys(convertedTs)) { - const layeredId = isLayering(schemaId, kdsSchemaIds) - ? layeredSchemaId(schemaId, kdsSchemaIds) + const layeredId = isLayering(schemaId, kdsSchemaIds, layerOrder) + ? layeredSchemaId(schemaId, kdsSchemaIds, layerOrder) : schemaId; const content = `declare module "@kickstartds/${getSchemaModule( @@ -220,31 +255,46 @@ ${convertedTs[schemaId]} const createDefaultObjects = async ( schemaGlobs: string[], defaultPageSchema = true, - layerKickstartdsComponents = true + layerKickstartdsComponents = true, + layerOrder: string[] ) => { const ajv = getSchemaRegistry(); const schemaIds = await processSchemaGlobs(schemaGlobs, ajv, { loadPageSchema: defaultPageSchema, layerRefs: layerKickstartdsComponents, + layerOrder, + typeResolution: false, }); const kdsSchemaIds = schemaIds.filter((schemaId) => schemaId.includes('schema.kickstartds.com') ); const customSchemaIds = getCustomSchemaIds(schemaIds); + const customSchemas = getSchemasForIds(customSchemaIds, ajv); + const topLayerSchemas = getTopLayerSchemas(customSchemas, layerOrder); + const topLayerSchemaIds = []; + for (const schema of topLayerSchemas) { + if (schema.$id) { + topLayerSchemaIds.push(schema.$id); + } else { + subCmdLogger.warn( + chalkTemplate`Schema ${schema.title} has no $id, skipping` + ); + } + } const defaultObjects: Record = {}; - for (const schemaId of customSchemaIds) { + for (const schemaId of topLayerSchemaIds) { defaultObjects[schemaId] = await getSchemaDefaults(schemaId, ajv); } const getImportName = (schemaId: string) => { - const layeredId = isLayering(schemaId, kdsSchemaIds) - ? layeredSchemaId(schemaId, kdsSchemaIds) + const layeredId = isLayering(schemaId, kdsSchemaIds, layerOrder) + ? layeredSchemaId(schemaId, kdsSchemaIds, layerOrder) : schemaId; return `${pascalCase(getSchemaName(layeredId))}Props`; }; - for (const schemaId of Object.keys(defaultObjects)) { + for (const schemaId of topLayerSchemaIds) { defaultObjects[schemaId] = `import { DeepPartial } from "../helpers"; import { ${getImportName(schemaId)} } from "./${getImportName(schemaId)}"; @@ -269,11 +319,14 @@ export default defaults;`; schemaGlobs: string[], templates: string[], globals: string[], - components: string[] + components: string[], + layerOrder: string[] ) => { const ajv = getSchemaRegistry(); const schemaIds = await processSchemaGlobs(schemaGlobs, ajv, { + typeResolution: false, hideCmsFields: true, + layerOrder, }); const customSchemaIds = getCustomSchemaIds(schemaIds); @@ -308,11 +361,13 @@ export default defaults;`; const toUniform = async ( schemaGlobs: string[], templates: string[], - globals: string[] + globals: string[], + layerOrder: string[] ) => { const ajv = getSchemaRegistry(); const schemaIds = await processSchemaGlobs(schemaGlobs, ajv, { hideCmsFields: true, + layerOrder, }); const customSchemaIds = getCustomSchemaIds(schemaIds); @@ -341,11 +396,13 @@ export default defaults;`; schemaGlobs: string[], templates: string[], globals: string[], - components: string[] + components: string[], + layerOrder: string[] ) => { const ajv = getSchemaRegistry(); const schemaIds = await processSchemaGlobs(schemaGlobs, ajv, { hideCmsFields: true, + layerOrder, }); const customSchemaIds = getCustomSchemaIds(schemaIds); @@ -382,11 +439,13 @@ export default defaults;`; const toStaticcms = async ( schemaGlobs: string[], templates: string[], - globals: string[] + globals: string[], + layerOrder: string[] ) => { const ajv = getSchemaRegistry(); const schemaIds = await processSchemaGlobs(schemaGlobs, ajv, { hideCmsFields: true, + layerOrder, }); const customSchemaIds = getCustomSchemaIds(schemaIds); diff --git a/types/index.d.ts b/types/index.d.ts index 8587011..d1a48a1 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -219,29 +219,34 @@ interface SchemaUtil { defaultPageSchema: boolean, layerRefs: boolean, typeNaming: string, - componentsPath: string + componentsPath: string, + layerOrder: string[] ) => Promise>; layerComponentPropTypes: ( schemaGlobs: string[], mergeAllOf: boolean, defaultPageSchema: boolean, - layerRefs: boolean + layerRefs: boolean, + layerOrder: string[] ) => Promise>; dereferenceSchemas: ( schemaGlobs: string[], defaultPageSchema: boolean, - layerRefs: boolean + layerRefs: boolean, + layerOrder: string[] ) => Promise>; createDefaultObjects: ( schemaGlobs: string[], defaultPageSchema: boolean, - layerRefs: boolean + layerRefs: boolean, + layerOrder: string[] ) => Promise>; toStoryblok: ( schemaGlobs: string[], templates: string[], globals: string[], - components: string[] + components: string[], + layerOrder: string[] ) => Promise>; toStoryblokConfig: ( elements: CMSResult @@ -249,13 +254,15 @@ interface SchemaUtil { toUniform: ( schemaGlobs: string[], templates: string[], - globals: string[] + globals: string[], + layerOrder: string[] ) => Promise>; toStackbit: ( schemaGlobs: string[], templates: string[], globals: string[], - components: string[] + components: string[], + layerOrder: string[] ) => Promise>; toStackbitConfig: ( elements: CMSResult @@ -264,7 +271,8 @@ interface SchemaUtil { schemaGlobs: string[], templates: string[], globals: string[], - components: string[] + components: string[], + layerOrder: string[] ) => Promise>; toStaticcmsConfig: ( elements: CMSResult, diff --git a/yarn.lock b/yarn.lock index 580a5a2..17f3813 100644 --- a/yarn.lock +++ b/yarn.lock @@ -921,10 +921,10 @@ mkdirp "^1.0.4" prettier "^2.6.2" -"@kickstartds/jsonschema-utils@3.6.1", "@kickstartds/jsonschema-utils@^3.6.1": - version "3.6.1" - resolved "https://registry.npmjs.org/@kickstartds/jsonschema-utils/-/jsonschema-utils-3.6.1.tgz#b692e56bfeae8c8bf8961ba3f86e358a811da198" - integrity sha512-Ke5adEzH0s5XXYJmBLGIIGBXAl0rWHaiOoFE0LmZUD1S8rTgS7VN5hoW6OWj35uXcWRVwC0sdkPFnKsVCxM+rQ== +"@kickstartds/jsonschema-utils@3.8.1", "@kickstartds/jsonschema-utils@^3.8.1": + version "3.8.1" + resolved "https://registry.npmjs.org/@kickstartds/jsonschema-utils/-/jsonschema-utils-3.8.1.tgz#4f568c7bd6865b8cb0f774d8263a190f8107d7e3" + integrity sha512-3ZdI2ZscE2yvgDN5XfiZMupRlFpU3+lGIBo7aG47YTYw58arBM/ES83Y9aS2FatWWKVEW8G+vIBmvWgn9R4y3w== dependencies: "@bcherny/json-schema-ref-parser" "10.0.5-fork" "@kickstartds/cambria" "1.0.5" @@ -936,52 +936,52 @@ json-schema-traverse "^1.0.0" jsonpointer "~5.0.1" -"@kickstartds/jsonschema2stackbit@^1.7.4": - version "1.7.4" - resolved "https://registry.npmjs.org/@kickstartds/jsonschema2stackbit/-/jsonschema2stackbit-1.7.4.tgz#bd43e7932c4b8e207e85af40b82caa52f3efccfb" - integrity sha512-IKZCckXqOZeAj3xvm01t0KNOdxCA/kdlkaT1keXIzHglzvBEACu5JjaGaU0/OfswVJ8tfUOh9rqEb3RsIfN0Lg== +"@kickstartds/jsonschema2stackbit@^1.7.8": + version "1.7.8" + resolved "https://registry.npmjs.org/@kickstartds/jsonschema2stackbit/-/jsonschema2stackbit-1.7.8.tgz#0988792b650a2859d5832292d9cbfd231b682295" + integrity sha512-hyg0SrF+HsxhURBVxpLcJt+YwlPXTstPM+l/ZLHWlG8YW4pSn5NlW7rRdx2ebATbldVwqmQ670pR0RyFF/nPrg== dependencies: - "@kickstartds/jsonschema-utils" "3.6.1" + "@kickstartds/jsonschema-utils" "3.8.1" ajv "^8.12.0" object-traversal "^1.0.1" pluralize-esm "~9.0.5" -"@kickstartds/jsonschema2staticcms@^2.9.35": - version "2.9.35" - resolved "https://registry.npmjs.org/@kickstartds/jsonschema2staticcms/-/jsonschema2staticcms-2.9.35.tgz#0872a8a7d441cb88230fc88a6b573f4477261200" - integrity sha512-uJUZXVVl+ZPwYgNfFwAW6lHQcmjyyttDMpC6CS9WE1Y3CyBMANT12UKkfrLbfEcj8LJNlUdS/mJBOoF8moT8Yw== +"@kickstartds/jsonschema2staticcms@^2.9.39": + version "2.9.39" + resolved "https://registry.npmjs.org/@kickstartds/jsonschema2staticcms/-/jsonschema2staticcms-2.9.39.tgz#f50f3f61eab7f6d84d1eab71dd0cb8429bb5b115" + integrity sha512-2E44xeNES95pEU9g/1Ym8+yfXZbLT6zTIqBuPCc6E5rIxRtMCUEB4my+kBr6yTvO6tsJ6IdZHF1NNct+kKuNOw== dependencies: - "@kickstartds/jsonschema-utils" "3.6.1" + "@kickstartds/jsonschema-utils" "3.8.1" ajv "^8.12.0" object-traversal "^1.0.1" pluralize "^8.0.0" -"@kickstartds/jsonschema2storyblok@^1.6.5": - version "1.6.5" - resolved "https://registry.npmjs.org/@kickstartds/jsonschema2storyblok/-/jsonschema2storyblok-1.6.5.tgz#f8e376df999a732d4bfb6a270a01afbe2cfba16f" - integrity sha512-h+AHseOHkiU95LZ+qxlw2d66yJm/nH4iU+mXgG9QGsRZrQjchFmEgGtj2/xXMJWl9d0ZCsWDuTBfr9/R8jE0dQ== +"@kickstartds/jsonschema2storyblok@^1.6.9": + version "1.6.9" + resolved "https://registry.npmjs.org/@kickstartds/jsonschema2storyblok/-/jsonschema2storyblok-1.6.9.tgz#877bd469f98d5625ba4f7390531c9032d3637797" + integrity sha512-AnMCBxCfG06rp7yV0bp7oOUSbgiv+iYNf1pLWKOWSJ0Qzz8wFRWK1wxozHwZ2OCB7rPsx34bJzCabwpAtg6UHQ== dependencies: - "@kickstartds/jsonschema-utils" "3.6.1" + "@kickstartds/jsonschema-utils" "3.8.1" ajv "^8.12.0" object-traversal "^1.0.1" uuid "~9.0.0" -"@kickstartds/jsonschema2types@^1.2.3": - version "1.2.3" - resolved "https://registry.npmjs.org/@kickstartds/jsonschema2types/-/jsonschema2types-1.2.3.tgz#a4580b772648682d1c07c37af5be7aabf38ee891" - integrity sha512-Dp41eGa4jkyahkB0NPYroixStSP/+f/01r5FuDIxdJK3ljU7FWTJGuYPNcToPN0gZxSHgaWASirFrAKUD5TgUg== +"@kickstartds/jsonschema2types@^1.2.7": + version "1.2.7" + resolved "https://registry.npmjs.org/@kickstartds/jsonschema2types/-/jsonschema2types-1.2.7.tgz#31753adcb79cd2c8a73a57f3195c94bc623dc8a4" + integrity sha512-f3yNQWFzGLZf2Jgnr4OyBJl8+ldgo/WmnSJML1OZ8QJ1bcj8cwaHhNxHWNCHRhExguIwReHLq6HKJUaADlkmuw== dependencies: "@kickstartds/json-schema-to-typescript" "~13.1.20" - "@kickstartds/jsonschema-utils" "3.6.1" + "@kickstartds/jsonschema-utils" "3.8.1" ajv "^8.12.0" object-traversal "^1.0.1" -"@kickstartds/jsonschema2uniform@^1.4.22": - version "1.4.22" - resolved "https://registry.npmjs.org/@kickstartds/jsonschema2uniform/-/jsonschema2uniform-1.4.22.tgz#59c14e21f1b720add603ee457fc1a30c751f6737" - integrity sha512-SuPgC+PN2IaOJq7zugyXne2zUyJ5e4TLyClbYMBQAxrWnboRDEc8QzcxR7Pf9WyHuAKaO8w2F35c5tEN8hAWAA== +"@kickstartds/jsonschema2uniform@^1.4.26": + version "1.4.26" + resolved "https://registry.npmjs.org/@kickstartds/jsonschema2uniform/-/jsonschema2uniform-1.4.26.tgz#255d7d1b05fe8ef62e307a8dc81a0533c1748617" + integrity sha512-C3l+HEofAulRHHX8KKW+dYMvteUptxgbyRrAuhmCGEuBAUYwVWmtIyC1RftvGs4syf8eSiOJvWbXORNIx0RYkw== dependencies: - "@kickstartds/jsonschema-utils" "3.6.1" + "@kickstartds/jsonschema-utils" "3.8.1" ajv "^8.12.0" change-case "^5.0.2"