Skip to content

Commit

Permalink
Merge pull request #11584 from storybookjs/tech/change-sbextract-cli-…
Browse files Browse the repository at this point in the history
…command-to-use-better-api

CHANGE to use getDataForManager over extract api on storyStore
  • Loading branch information
ndelangen authored Jul 21, 2020
2 parents 7f7c871 + 1792927 commit 7ef6f0a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"update-notifier": "^4.0.0"
},
"devDependencies": {
"@storybook/client-api": "6.0.0-rc.12",
"@types/cross-spawn": "^6.0.1",
"@types/inquirer": "^6.5.0",
"@types/puppeteer-core": "^2.0.0",
Expand Down
27 changes: 3 additions & 24 deletions lib/cli/src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,7 @@ const read = async (url: string) => {
const data = JSON.parse(
await page.evaluate(async () => {
// eslint-disable-next-line no-undef
const d = window.__STORYBOOK_STORY_STORE__.extract();

const result = Object.entries(d).reduce(
(acc, [k, v]: [string, any]) => ({
...acc,
[k]: {
...v,
parameters: {
globals: v.parameters.globals,
globalTypes: v.parameters.globalTypes,
options: v.parameters.options,
args: v.parameters.args,
argTypes: v.parameters.argTypes,
framework: v.parameters.framework,
fileName: v.parameters.fileName,
docsOnly: v.parameters.docsOnly,
},
},
}),
{}
);
return JSON.stringify(result, null, 2);
return JSON.stringify(window.__STORYBOOK_STORY_STORE__.getStoriesJsonData(), null, 2);
})
);

Expand Down Expand Up @@ -95,9 +74,9 @@ export async function extract(input: string, targetPath: string) {
if (input && targetPath) {
const [location, exit] = await useLocation(input);

const stories = await read(location);
const data = await read(location);

await writeFile(targetPath, JSON.stringify({ stories }, null, 2));
await writeFile(targetPath, JSON.stringify(data, null, 2));

await exit();
} else {
Expand Down
27 changes: 27 additions & 0 deletions lib/client-api/src/story_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ describe('preview.story_store', () => {
});
});

describe('getStoriesJsonData', () => {
it('produces stories objects with normalized metadata', () => {
const store = new StoryStore({ channel });

store.addGlobalMetadata({ parameters: { global: 'global' }, decorators: [] });

store.addKindMetadata('a', { parameters: { kind: 'kind' }, decorators: [] });

addStoryToStore(store, 'a', '1', () => 0, { story: 'story' });

const { v, globalParameters, kindParameters, stories } = store.getStoriesJsonData();

expect(v).toBe(2);
expect(globalParameters).toEqual({});
expect(kindParameters).toEqual({ a: {} });
expect(kindParameters.a).toEqual({});

expect(Object.keys(stories)).toEqual(['a--1']);
expect(stories['a--1']).toMatchObject({
id: 'a--1',
kind: 'a',
name: '1',
parameters: { __isArgsStory: false },
});
});
});

describe('getRawStory', () => {
it('produces a story with inherited decorators applied', () => {
const store = new StoryStore({ channel });
Expand Down
18 changes: 17 additions & 1 deletion lib/client-api/src/story_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import memoize from 'memoizerific';
import dedent from 'ts-dedent';
import stable from 'stable';
import mapValues from 'lodash/mapValues';
import pick from 'lodash/pick';
import store from 'store2';

import { Channel } from '@storybook/channels';
Expand Down Expand Up @@ -574,6 +575,21 @@ export default class StoryStore {
};
};

getStoriesJsonData = () => {
const value = this.getDataForManager();
const allowed = ['fileName', 'docsOnly', 'framework', '__id', '__isArgsStory'];

return {
v: 2,
globalParameters: pick(value.globalParameters, allowed),
kindParameters: mapValues(value.kindParameters, (v) => pick(v, allowed)),
stories: mapValues(value.stories, (v: any) => ({
...pick(v, ['id', 'name', 'kind', 'story']),
parameters: pick(v.parameters, allowed),
})),
};
};

pushToManager = () => {
if (this._channel) {
// send to the parent frame.
Expand Down Expand Up @@ -603,7 +619,7 @@ export default class StoryStore {
this.getStoriesForKind(kind).map((story) => this.cleanHooks(story.id));
}

// This API is a reimplementation of Storybook's original getStorybook() API.
// This API is a re-implementation of Storybook's original getStorybook() API.
// As such it may not behave *exactly* the same, but aims to. Some notes:
// - It is *NOT* sorted by the user's sort function, but remains sorted in "insertion order"
// - It does not include docs-only stories
Expand Down

0 comments on commit 7ef6f0a

Please sign in to comment.