import-js icon indicating copy to clipboard operation
import-js copied to clipboard

Auto import via NodeJS

Open Jrfidellis opened this issue 5 years ago • 3 comments

Is it possible to use this module to auto import all undefined variables in a project?

I would like to add a feature to do something like a fix(path) passing the root path of my project in a CLI tool i am creating.

This feature would be very useful to my project migration situation, where we used TS modules, and want to migrate it all to the import {} from syntax.

Jrfidellis avatar Nov 21 '19 18:11 Jrfidellis

Yes, you can run importjs from the command line to do this like so:

importjs fix --overwrite src/**/*.js

Or you can import it in a node script, though you might need to copy some boilerplate from the CLI: https://github.com/Galooshi/import-js/blob/dc8a158d15df50b2f15be3461f73706958df986d/lib/importjs.js#L59-L84

However, it's not perfect at finding the right import (especially when there are several with the same name, or your project contains build artifacts), and normally needs a level of human checking or intervention

coagmano avatar Nov 21 '19 23:11 coagmano

I was not able to use the wildcard (**) as you said, can you help me? @coagmano

It doesn't seem to be supported.

Jrfidellis avatar Dec 10 '19 22:12 Jrfidellis

The globstar (**) will only work on a bash-like shell, so if you're building it into a CLI, you'll need to shell out, or use a globbing package for file discovery. (Also, you can't easily shell out on Windows, so a package might be better regardless)

What approach are you using currently?

coagmano avatar Dec 10 '19 22:12 coagmano

Closing this as stale. If you need more guidance please feel free to reopen it. Thanks.

mikabytes avatar Jan 29 '24 07:01 mikabytes