From 1b762a4d5247b455cd9af2f940d2d01a964f8f84 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Tue, 23 Jan 2024 09:52:18 +0100 Subject: [PATCH] Add support for multiple file extensions in runCodemod function --- code/lib/codemod/src/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/code/lib/codemod/src/index.ts b/code/lib/codemod/src/index.ts index dafb69513fd5..0bc3f4d1b3db 100644 --- a/code/lib/codemod/src/index.ts +++ b/code/lib/codemod/src/index.ts @@ -47,6 +47,7 @@ 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); @@ -54,7 +55,11 @@ export async function runCodemod(codemod: any, { glob, logger, dryRun, rename, p } const files = await globby([glob, '!**/node_modules', '!**/dist']); + const extensions = new Set(files.map((file) => path.extname(file).slice(1))); + 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; @@ -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`,