hparra / gulp-rename

Rename files easily
MIT License
692 stars 73 forks source link

Error: write after end #84

Open ghost opened 5 years ago

ghost commented 5 years ago

Hello,

I am trying to use this package as a stream for a Yeoman generator, to rename gitignore files on the fly: https://yeoman.io/authoring/file-system.html#transform-output-files-through-streams

this.registerTransformStream(rename(path => {
  if (path.extname === '.gitignore') {
    path.basename = '.gitignore';
    path.extname = '';
  }
}));

The error received in the terminal seems to come from readable-stream: Error [ERR_STREAM_WRITE_AFTER_END]: write after end

Related issue on yeoman repository: https://github.com/yeoman/yo/issues/577

The code posted above works fine with gulp-rename@1.2.2, but not with any more recent version.

I am wondering if you are aware of this issue, and what would be the difference. According to my very quick scan on the source code, I have only seen newer dependencies versions and a LICENSE file... Not sure exactly what is the cause of this issue.

v1.2.2 dependencies:

"devDependencies": {
  "gulp": ">=3.0.0",
  "gulp-sourcemaps": "^1.5.0",
  "gulp-util": "^3.0.4",
  "jshint": "^2.6.3",
  "map-stream": ">=0.0.4",
  "mocha": ">=1.15.0",
  "should": ">=2.1.0"
},

v1.4.0 dependencies:

"devDependencies": {
  "gulp": "^4.0.0",
  "gulp-sourcemaps": "^2.6.4",
  "jscs": "^3.0.0",
  "jshint": "^2.0.0",
  "map-stream": "^0.0.7",
  "mocha": "^5.0.0",
  "should": "^13.0.0",
  "vinyl": "^2.0.0"
},

I am also wondering if tthose differences between version 1.2.2 and the more recent ones such as the 1.4.0 currently published on NPM are accessible somewhere? https://www.npmjs.com/package/gulp-rename?activeTab=versions

Those new versions do not seem to be available as releases on this repository: https://github.com/hparra/gulp-rename/releases

alvaromartmart commented 5 years ago

I'm having the same issue

ghost commented 5 years ago

If you wish a quickfix @alvaromartmart, you can use v1.2.2 meanwhile:

"devDependencies": {
  "gulp-rename": "<=1.2.2"
}
jariwalakrunal1983 commented 5 years ago

registerTransformStream with gulp-rename is still an issue even with gulp-rename v1.2.2. However, I get it working with glob.

const glob = require('glob');

writing() {
    const files = glob.sync('**', { dot: true, nodir: true, cwd: this.templatePath() })
    for (let i in files) {
        this.fs.copyTpl(
            this.templatePath(files[i]),
            this.destinationPath( this.props.destinationFolderPath + '\\' + files[i].replace(/__fileName__/g,this.props.fileName)),
            this.props
        )
    }
}
StefanRutzmoser commented 5 years ago

Still an issue with v2.0.0. With 1.2.2 it works for me.

danechitoaie commented 4 years ago

Having same issue, and what I've noticed is that it seems to be caused when you make changes to file object that is cloned originalFile.clone({ contents: false });

In my case I got it working by replacing gulp-rename with:

class MyTransformer extends Transform {
    constructor(props) {
        super({ objectMode: true });
        this.props = props;
    }

    _transform(chunk, encoding, callback) {
        for (const [pKey, pVal] of Object.entries(this.props)) {
            const regExp = new RegExp("{{" + pKey + "}}", "g");
            if (chunk.relative.match(regExp)) {
                chunk.path = path.join(chunk.base, chunk.relative.replace(regExp, pVal));
            }
        }

        callback(null, chunk);
    }
}

This seems to be working and I'm not getting any errors. As soon as I add chunk.clone({ contents: false }) and start using that it crashes. Without it it works.

DFreds commented 4 years ago

This is still an issue. Downgrading to <=1.2.2. worked for me

bendenoz commented 3 years ago

+1 - stream never ending in 1.4.0 when file source >= 100k (approximately) downgrading fixes the problem

yocontra commented 3 years ago

Cam everyone who is having issues post what version of node you are using?

bendenoz commented 3 years ago

had issue with both node 8 and 10

yocontra commented 3 years ago

@bendenoz Can you try it with the latest version of node and see if that resolves it?

Node 8 is a year past end of life as well so not something we're trying to support right now, but node 10 not working is something we will fix if its an issue there.

yocontra commented 3 years ago

As another thing you could try if you want to be a guinea pig - on line 3 of this package replace require('stream') with require('readable-stream') and see if that does anything.

bendenoz commented 3 years ago

@contra , readable-stream doesn't change anything, but the problem seems to lie in the originalFile.clone call. If I remove it and use the file originalFile directly it works. Tried changing options but didn't help.

The file is created by an old version of gulp-util / vinyl so it might be part of the problem

LeeNFr commented 1 year ago

suryadev99