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.
Apparently, the error comes from Yeoman Generators code in the yeoman-generators/lib/base.js file.
Here is how I led to that conclusion :
_runningset totrueinhookForfunction (line 296)runfunction (line 78) and just after that, methods of the classGeneratorare iterated (line 81-137)Generatoris called during the iteration, so@hookForis called whereas_runningistrue: warning!But, the constructor should not be called because a test is done during the iteration to prevent it (line 92) :
However, this test, in my opinion, should be :
The hack does the trick. Feel free to add comment if I’m wrong.