-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for template codemods #81
Conversation
So tests were passing for me locally. This fails in CI due to:
@rwjblue any clue what's going on? |
No, not sure. I was also having similar issues, and then ran out of time 😢. I need to try to revive my Node version bump PR and figure out what is up 🤔 |
954ad68
to
0d3607e
Compare
After rebasing, this is green now, so waiting for your review @rwjblue! 🙂 Btw, this is already in use (through my github fork) in https://github.com/kaliber5/ember-bootstrap-codemods... |
…ather than visitor fn
As ember-template-recast handles that, rwjblue#81 (comment)
A generated transform now exports its own type. This allows us to * remove the `type` arguments from `runTransform()`, which was problematic anyway as that type would have to passed in the project's `cli.js`, so would not have allowed to have different transform types per project. Instead the type is taken from the transform's exported type itself. * same for `runTransformTest()`: `options.type` is not needed anymore, as the type is also taken from the transform itself * instead of failing when a user tries to generate a fixture for a transform with non-matching types, the generate fixture command now implicitly determines the fixture type from its transform type
3c734ca
to
1902c28
Compare
I refactored the previous implementation quite a bit, inspired by your feedback. A generated transform now exports its own type. This allows us to
I hope this makes sense, @rwjblue please re-review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks great, thanks for doing that refactor!
commands/local/generate/fixture.js
Outdated
|
||
let { codemodName, fixtureName } = options; | ||
let codemodDir = `${process.cwd()}/transforms/${codemodName}`; | ||
let codemodTransform = `${codemodDir}/index.js`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add index.js
here? I wonder if we can basically just let it fall through via the require
that is done down in getTransformType
(since require(codemodDir)
and require(codemodDir + '/index.js')
are effectively the same thing)? That would allow folks to use other extensions (for example .ts
)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/bin-support.js
Outdated
function getTransformPath(binRoot, transformName) { | ||
const path = require('path'); | ||
|
||
return path.join(binRoot, '..', 'transforms', transformName, 'index.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do require.resolve(path.join(binRoot, '..', 'transforms', transformName))
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -0,0 +1,67 @@ | |||
'use strict'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that this is (or could be) basically the same as the jscodeshift one but we run a different function for actually testing (jscodeshift runs runInlineTest
and this one runs runTemplateTest
).
I would prefer to refactor these together to avoid the duplication that exists right now, and to also ensure that both support the same feature set. At the moment, I think this file is missing the ability to test passing different command line options (that the template transform can access with getOptions
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, done.
src/test-support/utils.js
Outdated
let root = process.cwd() + `/transforms/${options.name}/`; | ||
let transformPath = root + 'index.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this should be using getTransformPath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I now put getTransformPath
into transform-support.js
, and changed the signature a tiny bit, to expect the project's root folder (instead of binRoot
, as we don't have/need binRoot
here).
src/transform-support.js
Outdated
if (!fs.existsSync(transformPath)) { | ||
throw new Error(`Transform ${transformPath} not found.`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use require.resolve
in getTransformPath
then I think this can be removed (since require.resolve
would throw)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thank you very much @simonihmig! I'll try to get a release out in the morning... |
Updated for the further changes to codemod-cli landed in rwjblue/codemod-cli#81 after review. (still waiting for an official release though) Fixes #5
Closes #8