Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox: Add ability to run from local repro #18950

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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