From 9eca8337079d2ef966b559fd3a7236aa9557aa21 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 6 Nov 2017 00:52:36 +0900 Subject: [PATCH] Refactor StoryStore into @storybook/core --- app/angular/package.json | 1 + app/angular/src/client/preview/index.js | 2 +- app/angular/src/client/preview/story_store.js | 89 ----------------- app/react-native/package.json | 1 + app/react-native/src/preview/index.js | 2 +- app/react-native/src/preview/story_store.js | 98 ------------------ app/react/package.json | 1 + app/react/src/client/preview/index.js | 2 +- app/vue/package.json | 1 + app/vue/src/client/preview/index.js | 2 +- app/vue/src/client/preview/story_store.js | 99 ------------------- lib/core/README.md | 13 +++ lib/core/client.js | 2 + lib/core/package.json | 22 +++++ lib/core/src/client/index.js | 3 + lib/core/src/client/preview/index.js | 3 + .../core}/src/client/preview/story_store.js | 0 17 files changed, 51 insertions(+), 290 deletions(-) delete mode 100644 app/angular/src/client/preview/story_store.js delete mode 100644 app/react-native/src/preview/story_store.js delete mode 100644 app/vue/src/client/preview/story_store.js create mode 100644 lib/core/README.md create mode 100644 lib/core/client.js create mode 100644 lib/core/package.json create mode 100644 lib/core/src/client/index.js create mode 100644 lib/core/src/client/preview/index.js rename {app/react => lib/core}/src/client/preview/story_store.js (100%) diff --git a/app/angular/package.json b/app/angular/package.json index b97c12815ea9..4fe2e5218c45 100644 --- a/app/angular/package.json +++ b/app/angular/package.json @@ -30,6 +30,7 @@ "@storybook/addon-actions": "^3.3.0-alpha.2", "@storybook/addon-links": "^3.3.0-alpha.2", "@storybook/addons": "^3.3.0-alpha.2", + "@storybook/core": "^3.3.0-alpha.2", "@storybook/channel-postmessage": "^3.3.0-alpha.2", "@storybook/ui": "^3.3.0-alpha.2", "airbnb-js-shims": "^1.1.1", diff --git a/app/angular/src/client/preview/index.js b/app/angular/src/client/preview/index.js index 085bd1824aae..75088b24be6c 100644 --- a/app/angular/src/client/preview/index.js +++ b/app/angular/src/client/preview/index.js @@ -3,7 +3,7 @@ import { createStore } from 'redux'; import addons from '@storybook/addons'; import createChannel from '@storybook/channel-postmessage'; import qs from 'qs'; -import StoryStore from './story_store'; +import { StoryStore } from '@storybook/core/client'; import ClientApi from './client_api'; import ConfigApi from './config_api'; import render from './render'; diff --git a/app/angular/src/client/preview/story_store.js b/app/angular/src/client/preview/story_store.js deleted file mode 100644 index 99ebbc02f740..000000000000 --- a/app/angular/src/client/preview/story_store.js +++ /dev/null @@ -1,89 +0,0 @@ -/* eslint-disable no-underscore-dangle */ - -let count = 0; - -function getId() { - count += 1; - return count; -} - -export default class StoryStore { - constructor() { - this._data = {}; - } - - addStory(kind, name, fn) { - if (!this._data[kind]) { - this._data[kind] = { - kind, - index: getId(), - stories: {}, - }; - } - - this._data[kind].stories[name] = { - name, - index: getId(), - fn, - }; - } - - getStoryKinds() { - return Object.keys(this._data) - .map(key => this._data[key]) - .filter(kind => Object.keys(kind.stories).length > 0) - .sort((info1, info2) => info1.index - info2.index) - .map(info => info.kind); - } - - getStories(kind) { - if (!this._data[kind]) { - return []; - } - - return Object.keys(this._data[kind].stories) - .map(name => this._data[kind].stories[name]) - .sort((info1, info2) => info1.index - info2.index) - .map(info => info.name); - } - - getStory(kind, name) { - const storiesKind = this._data[kind]; - if (!storiesKind) { - return null; - } - - const storyInfo = storiesKind.stories[name]; - if (!storyInfo) { - return null; - } - - return storyInfo.fn; - } - - removeStoryKind(kind) { - this._data[kind].stories = {}; - } - - hasStoryKind(kind) { - return Boolean(this._data[kind]); - } - - hasStory(kind, name) { - return Boolean(this.getStory(kind, name)); - } - - dumpStoryBook() { - const data = this.getStoryKinds().map(kind => ({ kind, stories: this.getStories(kind) })); - - return data; - } - - size() { - return Object.keys(this._data).length; - } - - clean() { - this.getStoryKinds().forEach(kind => delete this._data[kind]); - } -} diff --git a/app/react-native/package.json b/app/react-native/package.json index cdeb682288d2..03281848555e 100644 --- a/app/react-native/package.json +++ b/app/react-native/package.json @@ -28,6 +28,7 @@ "@storybook/addon-links": "^3.3.0-alpha.2", "@storybook/addons": "^3.3.0-alpha.2", "@storybook/channel-websocket": "^3.3.0-alpha.2", + "@storybook/core": "^3.3.0-alpha.2", "@storybook/ui": "^3.3.0-alpha.2", "autoprefixer": "^7.1.6", "babel-core": "^6.26.0", diff --git a/app/react-native/src/preview/index.js b/app/react-native/src/preview/index.js index 2529253e331c..40445fa897b5 100644 --- a/app/react-native/src/preview/index.js +++ b/app/react-native/src/preview/index.js @@ -6,7 +6,7 @@ import parse from 'url-parse'; import addons from '@storybook/addons'; import createChannel from '@storybook/channel-websocket'; import { EventEmitter } from 'events'; -import StoryStore from './story_store'; +import { StoryStore } from '@storybook/core/client'; import StoryKindApi from './story_kind'; import OnDeviceUI from './components/OnDeviceUI'; import StoryView from './components/StoryView'; diff --git a/app/react-native/src/preview/story_store.js b/app/react-native/src/preview/story_store.js deleted file mode 100644 index f202ab8c6b2b..000000000000 --- a/app/react-native/src/preview/story_store.js +++ /dev/null @@ -1,98 +0,0 @@ -/* eslint no-underscore-dangle: 0 */ -import { EventEmitter } from 'events'; - -let count = 0; - -export default class StoryStore extends EventEmitter { - constructor() { - super(); - this._data = {}; - } - - addStory(kind, name, fn, fileName) { - count += 1; - if (!this._data[kind]) { - this._data[kind] = { - kind, - fileName, - index: count, - stories: {}, - }; - } - - this._data[kind].stories[name] = { - name, - index: count, - fn, - }; - - this.emit('storyAdded', kind, name, fn); - } - - getStoryKinds() { - return Object.keys(this._data) - .map(key => this._data[key]) - .sort((info1, info2) => info1.index - info2.index) - .map(info => info.kind); - } - - getStories(kind) { - if (!this._data[kind]) { - return []; - } - - return Object.keys(this._data[kind].stories) - .map(name => this._data[kind].stories[name]) - .sort((info1, info2) => info1.index - info2.index) - .map(info => info.name); - } - - getStoryFileName(kind) { - const storiesKind = this._data[kind]; - if (!storiesKind) { - return null; - } - - return storiesKind.fileName; - } - - getStory(kind, name) { - const storiesKind = this._data[kind]; - if (!storiesKind) { - return null; - } - - const storyInfo = storiesKind.stories[name]; - if (!storyInfo) { - return null; - } - - return storyInfo.fn; - } - - removeStoryKind(kind) { - delete this._data[kind]; - } - - hasStoryKind(kind) { - return Boolean(this._data[kind]); - } - - hasStory(kind, name) { - return Boolean(this.getStory(kind, name)); - } - - dumpStoryBook() { - const data = this.getStoryKinds().map(kind => ({ kind, stories: this.getStories(kind) })); - - return data; - } - - size() { - return Object.keys(this._data).length; - } - - clean() { - this.getStoryKinds().forEach(kind => delete this._data[kind]); - } -} diff --git a/app/react/package.json b/app/react/package.json index b60fee00853b..dda6a19f6985 100644 --- a/app/react/package.json +++ b/app/react/package.json @@ -26,6 +26,7 @@ "@storybook/addon-links": "^3.3.0-alpha.2", "@storybook/addons": "^3.3.0-alpha.2", "@storybook/channel-postmessage": "^3.3.0-alpha.2", + "@storybook/core": "^3.3.0-alpha.2", "@storybook/ui": "^3.3.0-alpha.2", "airbnb-js-shims": "^1.3.0", "autoprefixer": "^7.1.6", diff --git a/app/react/src/client/preview/index.js b/app/react/src/client/preview/index.js index 60508441eeee..e76364259832 100644 --- a/app/react/src/client/preview/index.js +++ b/app/react/src/client/preview/index.js @@ -4,7 +4,7 @@ import { createStore } from 'redux'; import addons from '@storybook/addons'; import createChannel from '@storybook/channel-postmessage'; import qs from 'qs'; -import StoryStore from './story_store'; +import { StoryStore } from '@storybook/core/client'; import ClientApi from './client_api'; import ConfigApi from './config_api'; import render from './render'; diff --git a/app/vue/package.json b/app/vue/package.json index dad0b8ce625a..a4ca2adfc76f 100644 --- a/app/vue/package.json +++ b/app/vue/package.json @@ -26,6 +26,7 @@ "@storybook/addon-links": "^3.3.0-alpha.2", "@storybook/addons": "^3.3.0-alpha.2", "@storybook/channel-postmessage": "^3.3.0-alpha.2", + "@storybook/core": "^3.3.0-alpha.2", "@storybook/ui": "^3.3.0-alpha.2", "airbnb-js-shims": "^1.3.0", "autoprefixer": "^7.1.6", diff --git a/app/vue/src/client/preview/index.js b/app/vue/src/client/preview/index.js index 60508441eeee..e76364259832 100644 --- a/app/vue/src/client/preview/index.js +++ b/app/vue/src/client/preview/index.js @@ -4,7 +4,7 @@ import { createStore } from 'redux'; import addons from '@storybook/addons'; import createChannel from '@storybook/channel-postmessage'; import qs from 'qs'; -import StoryStore from './story_store'; +import { StoryStore } from '@storybook/core/client'; import ClientApi from './client_api'; import ConfigApi from './config_api'; import render from './render'; diff --git a/app/vue/src/client/preview/story_store.js b/app/vue/src/client/preview/story_store.js deleted file mode 100644 index a82bba34d24d..000000000000 --- a/app/vue/src/client/preview/story_store.js +++ /dev/null @@ -1,99 +0,0 @@ -/* eslint no-underscore-dangle: 0 */ - -let count = 0; - -function getId() { - count += 1; - return count; -} - -export default class StoryStore { - constructor() { - this._data = {}; - } - - addStory(kind, name, fn, fileName) { - if (!this._data[kind]) { - this._data[kind] = { - kind, - fileName, - index: getId(), - stories: {}, - }; - } - - this._data[kind].stories[name] = { - name, - index: getId(), - fn, - }; - } - - getStoryKinds() { - return Object.keys(this._data) - .map(key => this._data[key]) - .filter(kind => Object.keys(kind.stories).length > 0) - .sort((info1, info2) => info1.index - info2.index) - .map(info => info.kind); - } - - getStories(kind) { - if (!this._data[kind]) { - return []; - } - - return Object.keys(this._data[kind].stories) - .map(name => this._data[kind].stories[name]) - .sort((info1, info2) => info1.index - info2.index) - .map(info => info.name); - } - - getStoryFileName(kind) { - const storiesKind = this._data[kind]; - if (!storiesKind) { - return null; - } - - return storiesKind.fileName; - } - - getStory(kind, name) { - const storiesKind = this._data[kind]; - if (!storiesKind) { - return null; - } - - const storyInfo = storiesKind.stories[name]; - if (!storyInfo) { - return null; - } - - return storyInfo.fn; - } - - removeStoryKind(kind) { - this._data[kind].stories = {}; - } - - hasStoryKind(kind) { - return Boolean(this._data[kind]); - } - - hasStory(kind, name) { - return Boolean(this.getStory(kind, name)); - } - - dumpStoryBook() { - const data = this.getStoryKinds().map(kind => ({ kind, stories: this.getStories(kind) })); - - return data; - } - - size() { - return Object.keys(this._data).length; - } - - clean() { - this.getStoryKinds().forEach(kind => delete this._data[kind]); - } -} diff --git a/lib/core/README.md b/lib/core/README.md new file mode 100644 index 000000000000..5ae08249a8ee --- /dev/null +++ b/lib/core/README.md @@ -0,0 +1,13 @@ +# Storybook for React + +[![Greenkeeper badge](https://badges.greenkeeper.io/storybooks/storybook.svg)](https://greenkeeper.io/) +[![Build Status](https://travis-ci.org/storybooks/storybook.svg?branch=master)](https://travis-ci.org/storybooks/storybook) +[![CodeFactor](https://www.codefactor.io/repository/github/storybooks/storybook/badge)](https://www.codefactor.io/repository/github/storybooks/storybook) +[![Known Vulnerabilities](https://snyk.io/test/github/storybooks/storybook/8f36abfd6697e58cd76df3526b52e4b9dc894847/badge.svg)](https://snyk.io/test/github/storybooks/storybook/8f36abfd6697e58cd76df3526b52e4b9dc894847) +[![BCH compliance](https://bettercodehub.com/edge/badge/storybooks/storybook)](https://bettercodehub.com/results/storybooks/storybook) [![codecov](https://codecov.io/gh/storybooks/storybook/branch/master/graph/badge.svg)](https://codecov.io/gh/storybooks/storybook) +[![Storybook Slack](https://storybooks-slackin.herokuapp.com/badge.svg)](https://storybooks-slackin.herokuapp.com/) + +This package contains common data structures used among the different frameworks +(React, RN, Vue, Angular, etc). + +FIXME diff --git a/lib/core/client.js b/lib/core/client.js new file mode 100644 index 000000000000..6de6d9128855 --- /dev/null +++ b/lib/core/client.js @@ -0,0 +1,2 @@ +/* eslint-disable global-require */ +module.exports = require('./dist/client').default; diff --git a/lib/core/package.json b/lib/core/package.json new file mode 100644 index 000000000000..70f98d5a6a21 --- /dev/null +++ b/lib/core/package.json @@ -0,0 +1,22 @@ +{ + "name": "@storybook/core", + "version": "3.3.0-alpha.2", + "description": "Storybook framework-agnostic API", + "homepage": "https://github.com/storybooks/storybook/tree/master/lib/core", + "bugs": { + "url": "https://github.com/storybooks/storybook/issues" + }, + "license": "MIT", + "main": "dist/client/index.js", + "repository": { + "type": "git", + "url": "https://github.com/storybooks/storybook.git" + }, + "scripts": { + "dev": "DEV_BUILD=1 nodemon --watch ./src --exec 'yarn prepare'", + "prepare": "node ../../scripts/prepare.js" + }, + "devDependencies": { + "babel-cli": "^6.26.0" + } +} diff --git a/lib/core/src/client/index.js b/lib/core/src/client/index.js new file mode 100644 index 000000000000..7cec7d05ee95 --- /dev/null +++ b/lib/core/src/client/index.js @@ -0,0 +1,3 @@ +import preview from './preview'; + +export default preview; diff --git a/lib/core/src/client/preview/index.js b/lib/core/src/client/preview/index.js new file mode 100644 index 000000000000..8892fdd3d07e --- /dev/null +++ b/lib/core/src/client/preview/index.js @@ -0,0 +1,3 @@ +import StoryStore from './story_store'; + +export default { StoryStore }; diff --git a/app/react/src/client/preview/story_store.js b/lib/core/src/client/preview/story_store.js similarity index 100% rename from app/react/src/client/preview/story_store.js rename to lib/core/src/client/preview/story_store.js