Skip to content

Commit

Permalink
Merge pull request #25708 from storybookjs/valentin/fix-jscodeshift-c…
Browse files Browse the repository at this point in the history
…odemod

Codemods: Add support for multiple file extensions in runCodemod function
  • Loading branch information
valentinpalkovic committed Jan 23, 2024
2 parents f2d311b + 1b762a4 commit 02bf75c
Showing 1 changed file with 6 additions and 0 deletions.
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)));
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

0 comments on commit 02bf75c

Please sign in to comment.