-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
install-addon-essentials.ts
64 lines (57 loc) · 1.81 KB
/
install-addon-essentials.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { formatFiles, Tree, logger, updateJson } from '@nrwl/devkit';
import { writeFileSync } from 'fs';
let needsInstall = false;
const targetStorybookVersion = '6.3.0';
function installAddonEssentials(tree: Tree) {
updateJson(tree, 'package.json', (json) => {
json.dependencies = json.dependencies || {};
json.devDependencies = json.devDependencies || {};
if (
!json.dependencies['@storybook/addon-essentials'] &&
!json.devDependencies['@storybook/addon-essentials']
) {
needsInstall = true;
json.devDependencies['@storybook/addon-essentials'] =
targetStorybookVersion;
}
return json;
});
}
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'],
};
`;
}
writeFileSync(`.storybook/main.js`, JSON.stringify(newContent, null, 2));
}
export default async function (tree: Tree) {
editRootMainJs(tree);
installAddonEssentials(tree);
await formatFiles(tree);
if (needsInstall) {
logger.info(
'Please make sure to run npm install or yarn install to get the latest packages added by this migration'
);
}
}