Skip to content

Commit

Permalink
feat(storybook): edit mainjs to include addon-essentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored and meeroslav committed Jun 25, 2021
1 parent 435b638 commit ee47bff
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { formatFiles, Tree, logger, updateJson } from '@nrwl/devkit';
import { writeFileSync } from 'fs';

let needsInstall = false;
const targetStorybookVersion = '6.3.0';
Expand All @@ -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);

Expand Down

0 comments on commit ee47bff

Please sign in to comment.