How to run jscodeshift transform on all files?
Asked Answered
P

2

6

I am creating a transform that will replace all instances of:

templateUrl: 'some/url/to/some.html'

with

template: require('some/url/to/some.html')

I am doing this because I am changing the way that our AngularJS code brings in their templates. I am going to make them all use the webpack string loader.

I have gotten the transform to work. But now I have no idea how to run it against all of the files in my project. I can't see in the documentation how to run this against all of the .js files in my project. To run my transform, I currently type:

jscodeshift ./folder/myComponent.js -t ./tools/codemodes/template.js -d -p

This command will run my transform against the myComponent.js file, but I want it to be run against all of the .js files in my project. What do I need to change with my current command to make it select all .js files, and run the transform against all of them?

Percept answered 21/4, 2017 at 15:4 Comment(0)
P
8

If you want to run a transform against all files in a directory, you just enter the directory path, instead of a file path. In the example above, do the following:

jscodeshift ./folder -t ./tools/codemod/templates.js -d -p

That will run the transform against all of your nested js files.

Percept answered 21/4, 2017 at 16:33 Comment(1)
If you've followed this syntax and are still having problems, make sure you explicitly specify the extensions option, like extensions=ts,tsx,test.tsx,test.ts .Nary
B
4

The answer provided by frosty will run on all files in the directory, not just JS files. If you want to just run the transform on all nested JS files, use the ** glob syntax:

jscodeshift ./path/to/folder/**/*.js -t ./tools/codemod/templates.js -d -p

Note that certain shells don't recognize ** and you will have to first manually turn it on with:

$ shopt -s globstar
Bitchy answered 4/1, 2018 at 3:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.