From 16311805589b376f2efa6a28ea3049ccd9da6d41 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 19 Feb 2024 15:45:20 +0100 Subject: [PATCH] Revert "remove the logging to file feature from autoblockers" This reverts commit 8422756247169a123dc1222b5b05e3a345a7166f. --- .../src/autoblock/block-dependencies-versions.ts | 3 +++ code/lib/cli/src/autoblock/block-node-version.ts | 3 +++ code/lib/cli/src/autoblock/block-stories-mdx.ts | 11 ++++++----- code/lib/cli/src/autoblock/block-storystorev6.ts | 5 +++++ code/lib/cli/src/autoblock/index.ts | 16 +++++++++++++++- code/lib/cli/src/autoblock/types.ts | 7 +++++++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/code/lib/cli/src/autoblock/block-dependencies-versions.ts b/code/lib/cli/src/autoblock/block-dependencies-versions.ts index fb052e60c62e..284562aa9f6d 100644 --- a/code/lib/cli/src/autoblock/block-dependencies-versions.ts +++ b/code/lib/cli/src/autoblock/block-dependencies-versions.ts @@ -43,6 +43,9 @@ export const blocker = createBlocker({ return acc; }, false); }, + message(options, data) { + return `Found ${data.packageName} version: ${data.installedVersion}, please upgrade to ${data.minimumVersion} or higher.`; + }, log(options, data) { switch (data.packageName) { case 'react-scripts': diff --git a/code/lib/cli/src/autoblock/block-node-version.ts b/code/lib/cli/src/autoblock/block-node-version.ts index 8d67cdd51018..220b29823e4e 100644 --- a/code/lib/cli/src/autoblock/block-node-version.ts +++ b/code/lib/cli/src/autoblock/block-node-version.ts @@ -11,6 +11,9 @@ export const blocker = createBlocker({ } return false; }, + message(options, data) { + return `Please use Node.js v18 or higher.`; + }, log(options, data) { return dedent` We've detected you're using Node.js v${data.nodeVersion}. diff --git a/code/lib/cli/src/autoblock/block-stories-mdx.ts b/code/lib/cli/src/autoblock/block-stories-mdx.ts index 3c1fadeda351..b868d913ecd0 100644 --- a/code/lib/cli/src/autoblock/block-stories-mdx.ts +++ b/code/lib/cli/src/autoblock/block-stories-mdx.ts @@ -11,7 +11,12 @@ export const blocker = createBlocker({ } return { files }; }, - log(options, data) { + message(options, data) { + return `Found ${data.files.length} stories.mdx ${ + data.files.length === 1 ? 'file' : 'files' + }, these must be migrated.`; + }, + log() { return dedent` Support for *.stories.mdx files has been removed. Please see the migration guide for more information: @@ -21,10 +26,6 @@ export const blocker = createBlocker({ Check the migration guide for more information: https://mdxjs.com/blog/v3/ - Found ${data.files.length} stories.mdx ${ - data.files.length === 1 ? 'file' : 'files' - }, these must be migrated. - Manually run the migration script to convert your stories.mdx files to CSF format documented here: https://storybook.js.org/docs/migration-guide#storiesmdx-to-mdxcsf `; diff --git a/code/lib/cli/src/autoblock/block-storystorev6.ts b/code/lib/cli/src/autoblock/block-storystorev6.ts index cd9eaffb6a7a..40a9f8822ac9 100644 --- a/code/lib/cli/src/autoblock/block-storystorev6.ts +++ b/code/lib/cli/src/autoblock/block-storystorev6.ts @@ -1,3 +1,4 @@ +import { relative } from 'path'; import { createBlocker } from './types'; import { dedent } from 'ts-dedent'; import type { StorybookConfigRaw } from '@storybook/types'; @@ -14,6 +15,10 @@ export const blocker = createBlocker({ } return false; }, + message(options, data) { + const mainConfigPath = relative(process.cwd(), options.mainConfigPath); + return `StoryStoreV7 feature must be removed from ${mainConfigPath}`; + }, log() { return dedent` StoryStoreV7 feature must be removed from your Storybook configuration. diff --git a/code/lib/cli/src/autoblock/index.ts b/code/lib/cli/src/autoblock/index.ts index a6c45a2318ba..ca8116d890cb 100644 --- a/code/lib/cli/src/autoblock/index.ts +++ b/code/lib/cli/src/autoblock/index.ts @@ -2,6 +2,7 @@ import type { AutoblockOptions, Blocker } from './types'; import { logger } from '@storybook/node-logger'; import chalk from 'chalk'; import boxen from 'boxen'; +import { writeFile } from 'node:fs/promises'; const excludesFalse = (x: T | false): x is T => x !== false; @@ -33,6 +34,7 @@ export const autoblock = async ( return { id: blocker.id, value: true, + message: blocker.message(options, result), log: blocker.log(options, result), }; } else { @@ -44,9 +46,12 @@ export const autoblock = async ( const faults = out.filter(excludesFalse); if (faults.length > 0) { + const LOG_FILE_NAME = 'migration-storybook.log'; + const messages = { welcome: `Blocking your upgrade because of the following issues:`, reminder: chalk.yellow('Fix the above issues and try running the upgrade command again.'), + logfile: chalk.yellow(`You can find more details in ./${LOG_FILE_NAME}.`), }; const borderColor = '#FC521F'; @@ -54,13 +59,22 @@ export const autoblock = async ( logger.plain( boxen( [messages.welcome] - .concat(faults.map((i) => i.log)) + .concat(faults.map((i) => i.message)) .concat([messages.reminder]) + .concat([messages.logfile]) .join('\n\n'), { borderStyle: 'round', padding: 1, borderColor } ) ); + await writeFile( + LOG_FILE_NAME, + faults.map((i) => '(' + i.id + '):\n' + i.log).join('\n\n----\n\n'), + { + encoding: 'utf-8', + } + ); + return faults[0].id; } diff --git a/code/lib/cli/src/autoblock/types.ts b/code/lib/cli/src/autoblock/types.ts index 39e6c728921a..62be9625c76e 100644 --- a/code/lib/cli/src/autoblock/types.ts +++ b/code/lib/cli/src/autoblock/types.ts @@ -21,6 +21,13 @@ export interface Blocker { * @returns A truthy value to activate the block, return false to proceed. */ check: (options: AutoblockOptions) => Promise; + /** + * Format a message to be printed to the log-file. + * @param context + * @param data returned from the check method. + * @returns The string to print to the terminal. + */ + message: (options: AutoblockOptions, data: T) => string; /** * Format a message to be printed to the log-file. * @param context