@nx/storybook 19.0.0-beta.7 → 19.0.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/migrations.json CHANGED
@@ -1,29 +1,5 @@
1
1
  {
2
2
  "generators": {
3
- "update-15.0.8-add-storybook-inputs": {
4
- "cli": "nx",
5
- "version": "15.0.0-beta.0",
6
- "description": "Stop hashing storybook config files and story files for build targets and dependent tasks",
7
- "factory": "./src/migrations/update-15-0-0/add-storybook-inputs"
8
- },
9
- "update-15.4.6": {
10
- "cli": "nx",
11
- "version": "15.4.6-beta.0",
12
- "description": "Refactor the Storybook target options",
13
- "factory": "./src/migrations/update-15-4-6/refactor-executor-options"
14
- },
15
- "update-15-5-3": {
16
- "cli": "nx",
17
- "version": "15.5.3-beta.0",
18
- "description": "Add @nrwl/webpack if it is missing and is used.",
19
- "factory": "./src/migrations/update-15-5-3/ensure-webpack-package"
20
- },
21
- "update-15-7-0": {
22
- "cli": "nx",
23
- "version": "15.7.0-beta.0",
24
- "description": "Add @storybook/addon-essentials to all project-level configs and attempt to remove root config.",
25
- "factory": "./src/migrations/update-15-7-0/add-addon-essentials-to-all"
26
- },
27
3
  "update-16-0-0-add-nx-packages": {
28
4
  "cli": "nx",
29
5
  "version": "16.0.0-beta.1",
@@ -877,93 +853,6 @@
877
853
  }
878
854
  }
879
855
  },
