Skip to content

Commit

Permalink
Rename broken MDX files to .mdx.broken
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Feb 23, 2024
1 parent f0a899d commit fd8c625
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 6 additions & 1 deletion code/lib/codemod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 15 additions & 6 deletions code/lib/codemod/src/transforms/mdx-to-csf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { MdxFlowExpression } from 'mdast-util-mdx-expression';
const mdxProcessor = remark().use(remarkMdx) as ReturnType<typeof remark>;

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);
Expand All @@ -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(
Expand Down Expand Up @@ -220,7 +230,6 @@ export async function transform(
is(node, { name: 'Meta' })
) {
metaAttributes.push(...node.attributes);
console.log({ storyNamespaceName });
node.attributes = [
{
type: 'mdxJsxAttribute',
Expand Down

0 comments on commit fd8c625

Please sign in to comment.