diff --git a/code/lib/codemod/src/index.ts b/code/lib/codemod/src/index.ts index e458e8ed9906..67eb20799190 100644 --- a/code/lib/codemod/src/index.ts +++ b/code/lib/codemod/src/index.ts @@ -87,7 +87,12 @@ export async function runCodemod(codemod: any, { glob, logger, dryRun, rename, p shell: true, } ); - if (result.status === 1) { + + if (codemod === 'mdx-to-csf' && result.status === 1) { + logger.log( + 'The codemod was not able to transform the files mentioned above. We have renamed the files to .mdx.broken. Please check the files and rename them back to .mdx after you have either manually transformed them to mdx + csf or fixed the issues so that the codemod can transform them.' + ); + } else if (result.status === 1) { logger.log('Skipped renaming because of errors.'); return; } diff --git a/code/lib/codemod/src/transforms/mdx-to-csf.ts b/code/lib/codemod/src/transforms/mdx-to-csf.ts index 2997a2fa89e0..eca57e8a8d7e 100644 --- a/code/lib/codemod/src/transforms/mdx-to-csf.ts +++ b/code/lib/codemod/src/transforms/mdx-to-csf.ts @@ -25,6 +25,7 @@ import type { MdxFlowExpression } from 'mdast-util-mdx-expression'; const mdxProcessor = remark().use(remarkMdx) as ReturnType; const renameList: { original: string; baseName: string }[] = []; +const brokenList: { original: string; baseName: string }[] = []; export default async function jscodeshift(info: FileInfo) { const parsed = path.parse(info.path); @@ -39,20 +40,29 @@ export default async function jscodeshift(info: FileInfo) { baseName += '_'; } - const result = await transform(info, path.basename(baseName)); + try { + const result = await transform(info, path.basename(baseName)); + + if (result[1] != null) { + fs.writeFileSync(`${baseName}.stories.js`, result[1]); + } - if (result[1] != null) { - fs.writeFileSync(`${baseName}.stories.js`, result[1]); renameList.push({ original: info.path, baseName }); - } - return result[0]; + return result[0]; + } catch (e) { + brokenList.push({ original: info.path, baseName }); + throw e; + } } process.on('exit', () => { renameList.forEach((file) => { fs.renameSync(file.original, `${file.baseName}.mdx`); }); + brokenList.forEach((file) => { + fs.renameSync(file.original, `${file.original}.broken`); + }); }); export async function transform( @@ -220,7 +230,6 @@ export async function transform( is(node, { name: 'Meta' }) ) { metaAttributes.push(...node.attributes); - console.log({ storyNamespaceName }); node.attributes = [ { type: 'mdxJsxAttribute',