16

I want to build a Yeoman generator that needs to unzip a file.

From their documentation, it seems this process is done using this.registerTransformStream(...). It says it accept any gulp plugin, so I tried gulp-unzip (link)

Here's my code:

// index.js
...
writing: function() {
  var source = this.templatePath('zip'); // the folder where the zipped file is
  var destination = this.destinationRoot();

  this.fs.copy(source, destination);
  this.registerTransformStream(unzip() );
}
...

The result seems promising, first it shows all the file list then I get Error: write after end error.

Here's the dump:

   create license.txt
   create readme.html
   create config.php
   ...
   ...
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at writeAfterEnd (C:\Users\myname\Documents\project\generator-test\node_modules\gulp-unzip\node_modules\readable-stream\lib\_stream_writable.js:144:12)
    at Transform.Writable.write (C:\Users\myname\Documents\project\generator-test\node_modules\gulp-unzip\node_modules\readable-stream\lib\_stream_writable.js:192:5)
    at DestroyableTransform.ondata (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:531:20)
    at emitOne (events.js:77:13)
    at DestroyableTransform.emit (events.js:169:7)
    at readableAddChunk (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:198:18)
    at DestroyableTransform.Readable.push (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:157:10)
    at DestroyableTransform.Transform.push (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:123:32)
    at DestroyableTransform._transform (C:\Users\myname\Documents\project\generator-test\node_modules\mem-fs-editor\lib\actions\commit.js:34:12)
    at DestroyableTransform.Transform._read (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:159:10)

The destination folder is empty after this. It seems the stream is trying to write the unzipped file but failed.

Does anyone solved this problem before? Or is there alternative way by just using the built-in fs?

Thanks a lot

hrsetyono
  • 4,474
  • 13
  • 46
  • 80
  • 1
    Personally, I think it would be easier for you to manually unzip the file using a Node.js package. Yeoman is only javascript code running, so no need to find a Yeoman related helper. Any node code will works inside the generator framework. – Simon Boudrias Aug 31 '16 at 20:28
  • 1
    @SimonBoudrias Sorry for the late reply. Yeoman uses `mem-fs`, so the zip file doesn't exist until all finished. I can't find a way to target that file using something like `node-unzip` since `mem-fs` doesn't seem to have callback too. – hrsetyono Sep 06 '16 at 05:46
  • 1
    no need to copy the zip file with mem-fs if you just want to unzip the content. – Simon Boudrias Sep 06 '16 at 06:20
  • 1
    @SimonBoudrias I finally found a way to still use the built-in copy. We can add callback by using [commit](https://github.com/SBoudrias/mem-fs-editor#commitfilters-callback). Inside the callback, I unzip it with `adm-zip` package. Thanks for your input :). I can give you the bounty if you want. Just put answer mentioning that. – hrsetyono Sep 06 '16 at 14:56

0 Answers0