880
- "15.5.2": {
881
- "version": "15.5.2-beta.0",
882
- "packages": {
883
- "@storybook/react-native": {
884
- "version": "^6.0.1-beta.11",
885
- "alwaysAddToPackageJson": false
886
- },
887
- "@storybook/addon-ondevice-actions": {
888
- "version": "^6.0.1-beta.11",
889
- "alwaysAddToPackageJson": false
890
- },
891
- "@storybook/addon-ondevice-backgrounds": {
892
- "version": "^6.0.1-beta.11",
893
- "alwaysAddToPackageJson": false
894
- },
895
- "@storybook/addon-ondevice-controls": {
896
- "version": "^6.0.1-beta.11",
897
- "alwaysAddToPackageJson": false
898
- },
899
- "@storybook/addon-ondevice-notes": {
900
- "version": "^6.0.1-beta.11",
901
- "alwaysAddToPackageJson": false
902
- }
903
- }
904
- },
905
- "15.4.3": {
906
- "version": "15.4.3-beta.1",
907
- "packages": {
908
- "@storybook/core-server": {
909
- "version": "^6.5.15",
910
- "alwaysAddToPackageJson": false
911
- },
912
- "@storybook/angular": {
913
- "version": "^6.5.15",
914
- "alwaysAddToPackageJson": false
915
- },
916
- "@storybook/react": {
917
- "version": "^6.5.15",
918
- "alwaysAddToPackageJson": false
919
- },
920
- "@storybook/web": {
921
- "version": "^6.5.15",
922
- "alwaysAddToPackageJson": false
923
- },
924
- "@storybook/addon-essentials": {
925
- "version": "^6.5.15",
926
- "alwaysAddToPackageJson": false
927
- },
928
- "@storybook/builder-webpack5": {
929
- "version": "^6.5.15",
930
- "alwaysAddToPackageJson": false
931
- },
932
- "@storybook/manager-webpack5": {
933
- "version": "^6.5.15",
934
- "alwaysAddToPackageJson": false
935
- },
936
- "@storybook/builder-vite": {
937
- "version": "^0.2.6",
938
- "alwaysAddToPackageJson": false
939
- }
940
- }
941
- },
942
- "15.1.1": {
943
- "version": "15.1.1-beta.0",
944
- "packages": {
945
- "@storybook/react-native": {
946
- "version": "^6.0.1-beta.9",
947
- "alwaysAddToPackageJson": false
948
- },
949
- "@storybook/addon-ondevice-actions": {
950
- "version": "^6.0.1-beta.9",
951
- "alwaysAddToPackageJson": false
952
- },
953
- "@storybook/addon-ondevice-backgrounds": {
954
- "version": "^6.0.1-beta.9",
955
- "alwaysAddToPackageJson": false
956
- },
957
- "@storybook/addon-ondevice-controls": {
958
- "version": "^6.0.1-beta.9",
959
- "alwaysAddToPackageJson": false
960
- },
961
- "@storybook/addon-ondevice-notes": {
962
- "version": "^6.0.1-beta.9",
963
- "alwaysAddToPackageJson": false
964
- }
965
- }
966
- },
967
856
  "18.2.0": {
968
857
  "version": "18.2.0-beta.1",
969
858
  "packages": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/storybook",
3
- "version": "19.0.0-beta.7",
3
+ "version": "19.0.0-beta.9",
4
4
  "private": false,
5
5
  "description": "The Nx Plugin for Storybook contains executors and generators for allowing your workspace to use the powerful Storybook integration testing & documenting capabilities.",
6
6
  "repository": {
@@ -30,14 +30,14 @@
30
30
  "migrations": "./migrations.json"
31
31
  },
32
32
  "dependencies": {
33
- "@nx/devkit": "19.0.0-beta.7",
33
+ "@nx/devkit": "19.0.0-beta.9",
34
34
  "@phenomnomnominal/tsquery": "~5.0.1",
35
35
  "semver": "^7.5.3",
36
36
  "tslib": "^2.3.0",
37
- "@nx/cypress": "19.0.0-beta.7",
38
- "@nx/js": "19.0.0-beta.7",
39
- "@nx/eslint": "19.0.0-beta.7",
40
- "@nrwl/storybook": "19.0.0-beta.7"
37
+ "@nx/cypress": "19.0.0-beta.9",
38
+ "@nx/js": "19.0.0-beta.9",
39
+ "@nx/eslint": "19.0.0-beta.9",
40
+ "@nrwl/storybook": "19.0.0-beta.9"
41
41
  },
42
42
  "publishConfig": {
43
43
  "access": "public"
@@ -1,2 +0,0 @@
1
- import { Tree } from '@nx/devkit';
2
- export default function (tree: Tree): Promise<void>;
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const devkit_1 = require("@nx/devkit");
4
- const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
5
- async function default_1(tree) {
6
- const nxJson = (0, devkit_1.readNxJson)(tree);
7
- const storybookTargets = getStorybookBuildTargets(tree);
8
- const hasProductionFileset = !!nxJson.namedInputs?.production;
9
- if (storybookTargets.size > 0 && hasProductionFileset) {
10
- const productionFileset = new Set(nxJson.namedInputs.production);
11
- for (const exclusion of [
12
- '!{projectRoot}/.storybook/**/*',
13
- '!{projectRoot}/**/*.stories.@(js|jsx|ts|tsx|mdx)',
14
- ]) {
15
- productionFileset.add(exclusion);
16
- }
17
- nxJson.namedInputs.production = Array.from(productionFileset);
18
- }
19
- for (const targetName of storybookTargets) {
20
- nxJson.targetDefaults ??= {};
21
- const storybookTargetDefaults = (nxJson.targetDefaults[targetName] ??= {});
22
- storybookTargetDefaults.inputs ??= [
23
- 'default',
24
- hasProductionFileset ? '^production' : '^default',
25
- ...(tree.exists('.storybook') ? ['{workspaceRoot}/.storybook/**/*'] : []),
26
- ];
27
- }
28
- (0, devkit_1.updateNxJson)(tree, nxJson);
29
- await (0, devkit_1.formatFiles)(tree);
30
- }
31
- exports.default = default_1;
32
- function getStorybookBuildTargets(tree) {
33
- const storybookBuildTargets = new Set();
34
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/storybook:build', (_, __, target) => {
35
- storybookBuildTargets.add(target);
36
- });
37
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@storybook/angular:build-storybook', (_, __, target) => {
38
- storybookBuildTargets.add(target);
39
- });
40
- return storybookBuildTargets;
41
- }
@@ -1,2 +0,0 @@
1
- import { Tree } from '@nx/devkit';
2
- export default function (tree: Tree): Promise<void>;
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const devkit_1 = require("@nx/devkit");
4
- const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
5
- async function default_1(tree) {
6
- updateNonAngularStorybookBuildTargets(tree);
7
- updateNonAngularStorybookServeTargets(tree);
8
- await (0, devkit_1.formatFiles)(tree);
9
- }
10
- exports.default = default_1;
11
- function updateNonAngularStorybookBuildTargets(tree) {
12
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/storybook:build', (_options, projectName, targetName, configuration) => {
13
- if (!configuration) {
14
- return;
15
- }
16
- const projectConfiguration = (0, devkit_1.readProjectConfiguration)(tree, projectName);
17
- if (!projectConfiguration.targets[targetName].options) {
18
- projectConfiguration.targets[targetName].options = {};
19
- }
20
- if (!projectConfiguration.targets[targetName].options.configDir) {
21
- projectConfiguration.targets[targetName].options.configDir =
22
- projectConfiguration.targets[targetName].options.config?.configFolder;
23
- }
24
- if (!projectConfiguration.targets[targetName].options.outputDir) {
25
- projectConfiguration.targets[targetName].options.outputDir =
26
- projectConfiguration.targets[targetName].options.outputPath;
27
- }
28
- if (!projectConfiguration.targets[targetName].options.docs) {
29
- projectConfiguration.targets[targetName].options.docs =
30
- projectConfiguration.targets[targetName].options.docsMode;
31
- }
32
- projectConfiguration.targets[targetName].outputs =
33
- projectConfiguration.targets[targetName].outputs?.map((output) => output.replace('{options.outputPath}', '{options.outputDir}'));
34
- delete projectConfiguration.targets[targetName].options.config;
35
- delete projectConfiguration.targets[targetName].options.outputPath;
36
- delete projectConfiguration.targets[targetName].options.docsMode;
37
- (0, devkit_1.updateProjectConfiguration)(tree, projectName, {
38
- ...projectConfiguration,
39
- });
40
- });
41
- }
42
- function updateNonAngularStorybookServeTargets(tree) {
43
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/storybook:storybook', (_options, projectName, targetName, configuration) => {
44
- if (!configuration) {
45
- return;
46
- }
47
- const projectConfiguration = (0, devkit_1.readProjectConfiguration)(tree, projectName);
48
- if (!projectConfiguration.targets[targetName].options) {
49
- projectConfiguration.targets[targetName].options = {};
50
- }
51
- if (!projectConfiguration.targets[targetName].options.configDir) {
52
- projectConfiguration.targets[targetName].options.configDir =
53
- projectConfiguration.targets[targetName].options.config?.configFolder;
54
- }
55
- if (!projectConfiguration.targets[targetName].options.docs) {
56
- projectConfiguration.targets[targetName].options.docs =
57
- projectConfiguration.targets[targetName].options.docsMode;
58
- }
59
- delete projectConfiguration.targets[targetName].options.config;
60
- delete projectConfiguration.targets[targetName].options.docsMode;
61
- (0, devkit_1.updateProjectConfiguration)(tree, projectName, {
62
- ...projectConfiguration,
63
- });
64
- });
65
- }
@@ -1,2 +0,0 @@
1
- import { Tree } from '@nx/devkit';
2
- export default function update(tree: Tree): Promise<import("@nx/devkit").GeneratorCallback>;
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const devkit_1 = require("@nx/devkit");
4
- const versions_1 = require("../../utils/versions");
5
- // Add @nx/webpack as needed.
6
- // See: https://github.com/nrwl/nx/issues/14455
7
- async function update(tree) {
8
- const projects = (0, devkit_1.getProjects)(tree);
9
- const reactPlugin = '@nrwl/react/plugins/storybook';
10
- let shouldInstall = false;
11
- for (const [, config] of projects) {
12
- let sbConfigPath = (0, devkit_1.joinPathFragments)(config.root, '.storybook/main.ts');
13
- if (!tree.exists(sbConfigPath)) {
14
- sbConfigPath = (0, devkit_1.joinPathFragments)(config.root, '.storybook/main.js');
15
- }
16
- if (!tree.exists(sbConfigPath)) {
17
- continue;
18
- }
19
- const sbConfig = tree.read(sbConfigPath, 'utf-8');
20
- if (sbConfig.includes(reactPlugin)) {
21
- shouldInstall = true;
22
- break;
23
- }
24
- }
25
- if (shouldInstall) {
26
- return (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { '@nrwl/webpack': versions_1.nxVersion });
27
- }
28
- }
29
- exports.default = update;
@@ -1,24 +0,0 @@
1
- import { Tree } from '@nx/devkit';
2
- import ts = require('typescript');
3
- /**
4
- * The purpose of this migrator is to help users move away
5
- * from the root .storybook/ configuration folder and files.
6
- *
7
- * Since we cannot be sure of how users make use of the root Storybook
8
- * directory, what we are going to do is the following:
9
- *
10
- * 1. Make sure that all project-level .storybook/main.js files contain
11
- * the @storybook/addon-essentials addon.
12
- * 2. If the root .storybook/main.js file contains the @storybook/addon-essentials remove it
13
- * from the root
14
- * 3. If there are things beyond the addons array in the root .storybook/main.js file,
15
- * then keep it as it is - inform user that they need to manually need to copy over any extra stuff
16
- * 4. If the root .storybook/main.js file is now empty, inform user that they can delete it safely
17
- *
18
- * Point the user to a guide that explains how to all these things.
19
- */
20
- export default function (tree: Tree): Promise<void>;
21
- export declare function getRootMainVariableName(mainJsTs: string): {
22
- rootMainVariableName: string;
23
- importExpression: ts.Node;
24
- };
@@ -1,271 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getRootMainVariableName = void 0;
4
- const devkit_1 = require("@nx/devkit");
5
- const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
6
- const tsquery_1 = require("@phenomnomnominal/tsquery");
7
- const remove_root_config_1 = require("./remove-root-config");
8
- /**
9
- * The purpose of this migrator is to help users move away
10
- * from the root .storybook/ configuration folder and files.
11
- *
12
- * Since we cannot be sure of how users make use of the root Storybook
13
- * directory, what we are going to do is the following:
14
- *
15
- * 1. Make sure that all project-level .storybook/main.js files contain
16
- * the @storybook/addon-essentials addon.
17
- * 2. If the root .storybook/main.js file contains the @storybook/addon-essentials remove it
18
- * from the root
19
- * 3. If there are things beyond the addons array in the root .storybook/main.js file,
20
- * then keep it as it is - inform user that they need to manually need to copy over any extra stuff
21
- * 4. If the root .storybook/main.js file is now empty, inform user that they can delete it safely
22
- *
23
- * Point the user to a guide that explains how to all these things.
24
- */
25
- async function default_1(tree) {
26
- const projectsThatFailedTOAddAddonEssentials = addAddonEssentialsToAllStorybooks(tree);
27
- if (projectsThatFailedTOAddAddonEssentials.length) {
28
- devkit_1.logger.info(`
29
- We could not add the @storybook/addon-essentials addon
30
- to the following projects' Storybook configurations:
31
-
32
- ${projectsThatFailedTOAddAddonEssentials.join(', ')}
33
-
34
- Please add it manually in the addons array of your project's
35
- .storybook/main.js|ts file.
36
- `);
37
- }
38
- const rootMainJsTsPath = tree.exists('.storybook/main.js')
39
- ? '.storybook/main.js'
40
- : tree.exists('.storybook/main.ts')
41
- ? '.storybook/main.ts'
42
- : undefined;
43
- if (rootMainJsTsPath) {
44
- const addonArrayOrEssentialsRemoved = removeAddonEssentialsFromRootStorybook(tree, rootMainJsTsPath);
45
- const storiesArrayRemoved = removeStoriesArrayFromRootIfEmpty(tree, rootMainJsTsPath);
46
- const removedRoot = (0, remove_root_config_1.removeRootConfig)(tree, rootMainJsTsPath);
47
- if (removedRoot) {
48
- // Logs are already printed in the removeRootConfig function
49
- }
50
- else {
51
- devkit_1.logger.info(`
52
- We removed the ${addonArrayOrEssentialsRemoved === 'addons'
53
- ? 'addons array '
54
- : '@storybook/addon-essentials addon '}
55
- ${storiesArrayRemoved ? 'and the stories array ' : ''}
56
- from the root .storybook/main.js|ts file.
57
- `);
58
- }
59
- }
60
- devkit_1.logger.info(`
61
- Read more about our effort to deprecate the root .storybook folder here:
62
- https://nx.dev/packages/storybook/documents/configuring-storybook
63
- `);
64
- await (0, devkit_1.formatFiles)(tree);
65
- }
66
- exports.default = default_1;
67
- function removeAddonEssentialsFromRootStorybook(tree, rootMainJsTsPath) {
68
- let rootMainJsTs = tree.read(rootMainJsTsPath, 'utf-8');
69
- const addonEssentials = tsquery_1.tsquery.query(rootMainJsTs, 'StringLiteral:has([text="@storybook/addon-essentials"])')?.[0];
70
- if (addonEssentials?.getText()?.length) {
71
- const fullAddonsNode = tsquery_1.tsquery.query(rootMainJsTs, 'PropertyAssignment:has([name="addons"])')?.[0];
72
- const stringLiterals = tsquery_1.tsquery.query(fullAddonsNode, 'StringLiteral');
73
- if (stringLiterals?.length === 1 &&
74
- stringLiterals?.[0]?.getText() === `'@storybook/addon-essentials'`) {
75
- rootMainJsTs = (0, devkit_1.applyChangesToString)(rootMainJsTs, [
76
- {
77
- type: devkit_1.ChangeType.Delete,
78
- start: fullAddonsNode.getStart(),
79
- length: rootMainJsTs[fullAddonsNode.getEnd()] === ','
80
- ? fullAddonsNode.getText().length + 1
81
- : fullAddonsNode.getText().length,
82
- },
83
- ]);
84
- tree.write(rootMainJsTsPath, rootMainJsTs);
85
- return 'addons';
86
- }
87
- else {
88
- rootMainJsTs = (0, devkit_1.applyChangesToString)(rootMainJsTs, [
89
- {
90
- type: devkit_1.ChangeType.Delete,
91
- start: addonEssentials.getStart(),
92
- length: rootMainJsTs[addonEssentials.getEnd()] === ','
93
- ? addonEssentials.getText().length + 1
94
- : addonEssentials.getText().length,
95
- },
96
- ]);
97
- tree.write(rootMainJsTsPath, rootMainJsTs);
98
- return 'esssentials';
99
- }
100
- }
101
- }
102
- function addAddonEssentialsToAllStorybooks(tree) {
103
- const projectsThatFailedTOAddAddonEssentials = [];
104
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/storybook:build', (options, projectName) => {
105
- const failedToAddAddon = addAddon(tree, options, projectName);
106
- if (failedToAddAddon) {
107
- projectsThatFailedTOAddAddonEssentials.push(failedToAddAddon);
108
- }
109
- });
110
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@storybook/angular:build-storybook', (options, projectName) => {
111
- const failedToAddAddon = addAddon(tree, options, projectName);
112
- if (failedToAddAddon) {
113
- projectsThatFailedTOAddAddonEssentials.push(failedToAddAddon);
114
- }
115
- });
116
- return Array.from(new Set(projectsThatFailedTOAddAddonEssentials));
117
- }
118
- function addAddon(tree, options, projectName) {
119
- const storybookDir = options?.['configDir'];
120
- if (storybookDir) {
121
- const mainJsTsPath = tree.exists(`${storybookDir}/main.js`)
122
- ? `${storybookDir}/main.js`
123
- : tree.exists(`${storybookDir}/main.ts`)
124
- ? `${storybookDir}/main.ts`
125
- : undefined;
126
- let addedAddons = mainJsTsPath
127
- ? transformMainJsTs(tree, mainJsTsPath)
128
- : false;
129
- if ((storybookDir && !mainJsTsPath) || !addedAddons) {
130
- return projectName;
131
- }
132
- }
133
- }
134
- function transformMainJsTs(tree, mainJsTsPath) {
135
- let mainJsTs = tree.read(mainJsTsPath, 'utf-8');
136
- const addonsArray = tsquery_1.tsquery.query(mainJsTs, 'ArrayLiteralExpression:has(Identifier[name="addons"])')?.[0];
137
- if (addonsArray?.getText()?.length) {
138
- // If addons array does not contain @storybook/addon-essentials, add it
139
- if (!addonsArray.getText().includes('@storybook/addon-essentials')) {
140
- mainJsTs = (0, devkit_1.applyChangesToString)(mainJsTs, [
141
- {
142
- type: devkit_1.ChangeType.Insert,
143
- index: addonsArray.getStart() + 1,
144
- text: `'@storybook/addon-essentials', `,
145
- },
146
- ]);
147
- tree.write(mainJsTsPath, mainJsTs);
148
- return true;
149
- }
150
- return false;
151
- }
152
- else {
153
- // We will add the addons array after the stories array
154
- // If I have a stories array, that's where my addons need to go
155
- // And there's no config without stories
156
- const storiesArray = tsquery_1.tsquery.query(mainJsTs, 'ArrayLiteralExpression:has(Identifier[name="stories"])')?.[0];
157
- if (storiesArray?.getText()?.length) {
158
- mainJsTs = (0, devkit_1.applyChangesToString)(mainJsTs, [
159
- {
160
- type: devkit_1.ChangeType.Insert,
161
- index: storiesArray.getEnd(),
162
- text: `, addons: ['@storybook/addon-essentials']`,
163
- },
164
- ]);
165
- tree.write(mainJsTsPath, mainJsTs);
166
- return true;
167
- }
168
- else {
169
- /**
170
- * main.js has potentially a different structure
171
- * sort of like this:
172
- * rootMain.addons.push(' ...)
173
- * rootMain.stories.push(' ...)
174
- * Like in older versions of Nx
175
- */
176
- const { rootMainVariableName, importExpression } = getRootMainVariableName(mainJsTs);
177
- // If there is a PropertyAccessExpression with the text rootMain.addons
178
- // then check if it has addon-essentials, if not add it
179
- const addonsPropertyAccessExpression = tsquery_1.tsquery.query(mainJsTs, `PropertyAccessExpression:has([expression.name="${rootMainVariableName}"]):has([name="addons"])`)?.[0];
180
- if (rootMainVariableName && importExpression) {
181
- if (addonsPropertyAccessExpression?.getText() ===
182
- `${rootMainVariableName}.addons.push`) {
183
- const parentCallExpression = addonsPropertyAccessExpression.parent;
184
- // see if parentCallExpression contains a StringLiteral with the text '@storybook/addon-essentials'
185
- const hasAddonEssentials = !!tsquery_1.tsquery
186
- .query(parentCallExpression, `StringLiteral:has([text="@storybook/addon-essentials"])`)?.[0]
187
- ?.getText();
188
- if (!hasAddonEssentials) {
189
- mainJsTs = (0, devkit_1.applyChangesToString)(mainJsTs, [
190
- {
191
- type: devkit_1.ChangeType.Insert,
192
- index: addonsPropertyAccessExpression.getEnd() + 1,
193
- text: `'@storybook/addon-essentials', `,
194
- },
195
- ]);
196
- tree.write(mainJsTsPath, mainJsTs);
197
- return true;
198
- }
199
- }
200
- else {
201
- mainJsTs = (0, devkit_1.applyChangesToString)(mainJsTs, [
202
- {
203
- type: devkit_1.ChangeType.Insert,
204
- index: importExpression.getEnd() + 1,
205
- text: `${rootMainVariableName}.addons.push('@storybook/addon-essentials');`,
206
- },
207
- ]);
208
- tree.write(mainJsTsPath, mainJsTs);
209
- return true;
210
- }
211
- }
212
- }
213
- return false;
214
- }
215
- }
216
- function removeStoriesArrayFromRootIfEmpty(tree, rootMainJsTsPath) {
217
- if (rootMainJsTsPath) {
218
- let rootMainJsTs = tree.read(rootMainJsTsPath, 'utf-8');
219
- const fullStoriesNode = tsquery_1.tsquery.query(rootMainJsTs, 'PropertyAssignment:has([name="stories"])')?.[0];
220
- if (!fullStoriesNode) {
221
- return false;
222
- }
223
- const stringLiterals = tsquery_1.tsquery.query(fullStoriesNode, 'StringLiteral');
224
- if (stringLiterals?.length === 0) {
225
- rootMainJsTs = (0, devkit_1.applyChangesToString)(rootMainJsTs, [
226
- {
227
- type: devkit_1.ChangeType.Delete,
228
- start: fullStoriesNode.getStart(),
229
- length: rootMainJsTs[fullStoriesNode.getEnd()] === ','
230
- ? fullStoriesNode.getText().length + 1
231
- : fullStoriesNode.getText().length,
232
- },
233
- ]);
234
- tree.write(rootMainJsTsPath, rootMainJsTs);
235
- return true;
236
- }
237
- }
238
- }
239
- function getRootMainVariableName(mainJsTs) {
240
- const requireVariableStatement = tsquery_1.tsquery.query(mainJsTs, `VariableStatement:has(CallExpression:has(Identifier[name="require"]))`);
241
- let rootMainVariableName;
242
- let importExpression;
243
- if (requireVariableStatement.length) {
244
- importExpression = requireVariableStatement.find((statement) => {
245
- const requireCallExpression = tsquery_1.tsquery.query(statement, 'CallExpression:has(Identifier[name="require"])');
246
- return requireCallExpression?.[0]?.getText()?.includes('.storybook/main');
247
- });
248
- if (importExpression) {
249
- rootMainVariableName = tsquery_1.tsquery
250
- .query(importExpression, 'Identifier')?.[0]
251
- ?.getText();
252
- }
253
- }
254
- else {
255
- const importDeclarations = tsquery_1.tsquery.query(mainJsTs, 'ImportDeclaration');
256
- importExpression = importDeclarations.find((statement) => {
257
- const stringLiteral = tsquery_1.tsquery.query(statement, 'StringLiteral');
258
- return stringLiteral?.[0]?.getText()?.includes('.storybook/main');
259
- });
260
- if (importExpression) {
261
- rootMainVariableName = tsquery_1.tsquery
262
- .query(importExpression, 'ImportSpecifier')?.[0]
263
- ?.getText();
264
- }
265
- }
266
- return {
267
- rootMainVariableName,
268
- importExpression,
269
- };
270
- }
271
- exports.getRootMainVariableName = getRootMainVariableName;
@@ -1,14 +0,0 @@
1
- import { Tree } from '@nx/devkit';
2
- /**
3
- * The purpose of this migrator is to help users move away
4
- * from the root .storybook/ configuration folder and files.
5
- *
6
- * This is the second part of the migrator.
7
- *
8
- * If the root main.js file is empty, then we can safely delete it
9
- * and also remove all the references to it from the project-level
10
- * Storybook configuration files.
11
- *
12
- * Point the user to a guide that explains how to all these things.
13
- */
14
- export declare function removeRootConfig(tree: Tree, rootMainJsTsPath: string): boolean;
@@ -1,205 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeRootConfig = void 0;
4
- const devkit_1 = require("@nx/devkit");
5
- const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
6
- const tsquery_1 = require("@phenomnomnominal/tsquery");
7
- const add_addon_essentials_to_all_1 = require("./add-addon-essentials-to-all");
8
- const ts = require("typescript");
9
- /**
10
- * The purpose of this migrator is to help users move away
11
- * from the root .storybook/ configuration folder and files.
12
- *
13
- * This is the second part of the migrator.
14
- *
15
- * If the root main.js file is empty, then we can safely delete it
16
- * and also remove all the references to it from the project-level
17
- * Storybook configuration files.
18
- *
19
- * Point the user to a guide that explains how to all these things.
20
- */
21
- function removeRootConfig(tree, rootMainJsTsPath) {
22
- if (checkIfRootMainJsTsIsEmpty(tree, rootMainJsTsPath)) {
23
- const hasRemainingRootMainJsReferences = removeImportFromAllFiles(tree);
24
- tree.delete(rootMainJsTsPath);
25
- devkit_1.logger.warn(`
26
- We removed the root ${rootMainJsTsPath} file and we also
27
- removed all it's imports from all project-level Storybook configuration files.
28
- `);
29
- if (hasRemainingRootMainJsReferences.length) {
30
- devkit_1.logger.warn(`
31
- However, there are still other references to the root .storybook/main.js|ts file
32
- in the following files:
33
-
34
- ${hasRemainingRootMainJsReferences.join('\n')}
35
-
36
- Please remove them manually.
37
- `);
38
- }
39
- return true;
40
- }
41
- }
42
- exports.removeRootConfig = removeRootConfig;
43
- function removeImportFromAllFiles(tree) {
44
- const hasRemainingRootMainJsReferences = [];
45
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/storybook:build', (options) => {
46
- const hasRemainingReference = makeTheChanges(tree, options);
47
- if (hasRemainingReference) {
48
- hasRemainingRootMainJsReferences.push(hasRemainingReference);
49
- }
50
- });
51
- (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@storybook/angular:build-storybook', (options) => {
52
- const hasRemainingReference = makeTheChanges(tree, options);
53
- if (hasRemainingReference) {
54
- hasRemainingRootMainJsReferences.push(hasRemainingReference);
55
- }
56
- });
57
- return hasRemainingRootMainJsReferences;
58
- }
59
- function makeTheChanges(tree, options) {
60
- const storybookDir = options?.['configDir'];
61
- if (storybookDir) {
62
- const mainJsTsPath = tree.exists(`${storybookDir}/main.js`)
63
- ? `${storybookDir}/main.js`
64
- : tree.exists(`${storybookDir}/main.ts`)
65
- ? `${storybookDir}/main.ts`
66
- : undefined;
67
- if (mainJsTsPath) {
68
- let mainJsTs = tree.read(mainJsTsPath, 'utf-8');
69
- const { rootMainVariableName, importExpression } = (0, add_addon_essentials_to_all_1.getRootMainVariableName)(mainJsTs);
70
- if (importExpression && rootMainVariableName) {
71
- const changesToBeMade = [
72
- {
73
- type: devkit_1.ChangeType.Delete,
74
- start: importExpression.getStart(),
75
- length: importExpression.getText().length,
76
- },
77
- ];
78
- const spreadElements = tsquery_1.tsquery.query(mainJsTs, `SpreadElement:has(Identifier[name="${rootMainVariableName}"])`);
79
- spreadElements.forEach((spreadElement) => {
80
- changesToBeMade.push({
81
- type: devkit_1.ChangeType.Delete,
82
- start: spreadElement.getStart(),
83
- length: mainJsTs[spreadElement.getEnd()] === ','
84
- ? spreadElement.getText().length + 1
85
- : spreadElement.getText().length,
86
- });
87
- });
88
- const spreadAssignments = tsquery_1.tsquery.query(mainJsTs, `SpreadAssignment:has(Identifier[name="${rootMainVariableName}"])`);
89
- spreadAssignments.forEach((spreadAssignment) => {
90
- changesToBeMade.push({
91
- type: devkit_1.ChangeType.Delete,
92
- start: spreadAssignment.getStart(),
93
- length: mainJsTs[spreadAssignment.getEnd()] === ','
94
- ? spreadAssignment.getText().length + 1
95
- : spreadAssignment.getText().length,
96
- });
97
- });
98
- const findOtherRootMainUses = tsquery_1.tsquery.query(mainJsTs, `Identifier[name="${rootMainVariableName}"]`);
99
- findOtherRootMainUses.forEach((otherRootMainUse) => {
100
- /**
101
- * This would be mainly to remove the legacy
102
- *
103
- * if (rootMain.webpackFinal) {
104
- * config = await rootMain.webpackFinal(config, { configType });
105
- * }
106
- */
107
- if (otherRootMainUse.parent.kind ===
108
- ts.SyntaxKind.PropertyAccessExpression &&
109
- otherRootMainUse.parent.parent?.kind === ts.SyntaxKind.IfStatement) {
110
- changesToBeMade.push({
111
- type: devkit_1.ChangeType.Delete,
112
- start: otherRootMainUse.parent.parent.getStart(),
113
- length: otherRootMainUse.parent.parent.getText().length + 1,
114
- });
115
- }
116
- });
117
- mainJsTs = (0, devkit_1.applyChangesToString)(mainJsTs, [...changesToBeMade]);
118
- tree.write(mainJsTsPath, mainJsTs);
119
- if (hasMoreRootMainUses(tree, mainJsTsPath, rootMainVariableName)) {
120
- if (checkIfUsesOldSyntaxAndUpdate(tree, mainJsTsPath, rootMainVariableName)) {
121
- return undefined;
122
- }
123
- return mainJsTsPath;
124
- }
125
- }
126
- }
127
- }
128
- }
129
- function checkIfUsesOldSyntaxAndUpdate(tree, filePath, rootMainVariableName) {
130
- const mainJsTs = tree.read(filePath, 'utf-8');
131
- const changesToBeMade = [];
132
- const { stringArray: addonsArrayString, expressionToDelete: addonsToDelete } = getPropertyArray('addons', mainJsTs, rootMainVariableName);
133
- const { stringArray: storiesArrayString, expressionToDelete: storiesToDelete, } = getPropertyArray('stories', mainJsTs, rootMainVariableName);
134
- if (storiesToDelete) {
135
- changesToBeMade.push(storiesToDelete);
136
- }
137
- if (addonsToDelete) {
138
- changesToBeMade.push(addonsToDelete);
139
- }
140
- if (addArrayToModuleExports('addons', mainJsTs, addonsArrayString)) {
141
- changesToBeMade.push(addArrayToModuleExports('addons', mainJsTs, addonsArrayString));
142
- }
143
- if (addArrayToModuleExports('stories', mainJsTs, storiesArrayString)) {
144
- changesToBeMade.push(addArrayToModuleExports('stories', mainJsTs, storiesArrayString));
145
- }
146
- if (changesToBeMade.length) {
147
- tree.write(filePath, (0, devkit_1.applyChangesToString)(mainJsTs, changesToBeMade));
148
- return true;
149
- }
150
- }
151
- function addArrayToModuleExports(propertyName, mainJsTs, arrayString) {
152
- if (!arrayString) {
153
- return;
154
- }
155
- const moduleExports = tsquery_1.tsquery.query(mainJsTs, `PropertyAccessExpression:has([expression.name="module"]):has([name="exports"]):has([name="${propertyName}"])`)?.[0];
156
- if (moduleExports) {
157
- const parentBinaryExpression = moduleExports.parent;
158
- const arrayExpression = tsquery_1.tsquery.query(parentBinaryExpression, `ArrayLiteralExpression`)?.[0];
159
- return {
160
- type: devkit_1.ChangeType.Insert,
161
- index: arrayExpression.getStart() + 1,
162
- text: arrayString,
163
- };
164
- }
165
- else {
166
- return {
167
- type: devkit_1.ChangeType.Insert,
168
- index: mainJsTs.length,
169
- text: `module.exports.${propertyName} = [${arrayString}];\n`,
170
- };
171
- }
172
- }
173
- function getPropertyArray(propertyName, mainJsTs, rootMainVariableName) {
174
- const propertyAccessExpression = tsquery_1.tsquery.query(mainJsTs, `PropertyAccessExpression:has([expression.name="${rootMainVariableName}"]):has([name="${propertyName}"])`)?.[0];
175
- if (propertyAccessExpression) {
176
- if (propertyAccessExpression?.getText() ===
177
- `${rootMainVariableName}.${propertyName}.push`) {
178
- const parentCallExpression = propertyAccessExpression.parent;
179
- const stringPropertyArray = tsquery_1.tsquery
180
- .query(parentCallExpression, `StringLiteral`)
181
- .map((stringLiteral) => stringLiteral?.getText());
182
- return {
183
- stringArray: `${stringPropertyArray.join(', ')}`,
184
- expressionToDelete: {
185
- type: devkit_1.ChangeType.Delete,
186
- start: parentCallExpression.getStart(),
187
- length: parentCallExpression.getText().length + 1,
188
- },
189
- };
190
- }
191
- }
192
- }
193
- function hasMoreRootMainUses(tree, filePath, rootMainVariableName) {
194
- const mainJsTs = tree.read(filePath, 'utf-8');
195
- const findRemainingRootMainUses = tsquery_1.tsquery.query(mainJsTs, `Identifier[name="${rootMainVariableName}"]`);
196
- return findRemainingRootMainUses?.length > 0;
197
- }
198
- function checkIfRootMainJsTsIsEmpty(tree, rootMainJsTsPath) {
199
- const rootMainJsTs = tree.read(rootMainJsTsPath, 'utf-8');
200
- const mainConfigObject = tsquery_1.tsquery.query(rootMainJsTs, 'ObjectLiteralExpression');
201
- if (mainConfigObject?.length === 1 &&
202
- mainConfigObject[0]?.getText()?.replace(/\s/g, '') === '{}') {
203
- return true;
204
- }
205
- }