Skip to content

Commit

Permalink
Merge pull request #415 from kadirahq/datastore
Browse files Browse the repository at this point in the history
Add database support for addons
  • Loading branch information
Muhammed Thanish authored Sep 1, 2016
2 parents 0806ca6 + 014fc27 commit 4b30879
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
11 changes: 10 additions & 1 deletion dist/client/manager/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var _storybookChannelPagebus = require('@kadira/storybook-channel-pagebus');

var _storybookChannelPagebus2 = _interopRequireDefault(_storybookChannelPagebus);

var _storybookDatabaseLocal = require('@kadira/storybook-database-local');

var _storybookDatabaseLocal2 = _interopRequireDefault(_storybookDatabaseLocal);

var _preview = require('./preview');

var _preview2 = _interopRequireDefault(_preview);
Expand All @@ -58,11 +62,16 @@ var ReactProvider = function (_Provider) {
function ReactProvider() {
(0, _classCallCheck3.default)(this, ReactProvider);

var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(ReactProvider).call(this));
var _this = (0, _possibleConstructorReturn3.default)(this, (ReactProvider.__proto__ || (0, _getPrototypeOf2.default)(ReactProvider)).call(this));

_this.dataId = _uuid2.default.v4();
_this.channel = (0, _storybookChannelPagebus2.default)({ key: _this.dataId });
_storybookAddons2.default.setChannel(_this.channel);
_this.database = _storybookAddons2.default.getDatabase();
if (!_this.database) {
_this.database = (0, _storybookDatabaseLocal2.default)({ url: location.origin + '/db' });
_storybookAddons2.default.setDatabase(_this.database);
}
return _this;
}

Expand Down
13 changes: 12 additions & 1 deletion dist/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var _middleware = require('./middleware');

var _middleware2 = _interopRequireDefault(_middleware);

var _middleware3 = require('@kadira/storybook-database-local/dist/server/middleware');

var _middleware4 = _interopRequireDefault(_middleware3);

var _package = require('../../package.json');

var _package2 = _interopRequireDefault(_package);
Expand All @@ -33,7 +37,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

var logger = console;

_commander2.default.version(_package2.default.version).option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt).option('-h, --host [string]', 'Host to run Storybook').option('-s, --static-dir <dir-names>', 'Directory where to load static files from', _utils.parseList).option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from').option('--dont-track', 'Do not send anonymous usage stats.').parse(process.argv);
_commander2.default.version(_package2.default.version).option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt).option('-h, --host [string]', 'Host to run Storybook').option('-s, --static-dir <dir-names>', 'Directory where to load static files from', _utils.parseList).option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from').option('-d, --db-path [db-file]', 'File where to store addon database JSON file').option('--enable-db', 'Enable the (experimental) addon database service on dev-server').option('--dont-track', 'Do not send anonymous usage stats.').parse(process.argv);

// The key is the field created in `program` variable for
// each command line argument. Value is the env variable.
Expand Down Expand Up @@ -81,6 +85,13 @@ if (_commander2.default.staticDir) {
var configDir = _commander2.default.configDir || './.storybook';
app.use((0, _middleware2.default)(configDir));

// The addon database service is disabled by default for now
// It should be enabled with the --enable-db for dev server
if (_commander2.default.enableDb) {
var dbPath = _commander2.default.dbPath || './.storybook/addon-db.json';
app.use('/db', (0, _middleware4.default)(dbPath));
}

app.listen.apply(app, listenAddr.concat([function (error) {
if (error) {
throw error;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"@kadira/react-split-pane": "^1.4.0",
"@kadira/storybook-addon-actions": "^1.0.2",
"@kadira/storybook-addon-links": "^1.0.0",
"@kadira/storybook-addons": "^1.3.1",
"@kadira/storybook-addons": "^1.5.0",
"@kadira/storybook-channel-pagebus": "^2.0.2",
"@kadira/storybook-database-local": "^1.1.0",
"@kadira/storybook-ui": "^3.3.2",
"autoprefixer": "^6.3.7",
"babel-core": "^6.11.4",
Expand Down
6 changes: 6 additions & 0 deletions src/client/manager/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import { Provider } from '@kadira/storybook-ui';
import addons from '@kadira/storybook-addons';
import createChannel from '@kadira/storybook-channel-pagebus';
import createDatabase from '@kadira/storybook-database-local';
import Preview from './preview';

export default class ReactProvider extends Provider {
Expand All @@ -12,6 +13,11 @@ export default class ReactProvider extends Provider {
this.dataId = UUID.v4();
this.channel = createChannel({ key: this.dataId });
addons.setChannel(this.channel);
this.database = addons.getDatabase();
if (!this.database) {
this.database = createDatabase({ url: `${location.origin}/db` });
addons.setDatabase(this.database);
}
}

getPanels() {
Expand Down
10 changes: 10 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import program from 'commander';
import path from 'path';
import fs from 'fs';
import storybook from './middleware';
import datastore from '@kadira/storybook-database-local/dist/server/middleware';
import packageJson from '../../package.json';
import { parseList, getEnvConfig } from './utils';
import { track, dontTrack } from './track_usage';
Expand All @@ -17,6 +18,8 @@ program
.option('-h, --host [string]', 'Host to run Storybook')
.option('-s, --static-dir <dir-names>', 'Directory where to load static files from', parseList)
.option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from')
.option('-d, --db-path [db-file]', 'File where to store addon database JSON file')
.option('--enable-db', 'Enable the (experimental) addon database service on dev-server')
.option('--dont-track', 'Do not send anonymous usage stats.')
.parse(process.argv);

Expand Down Expand Up @@ -66,6 +69,13 @@ if (program.staticDir) {
const configDir = program.configDir || './.storybook';
app.use(storybook(configDir));

// The addon database service is disabled by default for now
// It should be enabled with the --enable-db for dev server
if (program.enableDb) {
const dbPath = program.dbPath || './.storybook/addon-db.json';
app.use('/db', datastore(dbPath));
}

app.listen(...listenAddr, function (error) {
if (error) {
throw error;
Expand Down

0 comments on commit 4b30879

Please sign in to comment.