Skip to content

Commit

Permalink
Add stories via adding to main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Aug 1, 2022
1 parent 346a8ce commit b4a37bf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
1 change: 0 additions & 1 deletion code/addons/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"types": "dist/types/index.d.ts",
"files": [
"dist/**/*",
"!dist/stories/**/*",
"README.md",
"*.js",
"*.d.ts"
Expand Down
4 changes: 1 addition & 3 deletions code/renderers/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"types": "dist/index.d.ts",
"files": [
"dist/**/*",
"!dist/stories/**/*",
"types/**/*",
"README.md",
"*.js",
Expand Down Expand Up @@ -98,8 +97,7 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/config.ts",
"./src/stories/decorators.stories.tsx"
"./src/config.ts"
],
"platform": "browser"
},
Expand Down
46 changes: 22 additions & 24 deletions scripts/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,24 @@ const addPackageScripts = async ({
await writeJSON(packageJsonPath, packageJson, { spaces: 2 });
};

async function copyStories(possibleFrom: string[], to: string) {
await Promise.all(
possibleFrom.map(async (from) => {
if (!(await pathExists(from))) return;
async function readMainConfig({ cwd }: { cwd: string }) {
const configDir = path.join(cwd, '.storybook');
const mainConfigPath = getInterpretedFile(path.resolve(configDir, 'main'));
return readConfig(mainConfigPath);
}

async function addStories(paths: string[], { cwd }: { cwd: string }) {
const mainConfig = await readMainConfig({ cwd });

const stories = mainConfig.getFieldValue(['stories']) as string[];
const extraStories = paths.map((p) => path.resolve(p, './template/stories/*.stories.*'));
mainConfig.setFieldValue(['stories'], [...stories, ...extraStories]);

const code = '(c) => ({ ...c, rules: { ...c.rules, } })';
// @ts-ignore (not sure why TS complains here, it does exist)
mainConfig.setFieldNode(['webpackFinal'], babelParse(code).program.body[0].expression);

await ensureSymlink(from, to);
})
);
await writeConfig(mainConfig);
}

async function main() {
Expand Down Expand Up @@ -189,21 +199,12 @@ async function main() {
dryRun,
});

// TODO -- this seems like it might be framework specific. Should we search for a folder?
// or set it in the config somewhere?
const storiesDir = path.resolve(cwd, './stories');

// TODO -- can we get the options type to return something more specific
const renderer = renderersMap[framework as 'react' | 'angular'];
const isTS = isTSMap[framework as 'react' | 'angular'];
const possiblePackageStoriesDir = isTS ? ['src/stories'] : ['dist/stories', 'dist/esm/stories'];

// Copy over renderer stories
const rendererDir = path.join(codeDir, 'renderers', renderer);
await copyStories(
possiblePackageStoriesDir.map((dir) => path.join(rendererDir, dir)),
path.join(storiesDir, renderer)
);
const storiesToAdd = [] as string[];
storiesToAdd.push(path.join(codeDir, 'renderers', renderer));

// TODO -- sb add <addon> doesn't actually work properly:
// - installs in `deps` not `devDeps`
Expand All @@ -216,14 +217,11 @@ async function main() {
}

for (const addon of [...defaultAddons, ...optionValues.addon]) {
const addonStoriesDir = path.join(codeDir, 'addons', addon);

await copyStories(
possiblePackageStoriesDir.map((dir) => path.join(addonStoriesDir, dir)),
path.join(storiesDir, `addon-${addon}`)
);
storiesToAdd.push(path.join(codeDir, 'addons', addon));
}

await addStories(storiesToAdd, { cwd });

if (link) {
await executeCLIStep(steps.link, {
argument: cwd,
Expand Down

0 comments on commit b4a37bf

Please sign in to comment.