1

I'm trying to develop a custom generator for Yeoman using CoffeeScript but I'm facing a problem. When I use the hookFor method in the constructor of my class Generator, I get a warning hookFor must be used within the constructor when I try to init my project with Yeoman and my custom generator. Here is the code of my generator in index.coffee :

path = require 'path'
util = require 'util'
yeoman = require '../../../../'
module.exports = class Generator extends yeoman.generators.Base
    constructor: ->
        super()
        @directories = ['controllers', 'helpers', 'models', 'templates', 'views']
        @hookFor 'artefact:controller', args: ['App']
    deploy: ->
        @directory '.', '.'
        @mkdir path.join 'dev', directory for directory in @directories

Any help will be appreciated. Thanks.

1 Answers1

1

Apparently, the error comes from Yeoman Generators code in the yeoman-generators/lib/base.js file. Here is how I led to that conclusion :

  1. The warning is caused by the variable _running set to true in hookFor function (line 296)
  2. This variable is set to true in run function (line 78) and just after that, methods of the class Generator are iterated (line 81-137)
  3. the constructor defined in CoffeeScript for the class Generator is called during the iteration, so @hookFor is called whereas _running is true : warning!
  4. But, the constructor should not be called because a test is done during the iteration to prevent it (line 92) :

    if ( method.constructor === '-' )
    
  5. However, this test, in my opinion, should be :

    if ( method === 'constructor' )
    

The hack does the trick. Feel free to add comment if I'm wrong.