Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2895 from botpress/ya-adj-markdown
fix(cms): markdown property
- Loading branch information
Showing
3 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
modules/builtin/src/migrations/v12_5_0-1577776334-text-markdown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as sdk from 'botpress/sdk' | ||
|
||
const ELEMENTS_DIR = 'content-elements' | ||
|
||
const migration: sdk.ModuleMigration = { | ||
info: { | ||
description: 'Enable markdown by default for all text messages', | ||
target: 'bot', | ||
type: 'content' | ||
}, | ||
up: async ({ bp, metadata }: sdk.ModuleMigrationOpts): Promise<sdk.MigrationResult> => { | ||
const checkFile = (fileContent: string, nbElements: number) => { | ||
const parsed = JSON.parse(fileContent) | ||
return parsed.length === nbElements | ||
} | ||
|
||
const updateFormData = ({ formData }: sdk.ContentElement) => { | ||
for (const key of Object.keys(formData)) { | ||
if (key.startsWith('text$')) { | ||
const language = key.substr(key.indexOf('$') + 1, key.length) | ||
const markdownKey = 'markdown$' + language | ||
|
||
if (!(markdownKey in formData)) { | ||
formData[markdownKey] = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
const migrateBotTextContent = async (botId: string) => { | ||
const bpfs = bp.ghost.forBot(botId) | ||
const entFiles = await bpfs.directoryListing(ELEMENTS_DIR, '*.json') | ||
|
||
for (const fileName of entFiles) { | ||
try { | ||
const contentElements = await bpfs.readFileAsObject<sdk.ContentElement[]>(ELEMENTS_DIR, fileName) | ||
contentElements.forEach(element => updateFormData(element)) | ||
|
||
const fileContent = JSON.stringify(contentElements, undefined, 2) | ||
|
||
// Just double-checking before writing the content back | ||
if (checkFile(fileContent, contentElements.length)) { | ||
await bpfs.upsertFile(ELEMENTS_DIR, fileName, fileContent, { ignoreLock: true }) | ||
} | ||
} catch (err) { | ||
console.error(`Error processing file ${fileName} for bot ${botId}`) | ||
} | ||
} | ||
} | ||
|
||
if (metadata.botId) { | ||
await migrateBotTextContent(metadata.botId) | ||
} else { | ||
const bots = await bp.bots.getAllBots() | ||
await Promise.map(bots.keys(), botId => migrateBotTextContent(botId)) | ||
} | ||
|
||
return { success: true, message: 'Text content type updated successfully' } | ||
} | ||
} | ||
|
||
export default migration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters