yeoman / generator

Rails-inspired generator system that provides scaffolding for your apps
http://yeoman.io
BSD 2-Clause "Simplified" License
1.22k stars 299 forks source link

this.fs.copyTpl breaks with recursive globs (**/*) #735

Closed timkelty closed 9 years ago

timkelty commented 9 years ago

Ran into this here: https://github.com/timkelty/generator-craft-plugin

      this.fs.copyTpl(
        this.templatePath('pluginhandle/**/*'),
        this.destinationPath(pluginDest),
        this
      );

This was working rather recently, but with a freshly re-installed yo, I get this:

events.js:72
        throw er; // Unhandled 'error' event
              ^
TypeError: Cannot call method 'toString' of null
    at copy.process (/Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy-tpl.js:11:34)
    at applyProcessingFunc (/Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:12:16)
    at EditionInterface.exports._copySingle (/Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:52:24)
    at EditionInterface.<anonymous> (/Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:37:10)
    at Array.forEach (native)
    at EditionInterface.exports.copy (/Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:34:9)
    at EditionInterface.module.exports [as copyTpl] (/Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy-tpl.js:9:8)
    at module.exports.yeoman.generators.Base.extend.writing.pluginFiles (/Users/timkelty/Repos/generator-craft-plugin/app/index.js:125:15)
    at /Users/timkelty/Repos/generator-craft-plugin/node_modules/yeoman-generator/lib/base.js:406:16
    at processImmediate [as _immediateCallback] (timers.js:345:15)

I saw there were recent fixes for changes in mem-fs-editor, so I'm thinking it might be related?

blai commented 9 years ago

I think this is a https://github.com/SBoudrias/mem-fs-editor bug. You should file the bug there instead.

SBoudrias commented 9 years ago

Can this be because the system try to copy a directory as a file?

timkelty commented 9 years ago

@SBoudrias likely, as it works with globs like somedir/*. I used to work, though.

JonCognioDigital commented 9 years ago

I started learning to create my first generator today, following this tutorial.

http://yeoman.io/authoring/file-system.html

As soon as I added this section of code I got exactly the same error.

generators.Base.extend({ writing: function () { this.fs.copyTpl( this.templatePath('index.html'), this.destinationPath('public/index.html'), { title: 'Templating with Yeoman' } ); } });

I assumed I had done something wrong as I'm a newbie so finding this issue was useful. Hope it gets fixed soon as I'm guessing lots of people use that getting started guide.

Tokimon commented 9 years ago

I tried using the generator.template() method to render a glob expression (generator.template("folder/**/*", 'folder', data)) but actually it gives me another error: TypeError: First argument needs to be a number, array or string. at new Buffer (buffer.js:188:15) I did some digging in it and the error comes from this call in 'mem-fs-editor/actions/read.js': file.contents = file.contents || new Buffer(options.defaults); (line 11) The reason being that option.defaults is undefined. This could be avoided by adding a value to the options.defaults, but (and this is where yeoman comes in) the read() method is invoked in the template() method in 'yeoman-generator/lib/actions/actions.js' at the var body = this.engine(this.fs.read(source), data || this, options) (line 191). Here the second parameter in the read() method call (the options) is omitted, so it is not possible to correct this for the user of the generator.template() method.

Who is finally to blame I guess is up to the yeoman/mem-fs-editor teams, but somehow the glob copying doesn't work.

SBoudrias commented 9 years ago

This is now fix thanks to @blai - npm update to grab the patch release.

payner35 commented 9 years ago

I seem to be having this issue as well.... an example of a subgen is here https://github.com/payner35/generator-meteor-react/blob/master/view/index.js

$ yo meteor-react:view imaview Creating a new View for imaview

/usr/local/lib/node_modules/generator-meteor-react/templates/view/view.coffee /Users/gavin/Dropbox/git/test/client/views/imaview/imaview.coffee

events.js:72 throw er; // Unhandled 'error' event ^ TypeError: Cannot call method 'toString' of null at copy.process (/usr/local/lib/node_modules/generator-meteor-react/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy-tpl.js:11:34) at applyProcessingFunc (/usr/local/lib/node_modules/generator-meteor-react/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:12:16) at EditionInterface.exports._co...

if i install the package locally with npm link it works fine... just when i npm install from the remote package i get this error.

blai commented 9 years ago

@payner35 Can you try running npm cache clean and do npm install again to install the generator?

payner35 commented 9 years ago

does not seem to be a fix?

i do notice that Yeoman-generator is dependent on "mem-fs-editor": "^1.0.0" "name": "yeoman-generator", "version": "0.18.10",

should this not be bumped to at least 1.2.1 https://github.com/SBoudrias/mem-fs-editor/releases

is this the issue? https://github.com/SBoudrias/mem-fs-editor/issues/19

SBoudrias commented 9 years ago

^ will get patches and minor releases.

Make sure to run npm update and if you're using a npm mirror that this mirror is updated.

payner35 commented 9 years ago

still no luck.. i have checked the package on another computer to see if the issue is local to my machine.. but i get the same result.

again if its worth noting. npm link works fine?

this is my gen https://github.com/payner35/generator-meteor-react

blai commented 9 years ago

I looked at your code and found that https://github.com/payner35/generator-meteor-react/blob/master/view/index.js#L41 causes a copy from the source file /generator-meteor-react/templates/view/view.coffee (which does not exist if your generator-meteor-react is installed through npm).

The problem is in your package.json https://github.com/payner35/generator-meteor-react/blob/master/package.json#L26. You need to add a line to include your template folder in your npm install. So your package.json should have this:

  "files": [
    "app",
    "view",
    "collection",
    "component",
    "templates"
  ]
SBoudrias commented 9 years ago

Ah thanks for looking deeper into it @blai!

payner35 commented 9 years ago

Great..!! thanks @blai and @SBoudrias.

Package working and updated ;)