Add option to read/output to memory from rw generate (instead of writing files directly to disk)
See original GitHub issue- Current behavior: When running
rw generate..., the CLI reads and writes directly to disk - This makes it hard to integrate the generators with IDE environments. For example, if the user has open/dirty/unsaved files, having the CLI writing directly to disk can lead to conflicts (the dreaded: “this file was modified on disk, do you want to overwrite?” scenario).
 - Proposed behavior: Add an option (ex: 
--jsonor--dry-run) that tells the generator to output a JSON map representing the files that were modified/generated, instead of applying those changes directly. - This would enable seamless integration with IDEs (taking into account unsaved files, undo/redo, etc) and it can open the door for post-processing generated files, for example
 
Example
$ yarn rw g page About /about --json
will print:
{
  "/foo/web/src/pages/About/About.js": "import ...",
  "/foo/web/src/Routes.js": "..."
}
JSON Format
The JSON format could be very simple:
- Added files are represented by entries with content
 - Modified files are represented by entries with the new content (if IDEs/tools want to be smart about calculating diffs, it is up to them)
 - Deleted files are represented by entries with “null” value.
 
{
  "/foo/new-file.js": "content.",
  "/foo/modified-file.js": "new content",
  "/foo/deleted-file.js": null,
}
Passing File Overrides / Reading from Memory
- In order to fully eliminate the “this file was modified on disk, do you want to overwrite?” error, a second feature must be implemented: Generators must accept a set of file overrides.
 - This is necessary since some generators operate on existing files (they read a file from disk, run a regular expression or some transformation, and then write the new file). If Routes.js is open in an IDE and unsaved (with changes), for example, running 
rw g pagewill result in a conflict. - The solution is to enable passing “file overrides” when running generators.
 
Example
$ rw g page About /about --json --override '{"foo/web/src/Routes.js": "..."}'
Issue Analytics
- State:
 - Created 3 years ago
 - Reactions:2
 - Comments:10 (9 by maintainers)
 
Top Results From Across the Web
Python Write to File – Open, Read, Append, and Other File ...
And we want to add a new line to it, we can open it using the "a" mode (append) and then, call the...
Read more >1. fio - Flexible I/O tester rev. 3.32 - FIO's documentation!
Use the directory specified by path for generated state files instead of the current working directory. Any parameters following the options will be...
Read more >Fio Basics - Virtono Community
The following options are available for “–rw”: read – Sequential reading; write – Sequential Write; randread – random reading; randwrite – ...
Read more >How to read/write from/to a file using Go - Stack Overflow
Let's make a Go 1-compatible list of all the ways to read and write files in Go. Because file API has changed recently...
Read more >fio(1): flexible I/O tester - Linux man page
fio is a tool that will spawn a number of threads or processes doing a particular type of I/O action as specified by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The hard part is intercepting the filesystem calls. The rest is just the API to surface this feature. I’m just trying to provide a starting point that lets us get this done quickly. A lower level API to generators is even better, but it will take longer.
In the end, the point where the IDE will call this is roughly here. So it can be via importing an API, or an exec call.
Yes absolutely. Having a programmatic API for generators would open even more doors. But this one is necessary, and should be easy to implement.
I was suggesting the easiest way: Adding the “complete” content to the map. Always the complete content. No need to worry about diffs, insertion points, edits, etc. A new file and an edited file look the same in the map. The only way to know which is which is to compare against the existing working tree.
The simplest use case:
rw g page...