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

CLI: Run automigrate at the end of sb init #16671

Merged
merged 4 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
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
20 changes: 8 additions & 12 deletions lib/cli/src/automigrate/fixes/eslint-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,14 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
if (!dryRun) packageManager.addDependencies({ installAsDevDependencies: true }, deps);

if (!dryRun && unsupportedExtension) {
logger.warn(
dedent(`
⚠️ The plugin was successfuly installed but failed to configure.

Found an .eslintrc config file with an unsupported automigration format: ${unsupportedExtension}.
Supported formats for automigration are: ${SUPPORTED_ESLINT_EXTENSIONS.join(', ')}.

Please refer to https://github.com/storybookjs/eslint-plugin-storybook#usage to finish setting up the plugin manually.
`)
);

return;
throw new Error(dedent`
⚠️ The plugin was successfuly installed but failed to configure.

Found an .eslintrc config file with an unsupported automigration format: ${unsupportedExtension}.
Supported formats for automigration are: ${SUPPORTED_ESLINT_EXTENSIONS.join(', ')}.

Please refer to https://github.com/storybookjs/eslint-plugin-storybook#usage to finish setting up the plugin manually.
`);
}

const eslint = await readConfig(eslintFile);
Expand Down
12 changes: 9 additions & 3 deletions lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface FixOptions {
dryRun?: boolean;
}

export const automigrate = async ({ fixId, dryRun, yes }: FixOptions) => {
export const automigrate = async ({ fixId, dryRun, yes }: FixOptions = {}) => {
const packageManager = JsPackageManagerFactory.getPackageManager();
const filtered = fixId ? fixes.filter((f) => f.id === fixId) : fixes;

Expand All @@ -41,8 +41,14 @@ export const automigrate = async ({ fixId, dryRun, yes }: FixOptions) => {
]);

if (runAnswer.fix) {
await f.run({ result, packageManager, dryRun });
logger.info(`✅ fixed ${chalk.cyan(f.id)}`);
try {
await f.run({ result, packageManager, dryRun });
logger.info(`✅ fixed ${chalk.cyan(f.id)}`);
} catch (error) {
logger.info(`❌ error in ${chalk.cyan(f.id)}:`);
logger.info(error.message);
logger.info();
}
} else {
logger.info(`Skipping the ${chalk.cyan(f.id)} fix.`);
logger.info();
Expand Down
15 changes: 9 additions & 6 deletions lib/cli/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import raxGenerator from './generators/RAX';
import serverGenerator from './generators/SERVER';
import { JsPackageManagerFactory, readPackageJson } from './js-package-manager';
import { NpmOptions } from './NpmOptions';
import { automigrate } from './automigrate';

const logger = console;

Expand Down Expand Up @@ -283,7 +284,7 @@ const projectTypeInquirer = async (options: { yes?: boolean }) => {
return Promise.resolve();
};

export function initiate(options: CommandOptions, pkg: Package): Promise<void> {
export async function initiate(options: CommandOptions, pkg: Package): Promise<void> {
const welcomeMessage = 'sb init - the simplest way to add a Storybook to your project.';
logger.log(chalk.inverse(`\n ${welcomeMessage} \n`));

Expand All @@ -296,7 +297,7 @@ export function initiate(options: CommandOptions, pkg: Package): Promise<void> {
let projectType;
const projectTypeProvided = options.type;
const infoText = projectTypeProvided
? 'Installing Storybook for user specified project type'
? `Installing Storybook for user specified project type: ${projectTypeProvided}`
: 'Detecting project type';
const done = commandLog(infoText);

Expand All @@ -305,13 +306,13 @@ export function initiate(options: CommandOptions, pkg: Package): Promise<void> {

try {
if (projectTypeProvided) {
if (installableProjectTypes.includes(options.type)) {
if (installableProjectTypes.includes(projectTypeProvided)) {
const storybookInstalled = isStorybookInstalled(packageJson, options.force);
projectType = storybookInstalled
? ProjectType.ALREADY_HAS_STORYBOOK
: options.type.toUpperCase();
: projectTypeProvided.toUpperCase();
} else {
done(`The provided project type was not recognized by Storybook.`);
done(`The provided project type was not recognized by Storybook: ${projectTypeProvided}`);
logger.log(`\nThe project types currently supported by Storybook are:\n`);
installableProjectTypes.sort().forEach((framework) => paddedLog(`- ${framework}`));
logger.log();
Expand All @@ -326,8 +327,10 @@ export function initiate(options: CommandOptions, pkg: Package): Promise<void> {
}
done();

return installStorybook(projectType, {
await installStorybook(projectType, {
...options,
...(isEsm ? { commonJs: true } : undefined),
});

return automigrate();
}