Skip to content

Commit

Permalink
Cherry pick new template logic from next
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed May 22, 2023
1 parent e563909 commit 7100975
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,17 @@ function addStoriesEntry(mainConfig: ConfigFile, path: string) {
mainConfig.setFieldValue(['stories'], [...stories, entry]);
}

function addVariantToFolder(variant?: string, folder = 'stories') {
function getStoriesFolderWithVariant(variant?: string, folder = 'stories') {
return variant ? `${folder}_${variant}` : folder;
}

// packageDir is eg 'renderers/react', 'addons/actions'
async function linkPackageStories(
packageDir: string,
{ mainConfig, cwd, linkInDir }: { mainConfig: ConfigFile; cwd: string; linkInDir?: string },
frameworkVariant?: string
variant?: string
) {
const storiesFolderName = frameworkVariant ? addVariantToFolder(frameworkVariant) : 'stories';
const storiesFolderName = variant ? getStoriesFolderWithVariant(variant) : 'stories';
const source = join(CODE_DIRECTORY, packageDir, 'template', storiesFolderName);
// By default we link `stories` directories
// e.g '../../../code/lib/store/template/stories' to 'template-stories/lib/store'
Expand All @@ -299,10 +299,7 @@ async function linkPackageStories(
// The files must be linked in the cwd, in order to ensure that any dependencies they
// reference are resolved in the cwd. In particular 'react' resolved by MDX files.
const target = linkInDir
? resolve(
linkInDir,
frameworkVariant ? addVariantToFolder(frameworkVariant, packageDir) : packageDir
)
? resolve(linkInDir, variant ? getStoriesFolderWithVariant(variant, packageDir) : packageDir)
: resolve(cwd, 'template-stories', packageDir);

await ensureSymlink(source, target);
Expand Down Expand Up @@ -373,6 +370,9 @@ export const addStories: Task['run'] = async (
template.expected.renderer.startsWith('@storybook/') &&
template.expected.renderer !== '@storybook/server';

const sandboxSpecificStoriesFolder = key.replaceAll('/', '-');
const storiesVariantFolder = getStoriesFolderWithVariant(sandboxSpecificStoriesFolder);

if (isCoreRenderer) {
// Link in the template/components/index.js from store, the renderer and the addons
const rendererPath = await workspacePath('renderer', template.expected.renderer);
Expand All @@ -388,6 +388,22 @@ export const addStories: Task['run'] = async (
cwd,
linkInDir: resolve(cwd, storiesPath),
});

if (
await pathExists(
resolve(CODE_DIRECTORY, rendererPath, join('template', storiesVariantFolder))
)
) {
await linkPackageStories(
rendererPath,
{
mainConfig,
cwd,
linkInDir: resolve(cwd, storiesPath),
},
sandboxSpecificStoriesFolder
);
}
}

const isCoreFramework = template.expected.framework.startsWith('@storybook/');
Expand All @@ -404,8 +420,7 @@ export const addStories: Task['run'] = async (
});
}

const frameworkVariant = key.split('/')[1];
const storiesVariantFolder = addVariantToFolder(frameworkVariant);
console.log({ sandboxSpecificStoriesFolder, storiesVariantFolder });

if (
await pathExists(
Expand All @@ -419,7 +434,7 @@ export const addStories: Task['run'] = async (
cwd,
linkInDir: resolve(cwd, storiesPath),
},
frameworkVariant
sandboxSpecificStoriesFolder
);
}
}
Expand Down

0 comments on commit 7100975

Please sign in to comment.