Skip to content

Commit

Permalink
feat: global options, storySort
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 3, 2020
1 parent f3fc436 commit 3a55929
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 16 deletions.
16 changes: 10 additions & 6 deletions core/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const configFileNames = [
export interface ConfigrationResult {
config: Configuration;
configPath: string;
configFilePath: string;
}
/**
* return the configration folder from command-line parameters
Expand Down Expand Up @@ -54,12 +55,15 @@ export const loadConfiguration = (
configFileNames.includes(file.toLowerCase()),
);

return configFile
? {
config: require(path.resolve(configPath, configFile)),
configPath,
}
: undefined;
if (configFile) {
const configFilePath = path.resolve(configPath, configFile);
return {
config: require(configFilePath),
configFilePath,
configPath,
};
}
return undefined;
};

/**
Expand Down
10 changes: 9 additions & 1 deletion core/loader/src/replaceSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ export interface StoryPath {
relPath: string;
}

export const replaceSource = (stories: StoryPath[], hashKey: string) => {
export const replaceSource = (
stories: StoryPath[],
configFilePath: string | undefined,
hashKey: string,
) => {
const imports = `
const imports = {};
const configJSON = ${
configFilePath ? `require("${configFilePath}")` : 'undefined'
};
${stories
.map(story => `imports['${story.absPath}'] = require(${story.relPath});`)
.join('\n')}
Expand Down Expand Up @@ -59,6 +66,7 @@ ${stories
const newContent = `
${imports}
${storeConst}
store.config = configJSON;
${loadStories}
${hmr}
${exports}
Expand Down
1 change: 1 addition & 0 deletions core/loader/src/runtimeLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function(content: string) {
);
content = replaceSource(
stories,
config?.configFilePath,
`__COMPILATION_HASH__${params.compilationHash}`,
);
return content;
Expand Down
5 changes: 5 additions & 0 deletions core/loader/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import {
StoriesStore,
StoryComponents,
StoryPackages,
Configuration,
} from '@component-controls/specification';

export interface LoadingStore {
/**
* global configuration from project config file
*/
config?: Configuration;
/**
* global store of packages
*/
Expand Down
9 changes: 9 additions & 0 deletions core/specification/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ export interface Configuration {
* example: [story => <ThemeProvider>{story()}</ThemeProvider>]
*/
decorators?: StoryRenderFn[];
/**
* global options object
*/
options?: {
/**
* story sorting function
*/
storySort?: (a: string, b: string) => number;
};
}
6 changes: 5 additions & 1 deletion core/specification/src/stories.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CodeLocation, PackageInfo, StoryRenderFn } from './utility';
import { StoryComponent } from './components';
import { ComponentControls } from './controls';

