Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

fix: account for case where content type id contains dash #249

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions lib/bootstrap/jsonToScript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const eslint = require('eslint')

const normalizeContentTypeID = (id) => id.replaceAll('-', '_')

const removeNullValues = (value) => {
if (value instanceof Array) {
return value.map(removeNullValues)
Expand All @@ -26,17 +28,17 @@ const rejectEmptyObjects = ([, value]) => {
}

const createField = (itemId, field) => `
${itemId}.createField("${field.id}")${''.concat(...Object.entries(field.props).filter(rejectEmptyObjects).map(createProp))};
${normalizeContentTypeID(itemId)}.createField("${field.id}")${''.concat(...Object.entries(field.props).filter(rejectEmptyObjects).map(createProp))};
`

const createChangeFieldControl = (itemId, field) => {
const { fieldId, widgetId, settings } = field
const baseString = `${itemId}.changeFieldControl("${fieldId}", "builtin", "${widgetId}"`
const baseString = `${normalizeContentTypeID(itemId)}.changeFieldControl("${fieldId}", "builtin", "${widgetId}"`
return settings ? `${baseString}, ${JSON.stringify(settings)});` : `${baseString});`
}

const createContentType = (item, editorInterface) => `
const ${item.id} = migration.createContentType('${item.id}')${''.concat(...Object.entries(item.props).map(createProp))};
const ${normalizeContentTypeID(item.id)} = migration.createContentType('${item.id}')${''.concat(...Object.entries(item.props).map(createProp))};
${''.concat(...item.fields.map(field => createField(item.id, field)))}
${editorInterface.map(field => createChangeFieldControl(item.id, field)).join('\n')}
`
Expand Down