2022-07-25 22:43:46 +10:00
|
|
|
import path from 'path';
|
2022-07-27 22:09:36 +10:00
|
|
|
import { remove, pathExists } from 'fs-extra';
|
|
|
|
import prompts from 'prompts';
|
2022-07-25 22:43:46 +10:00
|
|
|
|
2022-07-25 11:19:26 +10:00
|
|
|
import { getOptionsOrPrompt } from './utils/options';
|
2022-07-25 22:43:46 +10:00
|
|
|
import { executeCLIStep } from './utils/cli-step';
|
2022-07-28 20:30:40 +10:00
|
|
|
import { exec } from '../code/lib/cli/src/repro-generators/scripts';
|
2022-07-25 11:19:26 +10:00
|
|
|
|
2022-07-28 20:15:57 +10:00
|
|
|
const frameworks = ['react', 'angular'];
|
2022-07-25 11:19:26 +10:00
|
|
|
const addons = ['a11y', 'storysource'];
|
2022-07-26 16:12:07 +10:00
|
|
|
const examplesDir = path.resolve(__dirname, '../examples');
|
2022-07-28 21:21:58 +10:00
|
|
|
const codeDir = path.resolve(__dirname, '../code');
|
2022-07-25 11:19:26 +10:00
|
|
|
|
2022-07-25 22:43:46 +10:00
|
|
|
async function getOptions() {
|
|
|
|
return getOptionsOrPrompt('yarn example', {
|
|
|
|
framework: {
|
|
|
|
description: 'Which framework would you like to use?',
|
|
|
|
values: frameworks,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
addon: {
|
|
|
|
description: 'Which extra addons (beyond the CLI defaults) would you like installed?',
|
|
|
|
values: addons,
|
|
|
|
multiple: true,
|
|
|
|
},
|
|
|
|
includeStories: {
|
2022-07-28 20:15:57 +10:00
|
|
|
description: "Include Storybook's own stories?",
|
|
|
|
promptType: (_, { framework }) => framework === 'react',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
create: {
|
|
|
|
description: 'Create the example from scratch (rather than degitting it)?',
|
|
|
|
},
|
2022-07-27 22:09:36 +10:00
|
|
|
forceDelete: {
|
|
|
|
description: 'Always delete an existing example, even if it has the same configuration?',
|
2022-07-28 20:15:57 +10:00
|
|
|
promptType: false,
|
2022-07-27 22:09:36 +10:00
|
|
|
},
|
|
|
|
forceReuse: {
|
2022-07-28 19:46:40 +10:00
|
|
|
description: 'Always reuse an existing example, even if it has a different configuration?',
|
2022-07-28 20:15:57 +10:00
|
|
|
promptType: false,
|
2022-07-27 22:09:36 +10:00
|
|
|
},
|
2022-07-28 19:46:40 +10:00
|
|
|
link: {
|
|
|
|
description: 'Link the storybook to the local code?',
|
|
|
|
inverse: true,
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
start: {
|
2022-07-28 19:46:40 +10:00
|
|
|
description: 'Start the example Storybook?',
|
2022-07-25 22:43:46 +10:00
|
|
|
inverse: true,
|
|
|
|
},
|
|
|
|
build: {
|
2022-07-28 19:46:40 +10:00
|
|
|
description: 'Build the example Storybook?',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
watch: {
|
2022-07-28 19:46:40 +10:00
|
|
|
description: 'Start building used packages in watch mode as well as the example Storybook?',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
2022-07-26 16:20:49 +10:00
|
|
|
dryRun: {
|
2022-07-28 19:46:40 +10:00
|
|
|
description: "Don't execute commands, just list them (dry run)?",
|
2022-07-26 16:20:49 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-27 22:09:36 +10:00
|
|
|
const steps = {
|
2022-07-25 22:43:46 +10:00
|
|
|
repro: {
|
|
|
|
command: 'repro',
|
|
|
|
description: 'Bootstrapping example',
|
|
|
|
icon: '👷',
|
|
|
|
hasArgument: true,
|
|
|
|
options: {
|
|
|
|
template: { values: frameworks },
|
|
|
|
e2e: {},
|
|
|
|
},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
add: {
|
|
|
|
command: 'add',
|
|
|
|
description: 'Adding addon',
|
|
|
|
icon: '+',
|
|
|
|
hasArgument: true,
|
|
|
|
options: {},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-28 21:21:58 +10:00
|
|
|
link: {
|
|
|
|
command: 'link',
|
|
|
|
description: 'Linking packages',
|
|
|
|
icon: '🔗',
|
|
|
|
hasArgument: true,
|
|
|
|
options: { local: {} },
|
|
|
|
},
|
2022-07-25 11:19:26 +10:00
|
|
|
build: {
|
2022-07-25 22:43:46 +10:00
|
|
|
command: 'build',
|
|
|
|
description: 'Building example',
|
|
|
|
icon: '🔨',
|
|
|
|
options: {},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
dev: {
|
|
|
|
command: 'dev',
|
|
|
|
description: 'Starting example',
|
2022-07-26 16:20:49 +10:00
|
|
|
icon: '🖥 ',
|
2022-07-25 22:43:46 +10:00
|
|
|
options: {},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const optionValues = await getOptions();
|
|
|
|
|
2022-07-28 21:21:58 +10:00
|
|
|
const { framework, forceDelete, forceReuse, link, dryRun } = optionValues;
|
2022-07-25 22:43:46 +10:00
|
|
|
const cwd = path.join(examplesDir, framework as string);
|
|
|
|
|
2022-07-27 22:09:36 +10:00
|
|
|
const exists = await pathExists(cwd);
|
2022-07-28 19:46:40 +10:00
|
|
|
let shouldDelete = exists && !forceReuse;
|
2022-07-27 22:09:36 +10:00
|
|
|
if (exists && !forceDelete && !forceReuse) {
|
2022-07-28 19:46:40 +10:00
|
|
|
const relativePath = path.relative(process.cwd(), cwd);
|
|
|
|
({ shouldDelete } = await prompts({
|
2022-07-27 22:09:36 +10:00
|
|
|
type: 'toggle',
|
2022-07-28 19:46:40 +10:00
|
|
|
message: `${relativePath} already exists, should delete it and create a new one?`,
|
|
|
|
name: 'shouldDelete',
|
|
|
|
initial: false,
|
2022-07-27 22:09:36 +10:00
|
|
|
active: 'yes',
|
|
|
|
inactive: 'no',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-07-28 19:46:40 +10:00
|
|
|
if (exists && shouldDelete && !dryRun) await remove(cwd);
|
2022-07-27 22:09:36 +10:00
|
|
|
|
2022-07-28 19:46:40 +10:00
|
|
|
if (!exists || shouldDelete) {
|
2022-07-25 22:43:46 +10:00
|
|
|
await executeCLIStep(steps.repro, {
|
|
|
|
argument: cwd,
|
|
|
|
optionValues: { template: framework },
|
|
|
|
cwd: examplesDir,
|
2022-07-26 16:20:49 +10:00
|
|
|
dryRun,
|
2022-07-25 22:43:46 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO -- sb add <addon> doesn't actually work properly:
|
|
|
|
// - installs in `deps` not `devDeps`
|
|
|
|
// - does a `workspace:^` install (what does that mean?)
|
|
|
|
// - doesn't add to `main.js`
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
for (const addon of optionValues.addon as string[]) {
|
|
|
|
const addonName = `@storybook/addon-${addon}`;
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-07-26 16:20:49 +10:00
|
|
|
await executeCLIStep(steps.add, { argument: addonName, cwd, dryRun });
|
2022-07-25 22:43:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO copy stories
|
2022-07-28 21:21:58 +10:00
|
|
|
|
|
|
|
if (link) {
|
|
|
|
await executeCLIStep(steps.link, {
|
|
|
|
argument: cwd,
|
|
|
|
cwd: codeDir,
|
|
|
|
dryRun,
|
|
|
|
optionValues: { local: true },
|
|
|
|
});
|
|
|
|
}
|
2022-07-25 22:43:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
const { start } = optionValues;
|
|
|
|
if (start) {
|
2022-07-28 20:30:40 +10:00
|
|
|
await exec(
|
|
|
|
'yarn storybook',
|
|
|
|
{ cwd },
|
|
|
|
{
|
|
|
|
dryRun,
|
|
|
|
startMessage: `⬆️ Starting Storybook`,
|
|
|
|
errorMessage: `🚨 Starting Storybook failed`,
|
|
|
|
}
|
|
|
|
);
|
2022-07-25 22:43:46 +10:00
|
|
|
} else {
|
2022-07-26 16:20:49 +10:00
|
|
|
await executeCLIStep(steps.build, { cwd, dryRun });
|
2022-07-25 22:43:46 +10:00
|
|
|
// TODO serve
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO start dev
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => console.error(err));
|