-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update example command to run SB commands
- Loading branch information
Showing
4 changed files
with
163 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,119 @@ | ||
import path from 'path'; | ||
import { existsSync } from 'fs'; | ||
|
||
import { getOptionsOrPrompt } from './utils/options'; | ||
import type { CLIStep } from './utils/cli-step'; | ||
import { executeCLIStep } from './utils/cli-step'; | ||
|
||
const frameworks = ['react']; | ||
const addons = ['a11y', 'storysource']; | ||
|
||
getOptionsOrPrompt('yarn example', { | ||
framework: { | ||
name: 'Which framework would you like to use?', | ||
values: frameworks, | ||
required: true, | ||
}, | ||
addon: { | ||
name: 'Which extra addons (beyond the CLI defaults) would you like installed?', | ||
values: addons, | ||
multiple: true, | ||
}, | ||
includeStories: { | ||
name: "Include Storybook's own stories (only applies if a react-based framework is used)?", | ||
}, | ||
create: { | ||
name: 'Create the example from scratch (rather than degitting it)?', | ||
}, | ||
verdaccio: { | ||
name: 'Use verdaccio rather than yarn linking stories?', | ||
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: { | ||
description: | ||
"Include Storybook's own stories (only applies if a react-based framework is used)?", | ||
}, | ||
create: { | ||
description: 'Create the example from scratch (rather than degitting it)?', | ||
}, | ||
verdaccio: { | ||
description: 'Use verdaccio rather than yarn linking stories?', | ||
}, | ||
start: { | ||
description: 'Start the example app?', | ||
inverse: true, | ||
}, | ||
build: { | ||
description: 'Build the example app?', | ||
}, | ||
watch: { | ||
description: 'Start building used packages in watch mode as well as the example app?', | ||
}, | ||
}); | ||
} | ||
|
||
const steps: Record<string, CLIStep> = { | ||
repro: { | ||
command: 'repro', | ||
description: 'Bootstrapping example', | ||
icon: '👷', | ||
hasArgument: true, | ||
options: { | ||
template: { values: frameworks }, | ||
e2e: {}, | ||
}, | ||
}, | ||
start: { | ||
name: 'Start the example app?', | ||
inverse: true, | ||
add: { | ||
command: 'add', | ||
description: 'Adding addon', | ||
icon: '+', | ||
hasArgument: true, | ||
options: {}, | ||
}, | ||
build: { | ||
name: 'Build the example app?', | ||
command: 'build', | ||
description: 'Building example', | ||
icon: '🔨', | ||
options: {}, | ||
}, | ||
watch: { | ||
name: 'Start building used packages in watch mode as well as the example app?', | ||
dev: { | ||
command: 'dev', | ||
description: 'Starting example', | ||
icon: '🖥', | ||
options: {}, | ||
}, | ||
}).then((r) => console.log(r)); | ||
}; | ||
|
||
async function main() { | ||
const optionValues = await getOptions(); | ||
const examplesDir = path.resolve(__dirname, '../examples'); | ||
|
||
const { framework } = optionValues; | ||
const cwd = path.join(examplesDir, framework as string); | ||
|
||
// TODO -- what to do when the directory already exists? | ||
if (!existsSync(cwd)) { | ||
await executeCLIStep(steps.repro, { | ||
argument: cwd, | ||
optionValues: { template: framework }, | ||
cwd: examplesDir, | ||
}); | ||
|
||
// 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 | ||
await executeCLIStep(steps.add, { argument: addonName, cwd }); | ||
} | ||
|
||
// TODO copy stories | ||
} | ||
|
||
const { start } = optionValues; | ||
if (start) { | ||
await executeCLIStep(steps.dev, { cwd }); | ||
} else { | ||
await executeCLIStep(steps.build, { cwd }); | ||
// TODO serve | ||
} | ||
|
||
// TODO start dev | ||
} | ||
|
||
main().catch((err) => console.error(err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getCommand, OptionSpecifier, OptionValues } from './options'; | ||
import { exec } from '../../lib/cli/src/repro-generators/scripts'; | ||
|
||
const cliExecutable = require.resolve('../../lib/cli/bin/index.js'); | ||
|
||
export type CLIStep = { | ||
command: string; | ||
description: string; | ||
hasArgument?: boolean; | ||
icon: string; | ||
// It would be kind of great to be able to share these with `lib/cli/src/generate.ts` | ||
options: OptionSpecifier; | ||
}; | ||
|
||
export async function executeCLIStep( | ||
cliStep: CLIStep, | ||
options: { | ||
argument?: string; | ||
optionValues?: OptionValues; | ||
cwd: string; | ||
} | ||
) { | ||
if (cliStep.hasArgument && !options.argument) | ||
throw new Error(`Argument required for ${cliStep.command} command.`); | ||
|
||
const prefix = `node ${cliExecutable} ${cliStep.command}`; | ||
const command = getCommand( | ||
cliStep.hasArgument ? `${prefix} ${options.argument}` : prefix, | ||
cliStep.options, | ||
options.optionValues || {} | ||
); | ||
|
||
await exec( | ||
command, | ||
{ cwd: options.cwd }, | ||
{ | ||
startMessage: `${cliStep.icon} ${cliStep.description}`, | ||
errorMessage: `🚨 ${cliStep.description} failed`, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters