Skip to content

Commit

Permalink
fix: refactor helpers.ts to register all.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rshen91 committed Jan 13, 2020
1 parent ea9e5f0 commit a75d477
Show file tree
Hide file tree
Showing 148 changed files with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions integration/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import { readdirSync } from 'fs';
import { getStorybook } from '@storybook/react';
import { join, resolve } from 'path';
import { lstatSync, readdirSync } from 'fs';
import { getStorybook, configure } from '@storybook/react';

export interface StoryInfo {
title: string;
Expand All @@ -13,12 +13,33 @@ export interface StoryGroupInfo {
stories: StoryInfo[];
}

function requireAllStories() {
const normalizedPath = join(__dirname, '../stories');
function requireAllStories(basedir: string, directory: string) {
function enumerateFiles(basedir: string, dir: string) {
let result: string[] = [];
readdirSync(join(basedir, dir)).forEach(function(file) {
const relativePath = join(dir, file);
const stats = lstatSync(join(basedir, relativePath));
if (stats.isDirectory()) {
result = result.concat(enumerateFiles(basedir, relativePath));
} else if (/\.tsx$/.test(relativePath)) {
result.push(relativePath);
}
});
return result;
}
const absoluteDirectory = resolve(basedir, directory);

const keys = enumerateFiles(absoluteDirectory, '.');
function requireContext(key: string) {
if (!keys.includes(key)) {
throw new Error(`Cannot find module '${key}'`);
}
const fullKey = require('path').resolve(absoluteDirectory, key);
return require(fullKey);
}

readdirSync(normalizedPath).forEach((file) => {
require(join(normalizedPath, file));
});
requireContext.keys = () => keys;
return requireContext;
}

function encodeString(string: string) {
Expand All @@ -34,8 +55,7 @@ function encodeString(string: string) {
}

export function getStorybookInfo(): StoryGroupInfo[] {
requireAllStories();

configure(requireAllStories(__dirname, '../stories'), module);
return getStorybook()
.filter(({ kind }) => kind)
.map(({ kind: group, stories: storiesRaw }) => {
Expand Down

0 comments on commit a75d477

Please sign in to comment.