diff --git a/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts b/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts index bd23866c6ef99c..c75c2879cec742 100644 --- a/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts +++ b/packages/storybook/src/migrations/update-12-5-0/install-addon-essentials.ts @@ -1,4 +1,5 @@ import { formatFiles, Tree, logger, updateJson } from '@nrwl/devkit'; +import { writeFileSync } from 'fs'; let needsInstall = false; const targetStorybookVersion = '6.3.0'; @@ -21,7 +22,37 @@ function installAddonEssentials(tree: Tree) { }); } +function editRootMainJs(tree: Tree) { + let newContent: string; + const rootMainJsExists = tree.exists(`.storybook/main.js`); + if (rootMainJsExists) { + const rootMainJs = require('./.storybook/main.js'); + const moduleContent = JSON.parse(rootMainJs); + const addonsArray: string[] = moduleContent?.addons; + if (addonsArray) { + if (!addonsArray.includes('@storybook/addon-essentials')) { + addonsArray.push('@storybook/addon-essentials'); + moduleContent.addonsArray = addonsArray; + } + } else { + moduleContent.addonsArray = ['@storybook/addon-essentials']; + } + newContent = ` + module.exports = ${JSON.stringify(moduleContent)} + `; + } else { + newContent = ` + module.exports = { + stories: [], + addons: ['@storybook/addon-essentials'], + }; + `; + } + tree.write(`.storybook/main.js`, JSON.stringify(newContent, null, 2)); +} + export default async function (tree: Tree) { + editRootMainJs(tree); installAddonEssentials(tree); await formatFiles(tree);