facebook / facebook/jscodeshift
Can jscodeshift output to a different file?
- Dominant language
- JavaScript
- Stars
- 10k
- Forks
- 498
- PR merge metrics
- No merged PRs in 30d
Description
I'm building a library that creates model classes backed by Immutable.js data structures based on Flow type definitions. I would prefer my jscodeshift transform to read the Flow definition in one file but output the transformed file in a different location, but I don't see that as being supported by jscodeshift. It seems to only be set up to overwrite files it processes (which is obviously the common use case). Could anybody let me know if this is possible or if you'd suggest an alternate approach? Here's an example of what I'm trying to build:
_model_defs/user.js_
```
export type UserType {
id: number,
name: string,
};
```
The jscodeshift transform would process this file and output something like:
_models/user.js_
```
export class User {
_state: Immutable.Map();
constructor(
get id(): string {
return this._state.get('id');
}
setId(id: number): User {
return new User(this._state.set('id', id));
}
get name(): string {
return this._state.get('name');
}
setId(name: string): User {
return new User(this._state.set('name', name));
}
}
```
I have this transform working, but it currently overwrites the definition file. I've thought about copying all the matching files into a temp directory, processing those and then moving them to their final destination, but that approach seems more difficult than I'd like. I've also thought about creating my own runner that runs each file through the jscodeshift API - maybe using a dry run - and writes the desired file based on the string results of the transform. This is less than ideal as I'd be copying many aspects of the jscodeshift runner and I'm not sure yet if I can easily access the string results, but I think I will be able to.
I'm trying to take this approach so that the resulting class files are not stored in source control, but generated based solely on the type definitions. As a last resort, I could probably drop that desire in favor of overwriting previously transformed files. But I feel this approach is more confusing and prone to producing unexpected results.
Thanks for reading this long post. I'd love to hear suggestions on how best to approach this.
Contributor guide
Assessment
This issue has not been assessed yet.