question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can jscodeshift output to a different file?

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

2reactions
ElonVolocommented, Jun 20, 2022

In my evcodeshift fork of jscodeshift, I added the ability to pass an option to defineTest that create a folder in the project directory that contains all the transforms that are made during the unit tests. I had trouble with figuring out what transforms were going bad during unit tests, and the diffed output wasn’t very helped, so I added in the options to save output from unit tests. See the below link for documentation on it.

(I probably need to put it in the README at some point (thought I think the general documentation on what to pass in for test options was kind of sparse).

https://github.com/ElonVolo/evcodeshift/blob/main/sample/__tests__/reverse-identifiers-test.js

But at this point, my understanding from the Facebook people is that they’re not going to be adding any additional features to jscodeshift (I already tried adding a --gitignore flag to use the projects .gitignore to avoid transforms), so work on jscodeshift is pretty much going to be limited to bugfixes. I’m guessing that a new flag isn’t going to fly with them (pun intended).

2reactions
TSMMarkcommented, Jan 19, 2022

@danielo515 Maybe you can create multiple instances of the root, such as:

  const rootA = j(fileInfo.source)
  const rootB = j('')

Then remove nodes from rootA, and add them to rootB Then writeFile rootB.toSource such as @dbchristopher suggested https://github.com/facebook/jscodeshift/issues/160#issuecomment-823541122

Read more comments on GitHub >

github_iconTop Results From Across the Web

jscodeshift: a toolkit for running codemods over multiple ...
It provides: A runner, which executes the provided transform for each file passed to it. It also outputs a summary of how many...
Read more >
Write Code to Rewrite Your Code: jscodeshift
Up your refactoring game by using codemods, scripts used to rewrite other ... This will run input-file.js through the transform some-transform.js and print...
Read more >
An Introduction to jscodeshift - The Blog of Feifan Zhou
The first argument is the directory in which you want to run the transformation; jscodeshift will recursively apply the transformation to all files...
Read more >
how to use jscodeshift to insert a line in the beginning of ...
It looks like you have to "cast" the node to the jscodeshift . A solution is: export default (file, api) => { const...
Read more >
Creating a custom transform for jscodeshift
The --dry and --print options will run the transform without overwriting the file and print the would-be-result to the console instead.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found