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

Codemods: Add support for multiple file extensions in runCodemod function #25708

Merged
merged 1 commit into from
Jan 23, 2024
Merged
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
6 changes: 6 additions & 0 deletions code/lib/codemod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@ export async function runCodemod(codemod: any, { glob, logger, dryRun, rename, p
// jscodeshift/prettier know how to handle .ts/.tsx extensions,
// so if the user uses one of those globs, we can auto-infer
let inferredParser = parser;

if (!parser) {
const extension = path.extname(glob).slice(1);
const knownParser = jscodeshiftToPrettierParser(extension);
if (knownParser !== 'babel') inferredParser = extension;
}

const files = await globby([glob, '!**/node_modules', '!**/dist']);
const extensions = new Set(files.map((file) => path.extname(file).slice(1)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@valentinpalkovic Did you try this on .stories.mdx files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I haven't I can put it on my todo list

const commaSeparatedExtensions = Array.from(extensions).join(',');

logger.log(`=> Applying ${codemod}: ${files.length} files`);

if (files.length === 0) {
logger.log(`=> No matching files for glob: ${glob}`);
return;
Expand All @@ -70,6 +75,7 @@ export async function runCodemod(codemod: any, { glob, logger, dryRun, rename, p
// which is faster, and also makes sure the user won't see babel messages such as:
// [BABEL] Note: The code generator has deoptimised the styling of repo/node_modules/prettier/index.js as it exceeds the max of 500KB.
'--no-babel',
`--extensions=${commaSeparatedExtensions}`,
'--fail-on-error',
'-t',
`${TRANSFORM_DIR}/${codemod}.js`,
Expand Down