@storybook/react-native 8.4.3-alpha.0 → 8.4.3-alpha.2

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/dist/index.js CHANGED
@@ -1038,7 +1038,7 @@ var View3 = class {
1038
1038
  );
1039
1039
  }
1040
1040
  if (onDeviceUI) {
1041
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_theming2.ThemeProvider, { theme: appliedTheme, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_safe_area_context.SafeAreaProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_gesture_handler.GestureHandlerRootView, { style: { flex: 1 }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_bottom_sheet.BottomSheetModalProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_ui.LayoutProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_ui.Layout, { storyHash, story, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(StoryView_default, {}) }) }) }) }) }) });
1041
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_theming2.ThemeProvider, { theme: appliedTheme, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_safe_area_context.SafeAreaProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_gesture_handler.GestureHandlerRootView, { style: { flex: 1 }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_bottom_sheet.BottomSheetModalProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_ui.StorageProvider, { storage, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_ui.LayoutProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native_ui.Layout, { storyHash, story, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(StoryView_default, {}) }) }) }) }) }) }) });
1042
1042
  } else {
1043
1043
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(StoryView_default, {});
1044
1044
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/react-native",
3
- "version": "8.4.3-alpha.0",
3
+ "version": "8.4.3-alpha.2",
4
4
  "description": "A better way to develop React Native Components for your app",
5
5
  "keywords": [
6
6
  "react",
@@ -19,8 +19,7 @@
19
19
  "license": "MIT",
20
20
  "main": "dist/index.js",
21
21
  "bin": {
22
- "sb-rn-get-stories": "./bin/get-stories.js",
23
- "sb-rn-watcher": "./bin/watcher.js"
22
+ "sb-rn-get-stories": "./bin/get-stories.js"
24
23
  },
25
24
  "exports": {
26
25
  ".": "./dist/index.js",
@@ -50,8 +49,8 @@
50
49
  "@storybook/csf": "^0.1.1",
51
50
  "@storybook/global": "^5.0.0",
52
51
  "@storybook/react": "^8.4.2",
53
- "@storybook/react-native-theming": "^8.4.3-alpha.0",
54
- "@storybook/react-native-ui": "^8.4.3-alpha.0",
52
+ "@storybook/react-native-theming": "^8.4.3-alpha.2",
53
+ "@storybook/react-native-ui": "^8.4.3-alpha.2",
55
54
  "chokidar": "^3.5.1",
56
55
  "commander": "^8.2.0",
57
56
  "dedent": "^1.5.1",
@@ -74,7 +73,7 @@
74
73
  "jest": "^29.7.0",
75
74
  "jotai": "^2.6.2",
76
75
  "react": "18.3.1",
77
- "react-native": "0.76.1",
76
+ "react-native": "0.76.3",
78
77
  "react-test-renderer": "^18.3.1",
79
78
  "tsup": "^7.2.0",
80
79
  "typescript": "^5.3.3"
@@ -92,5 +91,5 @@
92
91
  "publishConfig": {
93
92
  "access": "public"
94
93
  },
95
- "gitHead": "b298f535333a510b3eda185a8317e35f9b003c05"
94
+ "gitHead": "78da05071501569d90a08aa6dee9382bf79f7cd8"
96
95
  }
@@ -1,10 +1,5 @@
1
1
  const { getArguments } = require('./handle-args');
2
2
  const args = getArguments();
3
3
 
4
- if (args.v6Store) {
5
- const { writeRequires } = require('./loader');
6
- writeRequires(args);
7
- } else {
8
- const { generate } = require('./generate');
9
- generate(args);
10
- }
4
+ const { generate } = require('./generate');
5
+ generate(args);
@@ -2,7 +2,7 @@ function getArguments() {
2
2
  const { program } = require('commander');
3
3
 
4
4
  program
5
- .description('Getter and watcher for react native storybook')
5
+ .description('Generator for the storybook.requires file used in react native storybook')
6
6
  .option(
7
7
  '-c, --config-path <path>',
8
8
  'The path to your config folder relative to your project-dir',
package/bin/watcher.js DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- require('../scripts/watcher');
@@ -1,211 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`loader writeRequires when there are different file extensions writes the story imports 1`] = `
4
- "
5
- /* do not change this file, it is auto generated by storybook. */
6
-
7
- import { configure, addDecorator, addParameters, addArgsEnhancer } from '@storybook/react-native/V6';
8
-
9
- global.STORIES = [{"titlePrefix":"","directory":"./scripts/mocks/file-extensions","files":"FakeStory.stories.tsx","importPathMatcher":"^\\\\.[\\\\\\\\/](?:scripts\\\\/mocks\\\\/file-extensions\\\\/FakeStory\\\\.stories\\\\.tsx)$"}]
10
-
11
- import "@storybook/addon-ondevice-notes/register";
12
- import "@storybook/addon-ondevice-controls/register";
13
- import "@storybook/addon-ondevice-backgrounds/register";
14
- import "@storybook/addon-ondevice-actions/register";
15
-
16
- import { argsEnhancers } from "@storybook/addon-actions/dist/preview"
17
-
18
-
19
- import { decorators, parameters } from './preview';
20
-
21
- if (decorators) {
22
- decorators.forEach((decorator) => addDecorator(decorator));
23
- }
24
-
25
- if (parameters) {
26
- addParameters(parameters);
27
- }
28
-
29
-
30
-
31
- try {
32
- argsEnhancers.forEach(enhancer => addArgsEnhancer(enhancer));
33
- } catch{}
34
-
35
-
36
-
37
- const getStories=() => {
38
- return {"./scripts/mocks/file-extensions/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
39
- }
40
-
41
- configure(getStories, module, false)
42
-
43
-
44
- "
45
- `;
46
-
47
- exports[`loader writeRequires when there is a configuration object writes the story imports 1`] = `
48
- "
49
- /* do not change this file, it is auto generated by storybook. */
50
-
51
- import { configure, addDecorator, addParameters, addArgsEnhancer } from '@storybook/react-native/V6';
52
-
53
- global.STORIES = [{"titlePrefix":"ComponentsPrefix","files":"**/*.stories.tsx","directory":"./scripts/mocks/configuration-objects/components","importPathMatcher":"^\\\\.[\\\\\\\\/](?:scripts\\\\/mocks\\\\/configuration-objects\\\\/components(?:\\\\/(?!\\\\.)(?:(?:(?!(?:^|\\\\/)\\\\.).)*?)\\\\/|\\\\/|$)(?!\\\\.)(?=.)[^/]*?\\\\.stories\\\\.tsx)$"}]
54
-
55
- import "@storybook/addon-ondevice-notes/register";
56
- import "@storybook/addon-ondevice-controls/register";
57
- import "@storybook/addon-ondevice-backgrounds/register";
58
- import "@storybook/addon-ondevice-actions/register";
59
-
60
- import { argsEnhancers } from "@storybook/addon-actions/dist/preview"
61
-
62
-
63
- import { decorators, parameters } from './preview';
64
-
65
- if (decorators) {
66
- decorators.forEach((decorator) => addDecorator(decorator));
67
- }
68
-
69
- if (parameters) {
70
- addParameters(parameters);
71
- }
72
-
73
-
74
-
75
- try {
76
- argsEnhancers.forEach(enhancer => addArgsEnhancer(enhancer));
77
- } catch{}
78
-
79
-
80
-
81
- const getStories=() => {
82
- return {"./scripts/mocks/configuration-objects/components/FakeStory.stories.tsx": require("./components/FakeStory.stories.tsx")};
83
- }
84
-
85
- configure(getStories, module, false)
86
-
87
-
88
- "
89
- `;
90
-
91
- exports[`loader writeRequires when there is a story glob and exclude paths globs writes the story imports 1`] = `
92
- "
93
- /* do not change this file, it is auto generated by storybook. */
94
-
95
- import { configure, addDecorator, addParameters, addArgsEnhancer } from '@storybook/react-native/V6';
96
-
97
- global.STORIES = [{"titlePrefix":"","directory":"./scripts/mocks/exclude-config-files","files":"**/*.stories.tsx","importPathMatcher":"^\\\\.[\\\\\\\\/](?:scripts\\\\/mocks\\\\/exclude-config-files(?:\\\\/(?!\\\\.)(?:(?:(?!(?:^|\\\\/)\\\\.).)*?)\\\\/|\\\\/|$)(?!\\\\.)(?=.)[^/]*?\\\\.stories\\\\.tsx)$"}]
98
-
99
- import "@storybook/addon-ondevice-notes/register";
100
- import "@storybook/addon-ondevice-controls/register";
101
- import "@storybook/addon-ondevice-backgrounds/register";
102
- import "@storybook/addon-ondevice-actions/register";
103
-
104
- import { argsEnhancers } from "@storybook/addon-actions/dist/preview"
105
-
106
-
107
- import { decorators, parameters } from './preview';
108
-
109
- if (decorators) {
110
- decorators.forEach((decorator) => addDecorator(decorator));
111
- }
112
-
113
- if (parameters) {
114
- addParameters(parameters);
115
- }
116
-
117
-
118
-
119
- try {
120
- argsEnhancers.forEach(enhancer => addArgsEnhancer(enhancer));
121
- } catch{}
122
-
123
-
124
-
125
- const getStories=() => {
126
- return {"./scripts/mocks/exclude-config-files/include-components/FakeStory.stories.tsx": require("./include-components/FakeStory.stories.tsx")};
127
- }
128
-
129
- configure(getStories, module, false)
130
-
131
-
132
- "
133
- `;
134
-
135
- exports[`loader writeRequires when there is a story glob writes the story imports 1`] = `
136
- "
137
- /* do not change this file, it is auto generated by storybook. */
138
-
139
- import { configure, addDecorator, addParameters, addArgsEnhancer } from '@storybook/react-native/V6';
140
-
141
- global.STORIES = [{"titlePrefix":"","directory":"./scripts/mocks/all-config-files","files":"FakeStory.stories.tsx","importPathMatcher":"^\\\\.[\\\\\\\\/](?:scripts\\\\/mocks\\\\/all-config-files\\\\/FakeStory\\\\.stories\\\\.tsx)$"}]
142
-
143
- import "@storybook/addon-ondevice-notes/register";
144
- import "@storybook/addon-ondevice-controls/register";
145
- import "@storybook/addon-ondevice-backgrounds/register";
146
- import "@storybook/addon-ondevice-actions/register";
147
-
148
- import { argsEnhancers } from "@storybook/addon-actions/dist/preview"
149
-
150
-
151
- import { decorators, parameters } from './preview';
152
-
153
- if (decorators) {
154
- decorators.forEach((decorator) => addDecorator(decorator));
155
- }
156
-
157
- if (parameters) {
158
- addParameters(parameters);
159
- }
160
-
161
-
162
-
163
- try {
164
- argsEnhancers.forEach(enhancer => addArgsEnhancer(enhancer));
165
- } catch{}
166
-
167
-
168
-
169
- const getStories=() => {
170
- return {"./scripts/mocks/all-config-files/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
171
- }
172
-
173
- configure(getStories, module, false)
174
-
175
-
176
- "
177
- `;
178
-
179
- exports[`loader writeRequires when there is no preview does not add preview related stuff 1`] = `
180
- "
181
- /* do not change this file, it is auto generated by storybook. */
182
-
183
- import { configure, addDecorator, addParameters, addArgsEnhancer } from '@storybook/react-native/V6';
184
-
185
- global.STORIES = [{"titlePrefix":"","directory":"./scripts/mocks/no-preview","files":"FakeStory.stories.tsx","importPathMatcher":"^\\\\.[\\\\\\\\/](?:scripts\\\\/mocks\\\\/no-preview\\\\/FakeStory\\\\.stories\\\\.tsx)$"}]
186
-
187
- import "@storybook/addon-ondevice-notes/register";
188
- import "@storybook/addon-ondevice-controls/register";
189
- import "@storybook/addon-ondevice-backgrounds/register";
190
- import "@storybook/addon-ondevice-actions/register";
191
-
192
- import { argsEnhancers } from "@storybook/addon-actions/dist/preview"
193
-
194
-
195
-
196
-
197
- try {
198
- argsEnhancers.forEach(enhancer => addArgsEnhancer(enhancer));
199
- } catch{}
200
-
201
-
202
-
203
- const getStories=() => {
204
- return {"./scripts/mocks/no-preview/FakeStory.stories.tsx": require("./FakeStory.stories.tsx")};
205
- }
206
-
207
- configure(getStories, module, false)
208
-
209
-
210
- "
211
- `;
package/scripts/loader.js DELETED
@@ -1,185 +0,0 @@
1
- const path = require('path');
2
- const fs = require('fs');
3
- const glob = require('glob');
4
- const prettier = require('prettier');
5
- const { normalizeStories } = require('@storybook/core/common');
6
- const {
7
- toRequireContext,
8
- getFilePathExtension,
9
- getMain,
10
- ensureRelativePathHasDot,
11
- getPreviewExists,
12
- } = require('./common');
13
-
14
- const cwd = process.cwd();
15
-
16
- // TODO check if we need clearDecorators();
17
-
18
- // we clear decorators as a workaround for global decorators getting infinitely applied on HMR
19
- const previewImports = `
20
- import { decorators, parameters } from './preview';
21
-
22
- if (decorators) {
23
- decorators.forEach((decorator) => addDecorator(decorator));
24
- }
25
-
26
- if (parameters) {
27
- addParameters(parameters);
28
- }
29
- `;
30
-
31
- function normalizeExcludePaths(paths) {
32
- // automatically convert a string to an array of a single string
33
- if (typeof paths === 'string') {
34
- return [paths];
35
- }
36
-
37
- // ensure the paths is an array and if any items exists, they are strings
38
- if (Array.isArray(paths) && paths.every((p) => typeof p === 'string')) {
39
- return paths;
40
- }
41
-
42
- // when the paths aren't a string or an (empty) array of strings, return
43
- return undefined;
44
- }
45
-
46
- function writeRequires({ configPath, absolute = false, v6RequireContext = false }) {
47
- const storybookRequiresLocation = path.resolve(cwd, configPath, 'storybook.requires.js');
48
-
49
- const mainImport = getMain({ configPath });
50
-
51
- const main = mainImport.default ?? mainImport;
52
-
53
- const reactNativeOptions = main.reactNativeOptions;
54
-
55
- const excludePaths = reactNativeOptions && reactNativeOptions.excludePaths;
56
-
57
- const normalizedExcludePaths = normalizeExcludePaths(excludePaths);
58
-
59
- const storiesSpecifiers = normalizeStories(main.stories, {
60
- configDir: configPath,
61
- workingDir: cwd,
62
- });
63
-
64
- let configure = '';
65
-
66
- if (v6RequireContext) {
67
- const contexts = storiesSpecifiers.map((specifier) => {
68
- const { path: p, recursive: r, match: m } = toRequireContext(specifier);
69
-
70
- const pathToStory = ensureRelativePathHasDot(path.relative(configPath, p));
71
-
72
- return `require.context('${pathToStory}', ${r}, ${m})`;
73
- });
74
-
75
- configure = `
76
- const stories = [${contexts.join(',')}];
77
-
78
- configure(stories, module, false)
79
- `;
80
- } else {
81
- const storyRequires = storiesSpecifiers.reduce((acc, specifier) => {
82
- const paths = glob
83
- .sync(specifier.files, {
84
- cwd: path.resolve(cwd, specifier.directory),
85
- absolute,
86
- // default to always ignore (exclude) anything in node_modules
87
- ignore:
88
- normalizedExcludePaths !== undefined ? normalizedExcludePaths : ['**/node_modules'],
89
- })
90
- .map((storyPath) => {
91
- const pathWithDirectory = path.join(specifier.directory, storyPath);
92
-
93
- const requirePath = absolute
94
- ? storyPath
95
- : ensureRelativePathHasDot(path.relative(configPath, pathWithDirectory));
96
-
97
- const absolutePath = absolute ? requirePath : path.resolve(configPath, requirePath);
98
-
99
- const pathRelativeToCwd = path.relative(cwd, absolutePath);
100
-
101
- const normalizePathForWindows = (str) =>
102
- path.sep === '\\' ? str.replace(/\\/g, '/') : str;
103
-
104
- return `"./${normalizePathForWindows(
105
- pathRelativeToCwd
106
- )}": require("${normalizePathForWindows(requirePath)}")`;
107
- });
108
- return [...acc, ...paths];
109
- }, []);
110
-
111
- const path_obj_str = `{${storyRequires.join(',')}}`;
112
-
113
- configure = `
114
- const getStories=() => {
115
- return ${path_obj_str};
116
- }
117
-
118
- configure(getStories, module, false)
119
- `;
120
- }
121
-
122
- fs.writeFileSync(storybookRequiresLocation, '');
123
-
124
- const previewExists = getPreviewExists({ configPath });
125
-
126
- let previewJs = previewExists ? previewImports : '';
127
-
128
- const registerAddons = main.addons?.map((addon) => `import "${addon}/register";`).join('\n');
129
-
130
- let enhancersImport = '';
131
-
132
- let enhancers = '';
133
-
134
- // TODO: implement presets or something similar
135
- if (main.addons?.includes('@storybook/addon-ondevice-actions')) {
136
- enhancersImport = 'import { argsEnhancers } from "@storybook/addon-actions/dist/preview"';
137
-
138
- // try/catch is a temporary fix for https://github.com/storybookjs/react-native/issues/327 until a fix is found
139
- enhancers = `
140
- try {
141
- argsEnhancers.forEach(enhancer => addArgsEnhancer(enhancer));
142
- } catch{}
143
- `;
144
- }
145
-
146
- const normalizedStories = storiesSpecifiers.map((specifier) => ({
147
- ...specifier,
148
- importPathMatcher: specifier.importPathMatcher.source,
149
- }));
150
-
151
- const globalStories = `global.STORIES = ${JSON.stringify(normalizedStories)}`;
152
-
153
- const fileContent = `
154
- /* do not change this file, it is auto generated by storybook. */
155
-
156
- import { configure, addDecorator, addParameters, addArgsEnhancer } from '@storybook/react-native/V6';
157
-
158
- ${globalStories}
159
-
160
- ${registerAddons}
161
-
162
- ${enhancersImport}
163
-
164
- ${previewJs}
165
-
166
- ${enhancers}
167
-
168
- ${configure}
169
-
170
- `;
171
-
172
- const formattedFileContent = prettier.format(fileContent, { parser: 'babel' });
173
-
174
- fs.writeFileSync(storybookRequiresLocation, formattedFileContent, {
175
- encoding: 'utf8',
176
- flag: 'w',
177
- });
178
- }
179
-
180
- module.exports = {
181
- writeRequires,
182
- getMain,
183
- getPreviewExists,
184
- getFilePathExtension,
185
- };
@@ -1,179 +0,0 @@
1
- const path = require('path');
2
- const { writeRequires, getMain, getPreviewExists } = require('./loader');
3
-
4
- let pathMock;
5
- let fileContentMock;
6
-
7
- jest.mock('fs', () => ({
8
- ...jest.requireActual('fs'),
9
- writeFileSync: (filePath, fileContent, opts) => {
10
- pathMock = filePath;
11
- fileContentMock = fileContent;
12
- },
13
- }));
14
-
15
- jest.mock('prettier', () => ({
16
- format(s, opts) {
17
- return s;
18
- },
19
- }));
20
-
21
- describe('loader', () => {
22
- describe('getMain', () => {
23
- it('should return the main js default export as an object', () => {
24
- const main = getMain({ configPath: path.resolve(__dirname, 'mocks/all-config-files') });
25
- expect(main).toEqual({
26
- stories: ['./FakeStory.stories.tsx'],
27
- addons: [
28
- '@storybook/addon-ondevice-notes',
29
- '@storybook/addon-ondevice-controls',
30
- '@storybook/addon-ondevice-backgrounds',
31
- '@storybook/addon-ondevice-actions',
32
- ],
33
- });
34
- });
35
-
36
- it('should also work with relative paths', () => {
37
- // relative from where the command is run
38
- const main = getMain({ configPath: './scripts/mocks/all-config-files' });
39
- expect(main).toEqual({
40
- stories: ['./FakeStory.stories.tsx'],
41
- addons: [
42
- '@storybook/addon-ondevice-notes',
43
- '@storybook/addon-ondevice-controls',
44
- '@storybook/addon-ondevice-backgrounds',
45
- '@storybook/addon-ondevice-actions',
46
- ],
47
- });
48
- });
49
- it('should work for any supported file extension', () => {
50
- const main = getMain({ configPath: './scripts/mocks/file-extensions' });
51
- expect(main).toEqual({
52
- stories: ['./FakeStory.stories.tsx'],
53
- addons: [
54
- '@storybook/addon-ondevice-notes',
55
- '@storybook/addon-ondevice-controls',
56
- '@storybook/addon-ondevice-backgrounds',
57
- '@storybook/addon-ondevice-actions',
58
- ],
59
- });
60
- });
61
- });
62
-
63
- describe('getPreviewExists', () => {
64
- const supportedExtensions = ['js', 'jsx', 'ts', 'tsx'];
65
- describe('when using a relative path', () => {
66
- it('should return true if the preview exists', () => {
67
- supportedExtensions.forEach((ext) => {
68
- expect(getPreviewExists({ configPath: `scripts/mocks/preview-files/${ext}` })).toBe(true);
69
- });
70
- });
71
-
72
- it('should return false if the preview does not exist', () => {
73
- expect(getPreviewExists({ configPath: './scripts/mocks/no-preview' })).toBe(false);
74
- });
75
-
76
- it('should return false if the preview does not match any of supportedExtensions values', () => {
77
- expect(getPreviewExists({ configPath: './scripts/mocks/wrong-extension-preview' })).toBe(
78
- false
79
- );
80
- });
81
- });
82
-
83
- describe('when using an absolute path', () => {
84
- it('should return true if the preview exists', () => {
85
- supportedExtensions.forEach((ext) => {
86
- expect(
87
- getPreviewExists({
88
- configPath: path.resolve(__dirname, `mocks/preview-files/${ext}`),
89
- })
90
- ).toBe(true);
91
- });
92
- });
93
-
94
- it('should return false if the preview does not exist', () => {
95
- expect(getPreviewExists({ configPath: path.resolve(__dirname, 'mocks/no-preview') })).toBe(
96
- false
97
- );
98
- });
99
-
100
- it('should return false if the preview does not match any of supportedExtensions values', () => {
101
- expect(
102
- getPreviewExists({ configPath: path.resolve(__dirname, 'mocks/wrong-extension-preview') })
103
- ).toBe(false);
104
- });
105
- });
106
- });
107
-
108
- describe('writeRequires', () => {
109
- describe('when there is a story glob', () => {
110
- it('writes the story imports', () => {
111
- writeRequires({ configPath: 'scripts/mocks/all-config-files' });
112
- expect(pathMock).toEqual(
113
- path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.js')
114
- );
115
- expect(fileContentMock).toMatchSnapshot();
116
- });
117
- });
118
-
119
- describe('when there are different file extensions', () => {
120
- it('writes the story imports', () => {
121
- writeRequires({ configPath: 'scripts/mocks/file-extensions' });
122
- expect(pathMock).toEqual(
123
- path.resolve(__dirname, 'mocks/file-extensions/storybook.requires.js')
124
- );
125
- expect(fileContentMock).toMatchSnapshot();
126
- });
127
- });
128
-
129
- describe('when there is a story glob and exclude paths globs', () => {
130
- it('writes the story imports', () => {
131
- writeRequires({ configPath: 'scripts/mocks/exclude-config-files' });
132
- expect(pathMock).toEqual(
133
- path.resolve(__dirname, 'mocks/exclude-config-files/storybook.requires.js')
134
- );
135
-
136
- expect(fileContentMock).toContain('include-components/FakeStory.stories.tsx');
137
- expect(fileContentMock).not.toContain('exclude-components/FakeStory.stories.tsx');
138
-
139
- expect(fileContentMock).toMatchSnapshot();
140
- });
141
- });
142
-
143
- describe('when there is no story glob or addons', () => {
144
- it('throws an error', () => {
145
- expect(() => writeRequires({ configPath: 'scripts/mocks/blank-config' })).toThrow();
146
- });
147
- });
148
-
149
- describe('when there is no preview', () => {
150
- it('does not add preview related stuff', () => {
151
- writeRequires({ configPath: 'scripts/mocks/no-preview' });
152
- expect(pathMock).toEqual(path.resolve(__dirname, 'mocks/no-preview/storybook.requires.js'));
153
- expect(fileContentMock).toMatchSnapshot();
154
- });
155
- });
156
-
157
- describe('when the absolute option is true', () => {
158
- it('should write absolute paths to the requires file', () => {
159
- writeRequires({ configPath: 'scripts/mocks/all-config-files', absolute: true });
160
- expect(pathMock).toEqual(
161
- path.resolve(__dirname, 'mocks/all-config-files/storybook.requires.js')
162
- );
163
- expect(fileContentMock).toContain(
164
- path.resolve(__dirname, 'mocks/all-config-files/FakeStory.stories.tsx')
165
- );
166
- });
167
- });
168
-
169
- describe('when there is a configuration object', () => {
170
- it('writes the story imports', () => {
171
- writeRequires({ configPath: 'scripts/mocks/configuration-objects' });
172
- expect(pathMock).toEqual(
173
- path.resolve(__dirname, 'mocks/configuration-objects/storybook.requires.js')
174
- );
175
- expect(fileContentMock).toMatchSnapshot();
176
- });
177
- });
178
- });
179
- });
@@ -1,84 +0,0 @@
1
- const chokidar = require('chokidar');
2
- const path = require('path');
3
-
4
- const { writeRequires, getMain, getFilePathExtension } = require('./loader');
5
-
6
- const { getArguments } = require('./handle-args');
7
-
8
- const args = getArguments();
9
-
10
- if (!args.v6Store) {
11
- console.log(
12
- "in v7 you don't need the watcher anymore, if you are using v6 compat mode then pass the -v6 flag"
13
- );
14
-
15
- process.exit(0);
16
- }
17
-
18
- const log = console.log.bind(console);
19
-
20
- const mainExt = getFilePathExtension(args, 'main');
21
- const previewExt = getFilePathExtension(args, 'preview');
22
-
23
- const watchPaths = [`./main.${mainExt}`];
24
-
25
- if (previewExt) {
26
- watchPaths.push(`./preview.${previewExt}`);
27
- }
28
-
29
- console.log(watchPaths);
30
-
31
- const updateRequires = (event, watchPath) => {
32
- if (typeof watchPath === 'string') {
33
- log(`event ${event} for file ${path.basename(watchPath)}`);
34
- }
35
- writeRequires(args);
36
- };
37
-
38
- const globs = getMain(args).stories;
39
- // directory
40
- // files
41
- const globsStrings = globs.map((g) => {
42
- if (typeof g === 'string') return g;
43
- if (g.directory && g.files) {
44
- return `${g.directory}/${g.files}`;
45
- }
46
- });
47
-
48
- chokidar
49
- .watch(watchPaths, { cwd: args.configPath })
50
- .on('change', (watchPath) => updateRequires('change', watchPath));
51
-
52
- let isReady = false;
53
-
54
- chokidar
55
- .watch(globsStrings, { cwd: args.configPath })
56
- .on('ready', () => {
57
- log('Watcher is ready, performing initial write');
58
- writeRequires(args);
59
- log('Waiting for changes, press r to manually re-write');
60
- isReady = true;
61
- })
62
- .on('add', (watchPath) => {
63
- if (isReady) {
64
- updateRequires('add', watchPath);
65
- }
66
- })
67
- .on('unlink', (watchPath) => {
68
- if (isReady) {
69
- updateRequires('unlink', watchPath);
70
- }
71
- });
72
-
73
- const readline = require('readline');
74
- readline.emitKeypressEvents(process.stdin);
75
- process.stdin.setRawMode(true);
76
- process.stdin.on('keypress', (str, key) => {
77
- if (key.ctrl && key.name === 'c') {
78
- process.exit();
79
- }
80
- if (key.name === 'r') {
81
- log('Detected "r" keypress, rewriting story imports...');
82
- writeRequires(args);
83
- }
84
- });