-
-
Notifications
You must be signed in to change notification settings - Fork 386
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
No clear documentation on how to use registerTransformStream #837
Comments
@mcshaman to answer the question, yes you just call Let's leave this ticket open though, the documentation could be greatly improved around that point. (especially as gulp isn't used too much anymore) |
I was looking for a none trivial example and agreed with this ticket. Eventually, I worked out the following to be able to use gulp-filter to only rewrite .json files and ignore other types. Hopefully, this will be of use to others looking for a realistic example: const jsonTransform = require('gulp-json-transform');
const filter = require('gulp-filter');
const beautify = require('gulp-beautify');
const jsonFilter = filter(['**/*.json'], { restore: true });
module.exports = class extends Generator {
_private_scmsecret() {
return this.options.scmsecret || false
}
// The name `constructor` is important here
constructor(args, opts) {
super(args, opts);
// "--scmsecret" command line flag toggles write feature of json template
this.option('scmsecret');
const self = this;
// this pipe will pass through (restore) anything that doesn't match jsonFilter
this.registerTransformStream(
[
jsonFilter,
jsonTransform(function(data, file) {
// based on the flags passed to the generator we may or may not edit the json structure
if( !self._private_scmsecret() ){
// edit the json removing the scmsecret object attributes
...
return data
}
else return data
}),
beautify({indent_size: 2 }),
jsonFilter.restore
]
); |
I have also been trying to use this feature to do a string replace using gulp-replace without success. It appears that when the files are being written out using this.fs.copyTpl, the writer becomes disconnected from the "this" pointer and the result of the "registerTransformStream" doesn't stick when it gets to "_writeFiles(done)" code. (The debugger shows an array with a transformer in it at the end of "registerTransformStream's" execution , but on a breakpoint in "_writeFiles(done)" it shows an empty array). Example code:
templates/ directory contains a file thing.txt with the contents:
That file gets written out with "thing" unchanged but "appname" appropriately replaced. The expected behavior is that the file will contain:
Instead it writes:
|
There appears to be some weirdness with the "this" pointer here. Splicing in a hack into "_writeFiles" shows that if the transformStreams stay connected to this, the code works as expected and "thing" is replaced by "yo-test":
Somewhere in "run" things are getting messed up. The run code looks pretty complex so it will take a while to learn and debug. This looks like where things might possibly go pear shaped is in the "run(cb)" code where this segment is calling _writeFiles:
|
It appears that if you use "composeWith" that you loose some connections downstream. In the project where there is a problem, the example code that isn't working is chained like so:
If the example code (in entry-point/index.js) is not linked via "composeWith", then it works as expected. |
@robblovell yeah, composed generators registered transformed aren't going to run. That was reported somewhere on https://github.com/yeoman/generator/issues (sorry I don't have time to dig it up right now). There was some context around this issue and we might have come to an agreement about what we should do about this... |
@robblovell yeah, composed generators registered transformed aren't going to run. That was reported here yeoman/generator#819. There was some context around this issue and we might have come to an agreement about what we should do about this... |
Cool. I did back off from using compseWith in favor of a branching questioning strategy with my own branching organization. I was hoping to use the compse with to manage different templates as the question tree branches. I am off and running with this and I am moving forward. I guess this issue is being moved forward on that other tree. |
@robblovell I am looking for something similar with yeoman, so I setup a "demo" project(easy to run and for team contribution), then string replace all vars..Is there any update on that or it's your final solution? |
FYI Just opened a new issue #838, this function appears to be renamed to |
The changes are live on https://yeoman.io/authoring/file-system. |
I have been searching the internet for hours now trying to find documentation on how to use the registerTransformStream() method to modify files.
The documentation is very light on and says that you can pretty much use any gulp plugins. This is great however there are no example how to pipe them in a generator.
It is recommended to use gulp-if or gulp-filter to subset any transforms... However I cannot work out how you would effectively pipe to other transformers. Do I register all the transformers in the order that I want them piped?
The text was updated successfully, but these errors were encountered: