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

A way to get value out of composed generator? #1708

Closed
thejohnfreeman opened this issue Apr 23, 2019 · 5 comments
Closed

A way to get value out of composed generator? #1708

thejohnfreeman opened this issue Apr 23, 2019 · 5 comments

Comments

@thejohnfreeman
Copy link

@thejohnfreeman thejohnfreeman commented Apr 23, 2019

I'd like to send my users to generator-license to let them choose a license, but I need to know that choice after it runs to put it in the project metadata file I generate. Is there an API for getting values out of subgenerators? Can there be?

@SBoudrias
Copy link
Member

@SBoudrias SBoudrias commented Apr 24, 2019

You could read it from the package.json file after the generator-license run.

There's no API to pass data outside a composed generator. The system isn't design to allow this kind of interactions.

@thejohnfreeman
Copy link
Author

@thejohnfreeman thejohnfreeman commented Apr 24, 2019

generator-license does not emit a package.json, nor do I want it to. I'm trying to scaffold a Python project. Thank you for the answer regardless!

@FrederickEngelhardt
Copy link

@FrederickEngelhardt FrederickEngelhardt commented Jul 13, 2020

One way to do this would be to bind an update function to an object. Firing the function would then update the original generator.

In my use case, I want to combine a bunch of generators and update the file only once if possible. My root app generator will do this while my sub generators will only update the object that the root generator will eventually reference to create a file.

Example:

// Generator 1
module.exports = class extends Generator {
constructor(args, opts) {
    super(args, opts)

    this.packageJson = {
      name: 'test'
    }

    this._updatePackageJson = this._updatePackageJson.bind(this)
  }
 _updatePackageJson(newValues: Record<string, any>) {
    this.packageJson = merge(this.packageJson, newValues)
  }

  initializing() {
    this.composeWith('generator2', {
      configuration: this.configuration,
      updatePackageJson: this._updatePackageJson,
    })
  }
}
// generator 2
module.exports = class extends Generator {
constructor(args, opts) {
    super(args, opts)
    this.updatePackageJson = opts.updatePackageJson
  }

  updatePackageFunction(){
     this.updatePackageJson({ name: 'hello world' })
 }
}

@thejohnfreeman
Copy link
Author

@thejohnfreeman thejohnfreeman commented Jul 17, 2020

That requires both generators to be aware of each other. It doesn't work when composing with an arbitrary generator.

@mshima
Copy link
Member

@mshima mshima commented Jul 17, 2020

That requires both generators to be aware of each other. It doesn't work when composing with an arbitrary generator.

Yep, a new composing api is in experimental, it will be enable composing with arbitrary generator.
But it's not backward compatible.
https://github.com/mshima/generators/blob/3dead1692fd2716048c4282f5fe846d4627c88da/%40mshima/generator-generator/generators/app/index.js#L38-L89

Should be enabled by yo --experimental.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants