Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty generators #1276

Closed
ozum opened this issue Feb 10, 2021 · 10 comments
Closed

Empty generators #1276

ozum opened this issue Feb 10, 2021 · 10 comments
Labels
needs triage Awaiting triage

Comments

@ozum
Copy link

ozum commented Feb 10, 2021

Similar to #761, I need to run an empty generator. My use case is different though.

I'm developing an abstract generator. This base generator has a queued method called generateDbFiles, which is executed for each child generator. This method uses the _render() methods provided by the child generators.

this.queueMethod(async () => this.generateDbFiles(), `Database files generation`, "default");

So, child generators should provide a _render() method and some template files. If they don't want to change default behavior no more methods are necessary. This results in This Generator is empty. Add at least one method for it to run. error.

Could you please allow an empty generator? Otherwise, all child generators need to supply an empty stub method.

Many thanks,

@ozum ozum added the needs triage Awaiting triage label Feb 10, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open with no activity. Remove stale label or comment or this will be closed

@ozum
Copy link
Author

ozum commented Mar 19, 2021

Please reopen this issue.

@mshima mshima reopened this Mar 20, 2021
@mshima
Copy link
Member

mshima commented Mar 20, 2021

@ozum can you want to provide a PR?
It should be optional.

@ozum
Copy link
Author

ozum commented Mar 20, 2021

@mshima

I would like to. Since I'm not familiar with the codebase, it seems enough to remove the condition below. Is it OK, or are there any dependent code?

generator/index.js line 935 contains:

    if (validMethods.length === 0 && this._prompts.length === 0) {
      throw new Error(
        'This Generator is empty. Add at least one method for it to run.'
      );
    }

@mshima
Copy link
Member

mshima commented Mar 20, 2021

I would like to. Since I'm not familiar with the codebase, it seems enough to remove the condition below. Is it OK, or are there any dependent code?

A generator without tasks is an error, unless you are doing advanced use like you said.
So it should be optional.
It's not just remove, you should add an option to features and if that option is set, don't throw that error.

@ozum
Copy link
Author

ozum commented Mar 20, 2021

Hmm,

Something like below?

    this.option('allow-empty-generator', {
      type: Boolean,
      description: 'Allows generators without tasks.',
      default: false
    });

// ...

    if (!this.options.allowEmptyGenerator && validMethods.length === 0 && this._prompts.length === 0) {
      throw new Error(
        'This Generator is empty. Add at least one method for it to run.'
      );
    }

@mshima
Copy link
Member

mshima commented Mar 20, 2021

The option should not be exposed to users, so no this.option(definition) is needed.
For yeoman-generator@5, I've added the features constructor parameter along with args and options.
args and options are passed by the final user, while features is for generator customizations.

So

   if (!this.features.allowEmptyGenerator && validMethods.length === 0 && this._prompts.length === 0) {
      throw new Error(
        'This Generator is empty. Add at least one method for it to run.'
      );
    }

And add tests.

@ozum
Copy link
Author

ozum commented Mar 20, 2021

Currently, I can't migrate to Yeoman 5. Is there a way for v4?

@github-actions github-actions bot removed the stale label Mar 21, 2021
@mshima
Copy link
Member

mshima commented Mar 23, 2021

It's not worth, you can try to create a empty function on the object in the parent constructor:

this.notEmptyGenerator = function() {};
Object.assign(this, {function notEmptyGenerator()});

There are others alternatives if those don't work.

@ozum
Copy link
Author

ozum commented Mar 24, 2021

Thanks for the suggestion @mshima.

@ozum ozum closed this as completed Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Awaiting triage
Projects
None yet
Development

No branches or pull requests

2 participants