Im trying to covert 'require' to 'import' programatically. I see that vs-code have such feature builtin.
Currenlty im using ts-morph Also i found such repo https://github.com/typescript-language-server/typescript-language-server
but there is not good example on how to run Apply Code Action
via API
https://github.com/typescript-language-server/typescript-language-server
I cannot attest to doing what you need using
typescript-language-server
but you can do this using ajscodeshift
codemod. This should work similar to how a ts-morph codemod works. This codemod should help you convert require to import as needed:It seems that you are having issues with running the codemod as well. You simply have to follow the usage guidelines of jscodeshift CLI. In this case, you can simply place the codemod in a .js file and run the following command:
The
-d
flag runs the codemod in dry-run mode. You can omit this if you want the project files to be affected. The-p
flag prints out the codemod run results. It's recommended that you keep this especially when testing out the codemod. input-file.js can be replaced with the path that should be affected by the codemod. You can limit the codemod run to specific directories or files within the project.