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

Separate user/author defined options from yeoman internal options #1180

Closed
desidia26 opened this issue Feb 26, 2020 · 1 comment
Closed

Separate user/author defined options from yeoman internal options #1180

desidia26 opened this issue Feb 26, 2020 · 1 comment
Labels
needs triage Awaiting triage

Comments

@desidia26
Copy link

Feature:
Currently to access user supplied options/options that the author of the generator defines, that author must reference the this.options object. This object contains what looks to be yeoman internal data such as

`

env: Environment {

_events: [Object: null prototype] { error: [Function] },

_eventsCount: 1,

_maxListeners: undefined,

arguments: [],

options: {},

adapter: TerminalAdapter { promptModule: [Function] },

store: Store {

  _generators: [Object],

  _meta: [Object],

  _packagesPaths: [Object],

  _packagesNS: [Array]

},

runLoop: Queue {

  queueNames: [Array],

  __queues__: [Object],

  _maxListeners: 0

},

sharedFs: Store {

  _events: [Object: null prototype] {},

  _eventsCount: 0,

  _maxListeners: 0

},

lookups: [ '.', 'generators', 'lib/generators' ],

aliases: [ [Object] ],

sharedOptions: { sharedData: {} }

`

It would be helpful to separate these from user/author defined options so that option validation could be done more easily.

Use case: I have a generator where I wish to check and see if the user has passed an argument that I don't account for, and notify them.

@desidia26 desidia26 added the needs triage Awaiting triage label Feb 26, 2020
@mshima
Copy link
Member

mshima commented Feb 26, 2020

It's possible to pass environments defined options on another constructor parameter to Generator.
https://github.com/yeoman/environment/blob/5c77b3ed017ab5b3f3e4aafe544f8a2b8b1d60a6/lib/environment.js#L547-L555

But generator's ones cannot be differentiated.

generator/lib/index.js

Lines 820 to 830 in 3d8374b

options = _.extend(
{
skipInstall: this.options.skipInstall || this.options['skip-install'],
'skip-install': this.options.skipInstall || this.options['skip-install'],
skipCache: this.options.skipCache || this.options['skip-cache'],
'skip-cache': this.options.skipCache || this.options['skip-cache'],
forceInstall: this.options.forceInstall || this.options['force-install'],
'force-install': this.options.forceInstall || this.options['force-install']
},
options
);

At last and most important is that the options passed at the second parameter should not be changed for backward compatibility
We are not breaking backward compatibility for this feature.
Ex: this.options.namespace is used in a lot of places, this.options.env is used since at least generator 1.0.0.

So its needed to subtract environment options from the received options.
A workaround is:

const userOptions = {...this.options};
delete userOptions.env;
delete userOptions.resolved;
delete userOptions.namespace;
Object.keys(this.env.sharedOptions).forEach(key => delete userOptions[key]);
['skipInstall', 'skip-install', 'skipCache', 'skip-cache', 'forceInstall', 'force-install'].forEach(key => delete userOptions[key]);

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