Skip to content

Commit

Permalink
feat: loader chalk log
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 2, 2020
1 parent 340d29b commit f3fc436
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
8 changes: 5 additions & 3 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import * as fs from 'fs';
import * as path from 'path';

import * as chalk from 'chalk';
import { getOptions } from 'loader-utils';
import { loader } from 'webpack';
import {
InstrumentOptions,
parseStories,
} from '@component-controls/instrument';

import { addStoriesDoc } from './store';
import { reserveStoriesDoc, addStoriesDoc } from './store';

module.exports.pitch = async function() {
const options: InstrumentOptions = getOptions(this) || {};
const context = this as loader.LoaderContext;
const filePath = this.resource;
reserveStoriesDoc(filePath);
const source = fs.readFileSync(filePath, 'utf8');
const { transformed, ...store } = await parseStories(
source,
Expand All @@ -23,7 +24,8 @@ module.exports.pitch = async function() {
if (store) {
const relPath = path.relative(context.rootContext, filePath);
const moduleId = relPath.startsWith('.') ? relPath : `./${relPath}`;
addStoriesDoc({
console.log(chalk.bgRgb(244, 147, 66)('@loaded: '), filePath);
addStoriesDoc(filePath, {
stories: store.stories,
components: store.components,
packages: store.packages,
Expand Down
16 changes: 12 additions & 4 deletions core/loader/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,30 @@ export interface LoadingStore {
/**
* stores, loaded from each .stories.* file
*/
stores: Pick<StoriesStore, 'stories' | 'docs'>[];
stores: (Partial<Pick<StoriesStore, 'stories' | 'docs'>> & {
filePath: string;
})[];
}
export const store: LoadingStore = {
stores: [],
components: {},
packages: {},
};

export const addStoriesDoc = async (added: StoriesStore) => {
export const reserveStoriesDoc = (filePath: string) => {
store.stores.push({ filePath });
};
export const addStoriesDoc = (filePath: string, added: StoriesStore) => {
const { components, packages, stories, docs } = added;
Object.keys(components).forEach(key => {
store.components[key] = components[key];
});
Object.keys(packages).forEach(key => {
store.packages[key] = packages[key];
});

store.stores.push({ stories, docs });
const storeStore = store.stores.find(s => s.filePath === filePath);
if (storeStore) {
storeStore.stories = stories;
storeStore.docs = docs;
}
};
17 changes: 12 additions & 5 deletions core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ export const loadStoryStore = (
packages: {},
};
stores.forEach(s => {
if (Object.keys(s.docs).length > 0) {
Object.keys(s.docs).forEach(docName => {
const doc = s.docs[docName];
const storeDocs = s.docs;
const storeStories = s.stories;
if (
storeDocs &&
storeStories &&
s.stories &&
Object.keys(storeDocs).length > 0
) {
Object.keys(storeDocs).forEach(docName => {
const doc = storeDocs[docName];
globalStore.docs[docName] = doc;
Object.keys(s.stories).forEach(storyName => {
const story: Story = s.stories[storyName];
Object.keys(storeStories).forEach((storyName: string) => {
const story: Story = storeStories[storyName];
const {
title,
stories,
Expand Down
16 changes: 0 additions & 16 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,6 @@ exports.sourceNodes = async function sourceNodes(
const nodeDoc = Object.assign({}, doc, docMetadata);
createNode(nodeDoc);
});
Object.keys(loadedStore.stories).forEach(storyId => {
//@ts-ignore
const story = loadedStore.stories[storyId];
const storyMetadata: NodeInput = {
id: storyId,
children: [],
internal: {
type: 'story',
content: JSON.stringify(story),
contentDigest: createContentDigest(story),
},
};

const nodeStory = Object.assign({}, story, storyMetadata);
createNode(nodeStory);
});
}
};

Expand Down

0 comments on commit f3fc436

Please sign in to comment.