- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 19
Open
Labels
🪲 bugSomething isn't workingSomething isn't working
Description
Hello, trying to use your codeshift-cli, but faced a problem.
I created an example of my codemode repo - https://stackblitz.com/edit/node-uhcfpx?file=index.ts
Here I have a src
folder which contains all version transforms. Each has its own motions
and so on. Also there is a utils
folder with some functions I need across all transformations.
So I'm trying to execute local transform, but getting some errors:
- Trying to execute
codeshift -t ./src/3.0.0/transform.ts ./tmp/foo.tsx
- getting anError: Cannot find module './src/3.0.0/transform.ts'
. Looks like cli cannot find a transform file for some reason. Maybe I'm doing something wrong. - Trying to execute
codeshift ./tmp/foo.tsx
- it says that "No transforms specified" but then it gives me a prompt where I can choose a transformation (3.0.0) and then it throws anError: Cannot find module 'utils'
. In my project I have a utils folder and I use it in my transformations, but for some reason cli can not resolve it. - Tests with jest works fine, so looks like jest can successfully run my codemode with utils (run
jest
in console is ok)
Could you please help me to understand and find a problem?
Metadata
Metadata
Assignees
Labels
🪲 bugSomething isn't workingSomething isn't working
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
danieldelcore commentedon Feb 14, 2023
Hey @EliteUser, thanks for raising an issue!
Having a look at the code now and noticed
Error: Cannot find module 'utils'
is caused by an incorrect import insrc/3.0.0/transform.ts
&src/3.0.0/motions/test-motion/test-motion.ts
.I think you might be expecting this to work via webpack's import alias which isn't setup in this particular repo. To replicate it here, you will have to setup webpack, however IMO the simple relative import approach is best.
This is a strange one, this should be working given it passes the transform directly to
jscodeshift
, I'll have to investigate. However, looks like, as you said,codeshift ./tmp/foo.tsx
works in the meantime 🤔. I'll check that and get back to you regardless 😄Here's a link to a working repro: https://stackblitz.com/edit/node-cufcnk?file=src/3.0.0/motions/test-motion/test-motion.ts
Let me know if you run into any other issues!
Cheers,
Dan.
EliteUser commentedon Feb 15, 2023
Hello thank you for the answer!
I think I don't need webpack here, because my repo will be just a cli script. I know, that relative path to utils works, but it is not good approach, because I'm using it in other places, so it will end writing
../../../../utils
which is a good candidate fortsconfig's paths
. But as I said, it looks like paths works only for checking an resolving in my ide, but when I'm trying to execute transform it says "sorry, I don't undermstand 😢".I think that tsconfig-paths may help, but I don't understand how to make this thing work, and not sure it will help at all 😄. By the way, tests works fine, so it seems that jest can recognize paths, why node cant?
danieldelcore commentedon Feb 16, 2023
Oohh now I understand, this is actually because
jscodeshift
has an internaltsconfig
for parsing typescript files which can be found here: https://github.com/facebook/jscodeshift/blob/main/parser/tsOptions.js . Making it really hard to customise from the outside.One way you can work around this is to transpile your TS files into javascript, similar to what we do with more typical web applications.
For example:
tsconfig.json
/dist
folder as per normalcodeshift.config.js
to point to the transpiled transforms in the/dist
folder (should be js files instead of ts files)For what it's worth,
@codeshift/cli
does this for you already (except for the custom paths).All you need to do is run:
npx @codeshift/cli init <project-dir>
Optionally you can then run the following to create a transform automatically 😄
npx @codeshift/cli init <project-dir> --transform 1.0.0
danieldelcore commentedon Feb 16, 2023
That being said, I think the
dist
that is currently output isn't exactly right, so I'll need to create a ticket to update it make this automatic in the future.synoptase commentedon Mar 21, 2023
Adding my stone.
Given the following:
codeshift.config.js
This command fails:
While this succeeds:
codeshift ../shared/components/MyFile.js -p babel