Run an AI Refactor on Every File in a Directory with Cursor

With Cursor's "Composer" feature, you can select and entire directory and make changes to every file. This video walks through the process of selecting a folder and refactoring all the files from JavaScript to TypeScript. It also demonstrates other tips around generating terminal commands and customizing your main Cursor "Rules for AI".

Share with a coworker

Social Share Links

Transcript

[00:00] Open Composer with Command-I and I'll say I need to refactor all of my scripts in the and then add the at scripts directory and then continue with from JavaScript to TypeScript. Please refactor them one at a time and after you're done converting the script please delete the old JavaScript version. Wait for my input to continue on to refactor the next script. Prompt me to enter Y for continue or N for stop. We'll let that paste in.

[00:25] I'm going to hit command slash to change to the latest Cloud model or you can just click right here to switch between models. Let's go ahead and run this. For some reason this did cancel even though it looks like it worked successfully. So I'm gonna go ahead and roll back up, edit the message, and then resubmit. I'll go ahead and accept it And it looks like it did not delete the old script for me.

[00:49] Maybe that's what canceled it out last time. I'm gonna hide this with escape. It detected a deprecated API and this one's expected. Since this is node this one means that looks like we don't have a TSConfig in here. I'll just set that up real quick.

[01:07] Just need to fix the module resolution. Then looks like we're pretty good. And it found another deprecated API. Now if we want we can also hit command shift P and say compare active file with and then we could say chatgpt.js and we could scroll through the diff here to see if it missed anything else. Looks like a big one as it's stripped out all the comments but other than that it looks like semicolons and types are the biggest change.

[01:38] So to continue on I'm going to say please delete the old chat-gpt.js and continue. We'll switch models again with command-slash. Looks like I lost it when I closed Composer. This will happen sometimes when it finds a linting error it will aggressively try and fix it and prioritize those. If you see that happen too much add something like ignore all linting errors.

[02:03] But for now let's try this again. Let's copy and paste this down here, hit enter, and it does look like it's struggling to delete that file for some reason. But we'll go ahead and accept all, hit escape, look at our corgi, scroll down a bit, everything looks pretty clean. This looks brand new because that definitely didn't exist in here. And let's open Composer again.

[02:27] This time we'll just hit y, hit enter, then accept all. We'll check the create gist. Looks like it created this interface for us, which may actually be wrong since HTML URL does exist on the result of that. So we can just ditch that. Now rather than manually deleting every JavaScript file in the scripts directory.

[02:48] Let's hit ctrl-tilde, command-k, and I'll say delete all the .js files in the at scripts. And it looks like that actually doesn't show up in search, so I'm just going to go period slash scripts directory and let it figure out from there. I'll hit enter and that'll generate this from here. One tip there is often you'll have to say I am on a Mac. For some reason cursor currently doesn't automatically feed that into the context.

[03:19] And you can hit command-enter to run that and then all of the JavaScript scripts will be gone. And one last side note you can come in here and go into your general rules for AI and you can say things like I'm on a Mac, never use the find command just to show that this is working. So if I come down here and I press up to try and run the same command, hit submit, you'll see that this time it avoided the find command. So I'll delete this, hit save, press up again, hit submit, and you'll see this time it went back to the find command.