Skip to content

Commit

Permalink
Merge pull request #18950 from storybookjs/sandbox-from-local-repro
Browse files Browse the repository at this point in the history
Sandbox: Add ability to run from local repro
  • Loading branch information
yannbf authored Aug 17, 2022
2 parents 09ba03a + 2322310 commit c503691
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions scripts/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ensureSymlink,
ensureDir,
existsSync,
copy,
} from 'fs-extra';
import prompts from 'prompts';
import type { AbortController } from 'node-abort-controller';
Expand All @@ -22,6 +23,7 @@ import { ConfigFile, readConfig, writeConfig } from '../code/lib/csf-tools';
import { babelParse } from '../code/lib/csf-tools/src/babelParse';
import TEMPLATES from '../code/lib/cli/src/repro-templates';
import { servePackages } from './utils/serve-packages';
import dedent from 'ts-dedent';

type Template = keyof typeof TEMPLATES;
const templates: Template[] = Object.keys(TEMPLATES) as any;
Expand All @@ -41,6 +43,7 @@ const defaultAddons = [
];
const sandboxDir = path.resolve(__dirname, '../sandbox');
const codeDir = path.resolve(__dirname, '../code');
const reprosDir = path.resolve(__dirname, '../repros');

export const options = createOptions({
template: {
Expand All @@ -59,9 +62,9 @@ export const options = createOptions({
description: "Include Storybook's own stories?",
promptType: (_, { template }) => template === 'react',
},
create: {
fromLocalRepro: {
type: 'boolean',
description: 'Create the template from scratch (rather than degitting it)?',
description: 'Create the template from a local repro (rather than degitting it)?',
},
forceDelete: {
type: 'boolean',
Expand Down Expand Up @@ -246,7 +249,7 @@ async function addStories(paths: string[], { mainConfig }: { mainConfig: ConfigF
}

export async function sandbox(optionValues: OptionValues<typeof options>) {
const { template, forceDelete, forceReuse, dryRun, debug } = optionValues;
const { template, forceDelete, forceReuse, dryRun, debug, fromLocalRepro } = optionValues;

await ensureDir(sandboxDir);
let publishController: AbortController;
Expand All @@ -273,13 +276,29 @@ export async function sandbox(optionValues: OptionValues<typeof options>) {
if (exists && shouldDelete && !dryRun) await remove(cwd);

if (!exists || shouldDelete) {
await executeCLIStep(steps.repro, {
argument: template,
optionValues: { output: cwd, branch: 'next' },
cwd: sandboxDir,
dryRun,
debug,
});
if (fromLocalRepro) {
const srcDir = path.join(reprosDir, template, 'after-storybook');
if (!existsSync(srcDir)) {
throw new Error(dedent`
Missing repro directory '${srcDir}'!
To run sandbox against a local repro, you must have already generated
the repro template in the /repros directory using:
yarn generate-repros-next --template ${template}
`);
}
const destDir = cwd;
await copy(srcDir, destDir);
} else {
await executeCLIStep(steps.repro, {
argument: template,
optionValues: { output: cwd, branch: 'next' },
cwd: sandboxDir,
dryRun,
debug,
});
}

const mainConfig = await readMainConfig({ cwd });

Expand Down

0 comments on commit c503691

Please sign in to comment.