yeoman / generator

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

Priority does not work as I expected #839

Closed Ovski4 closed 8 years ago

Ovski4 commented 9 years ago

Hi,

I'm just getting started and trying to build my own generator, but I don't understand how priorities work.

Here is my package.json file

{
  "name": "generator-ovski-custom",
  "version": "0.1.0",
  "description": "",
  "files": [],
  "keywords": [
    "yeoman-generator"
  ],
  "dependencies": {
    "yeoman-generator": "^0.20.2"
  }
}

Here is my generators/app/index.js

var generators = require('yeoman-generator');

var myVar = "FIRST";

module.exports = generators.Base.extend({
    end: function () {
        console.log('END');
        console.log(myVar);
    },
    prompting: function () {
        console.log("PROMPTING");
        var done = this.async();
        this.prompt({
            type: 'input',
            name: 'name',
            message: 'Your project name',
            default: this.appname
        }, function (answers) {
            myVar = answers.name;
            this.log(answers.name);
            done();
        }.bind(this));
    },
    init: function () {
        console.log('INIT');
    console.log(myVar);
    }
});

I read the docs at http://yeoman.io/authoring/running-context.html so here is what I expected by running yo ovski-custom:

INIT
FIRST
PROMPTING
? Your project name: Ok
Ok
END
Ok

And here is what i got:

END
FIRST
PROMPTING
? Your project name: Ok
Ok
INIT
Ok

Maybe I didn't understood, and the "grouped-queue thing" is only triggered if I start composing generators together? I get the same behaviour with group priorities, and with the version 0.17.7 of the yeoman-generator dependency.

Thanks!

SBoudrias commented 9 years ago

Try on the latest version.

SBoudrias commented 9 years ago

Also, init is not a valid priority name.

Ovski4 commented 9 years ago

I am already using the latest version (0.20.2)

I edited init to initializing, now I got the following

END
FIRST
INIT
FIRST
PROMPTING
? Your project name: Ok
Ok

The 'prompting' function is still running after the 'end' function

SBoudrias commented 9 years ago

Will investigate. My guess is the first task is running right away as it's the first one queued.

Ovski4 commented 9 years ago

Thanks. I think you are right, I'll run more tests

juliosampaio commented 9 years ago

Hey guys, any clue? I'm facing the same issue here. Is there something wrong with my code?

Here is my ../app/index.js

var yeoman = require('yeoman-generator');
var util = require('util');
var chalk = require('chalk');
var prompt = require('./prompting');

var MyIonicGenerator = module.exports = function MyIonicGenerator(args, options) {
  yeoman.generators.Base.apply(this, arguments);
  this.argument('appname', { type: String, required: true, desc: 'The name of the app'});
  prompt.init(this);
};

util.inherits(MyIonicGenerator, yeoman.generators.Base);

MyIonicGenerator.prototype.install = function install() {
  var c = this.config;
  var args = ['start', this.appname];

  if (c.useStarterTeamplate) {
    args.push(c.starterTemplate);
  }

  if (c.package) {
    args.push('-i');
    args.push(c.package);
  }

  if (c.ionicAppID) {
    args.push('--io-app-id');
    args.push(c.ionicAppID);
  }

  if (c.useSass) {
    args.push('--sass');
  }

  var ionicCli = this.spawnCommand('ionic', args);

  ionicCli
    .on('error', function (err) {
      this.log(chalk.red(JSON.stringify(err)));
    }.bind(this));
};

MyIonicGenerator.prototype.prompting = function prompting() {
  var done = this.async();
  this.prompt(prompt.questions(), function (answers) {
    this.config = answers;
    done();
  }.bind(this));
};

When runing it, the first thing executed is the install method, any help?

SBoudrias commented 9 years ago

Maybe just order the priority in the right order for the time being?

Ovski4 commented 9 years ago

That's what I did for the time being. Even though it's still an issue, I'm using group priorities in the right order and it works as expected

luckylooke commented 8 years ago

Any progress here? I have issue with end, it is called before initialization or not at all. yo version 1.4.8

SBoudrias commented 8 years ago

@luckylooke your issue doesn't look related.

luckylooke commented 8 years ago

@SBoudrias why? It also depends on position of end property. If I move it on top, it will be fired before initialization. If I move it down (in 'right' order you mentioned), it will not fire at all. I have feeling that it is caused by same bug.

SBoudrias commented 8 years ago

@luckylooke okay so you have a part of the answer (why it triggers first). But if it doesn't fire when you move it after, it's because your generator is never ending. My educated guess is you're dead looping the process with some asynchronicity management.

luckylooke commented 8 years ago

@SBoudrias, Ok, thanks for clues.. I will test some things later on it.

SBoudrias commented 8 years ago

Fixed in https://github.com/SBoudrias/grouped-queue/releases/tag/v0.3.2