import { Configuration } from './configuration';
/**
* an identifier/variable.argument in the source code
*/
Expand Down Expand Up @@ -271,6 +271,10 @@ export interface StoryPackages {
* store of stories information in memory after the loader is applied
*/
export interface StoriesStore {
/**
* global configuration for config file
*/
config?: Configuration;
/**
* list of story files, or groups
*/
Expand Down
6 changes: 5 additions & 1 deletion core/store/src/Store/Store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StoriesStore } from '@component-controls/specification';
import { StoriesStore, Configuration } from '@component-controls/specification';
import { BroadcastChannel } from 'broadcast-channel';
import {
StoreObserver,
Expand Down Expand Up @@ -121,6 +121,10 @@ export class Store implements StoryStore {
const store = this.getStore();
return store ? store.docs : undefined;
};

get config(): Configuration | undefined {
return this.loadedStore?.config;
}
/**
* modify story properties, for example controls values.
* will notify all installed store observers of the changed story.
Expand Down
2 changes: 2 additions & 0 deletions core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const loadStoryStore = (
stores,
packages: loadedPackages,
components: loadedComponents,
config,
} = newStore;

if (stores) {
Expand All @@ -33,6 +34,7 @@ export const loadStoryStore = (
stories: {},
components: {},
packages: {},
config,
};
stores.forEach(s => {
const storeDocs = s.docs;
Expand Down
2 changes: 2 additions & 0 deletions core/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
StoriesStore,
Story,
StoryDocs,
Configuration,
} from '@component-controls/specification';

/**
Expand All @@ -16,6 +17,7 @@ export interface StoryStore {
getStory: (storyId: string) => Story | undefined;
getStoryDoc: (name: string) => StoryDocs | undefined;
getDocs: () => StoryDocs | undefined;
config: Configuration | undefined;
updateStoryProp: (
storyId: string,
propName: string,
Expand Down
21 changes: 21 additions & 0 deletions examples/gatsby/.config/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const categories = ['Introduction', 'Controls','Blocks', 'Editors', 'Components', 'App components', 'Plugins'];
storySort: (a, b) => {
const aKind = a[1].kind.split('/')[0];
const aIndex = categories.findIndex(c => c === aKind);
const bKind = b[1].kind.split('/')[0];
const bIndex = categories.findIndex(c => c === bKind);
return aIndex - bIndex;
},

module.exports = {
stories: [
'../../stories/src/**/*.stories.(js|jsx|tsx|mdx)',
'../../../core/specification/src/stories/**/*.stories.(js|jsx|tsx|mdx)',
'../../../ui/app-components/src/**/*.stories.(js|jsx|tsx|mdx)',
'../../../ui/components/src/**/*.stories.(js|jsx|tsx|mdx)',
],
options: {
storySort: (a, b) => {
const aDoc = a.split('/')[0];
const aIndex = categories.findIndex(c => c === aDoc);
const bDoc = b.split('/')[0];
const bIndex = categories.findIndex(c => c === bDoc);
return aIndex - bIndex;
},
}
};
12 changes: 8 additions & 4 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { compile } from '@component-controls/webpack-compile';
import {
compile,
watch,
CompileProps,
} from '@component-controls/webpack-compile';
import { NodePluginArgs, NodeInput, CreatePagesArgs } from 'gatsby';
import { StoriesStore } from '@component-controls/specification';
import { loadStoryStore } from '@component-controls/store';
Expand All @@ -11,12 +15,12 @@ exports.sourceNodes = async function sourceNodes(
options: LoaderOptions,
) {
const { createNode } = actions;

const { store } = await compile({
const config: CompileProps = {
webPack: options.webpack,
presets: defaultPresets,
configPath: options.configPath,
});
};
const { store } = true ? await watch(config) : await compile(config);
const loadedStore: StoriesStore | undefined = loadStoryStore(store);

if (loadedStore) {
Expand Down
1 change: 0 additions & 1 deletion integrations/storybook/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ module.exports = {
return result;
},
webpackFinal: (config: any = {}, options: PresetOptions = {}) => {
debugger;
const mergedConfig = mergeWebpackConfig(
config,
options?.webpack || defaultRules,
Expand Down
1 change: 0 additions & 1 deletion plugins/axe-plugin/src/typings.d.ts

This file was deleted.

6 changes: 5 additions & 1 deletion ui/app/src/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ export const Sidebar: FC<SidebarProps> = ({ docPath, buttonClass, title }) => {
const { storeProvider } = useContext(BlockContext);
const menuItems = useMemo(() => {
if (storeProvider) {
const { options } = storeProvider.config || {};

const docs: string[] = Object.keys(storeProvider.getDocs() || []);
const menuItems = docs.reduce((acc: MenuItems, doc: string) => {
const sortedDocs =
options && options.storySort ? docs.sort(options.storySort) : docs;
const menuItems = sortedDocs.reduce((acc: MenuItems, doc: string) => {
const levels = doc.split('/');
createMenuItem(levels, levels, acc);
return acc;
Expand Down

0 comments on commit 3a55929

Please sign in to comment.