Skip to content

Commit

Permalink
Add features argument to constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 5, 2021
1 parent 9b9a9cb commit 9729a58
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ class Generator extends Base {
* @param {string[]} args - Provide arguments at initialization
* @param {Object} options - Provide options at initialization
* @param {Priority[]} [options.customPriorities] - Custom priorities
* @param {boolean|string} [options.unique] - Generates a uniqueBy id for the environment
* Accepts 'namespace' or 'true' for one instance by namespace
Accepts 'argument' for one instance by namespace and 1 argument
*
* @property {Object} env - the current Environment being run
* @property {String} resolved - the path to the current generator
* @property {String} description - Used in `--help` output
Expand All @@ -131,10 +127,11 @@ class Generator extends Base {
* }
* };
*/
constructor(args, options) {
constructor(args, options, features) {
super();

if (!Array.isArray(args)) {
features = options;
options = args;
args = [];
}
Expand All @@ -150,6 +147,7 @@ class Generator extends Base {
this._namespace = this.options.namespace;
this._namespaceId = this.options.namespaceId;
this.yoGeneratorVersion = packageJson.version;
this.features = features || {unique: this.options.unique};

this.option('help', {
type: Boolean,
Expand Down Expand Up @@ -269,19 +267,35 @@ class Generator extends Base {
}
}

/**
* Configure Generator behaviours.
*
* @param {Object} features
* @param {boolean|string} [features.unique] - Generates a uniqueBy id for the environment
* Accepts 'namespace' or 'true' for one instance by namespace
* Accepts 'argument' for one instance by namespace and 1 argument
*
*/
setFeatures(features) {
Object.assign(this.features, features);
}

/**
* Specifications for Environment features.
*
* @return {Object}
*/
getFeatures() {
if (this.options.unique) {
if (this.features.unique) {
const {namespace} = this.options;
let uniqueBy;
if (this.options.unique === true || this.options.unique === 'namespace') {
if (
this.features.unique === true ||
this.features.unique === 'namespace'
) {
uniqueBy = namespace;
} else if (
this.options.unique === 'argument' &&
this.features.unique === 'argument' &&
this._args.length === 1
) {
const namespaceId = this.env
Expand All @@ -290,7 +304,7 @@ class Generator extends Base {
uniqueBy = namespaceId.id;
} else {
throw new Error(
`Error generating a uniqueBy value. Uniqueness ${this.options.unique} is not supported`
`Error generating a uniqueBy value. Uniqueness '${this.features.unique}' not supported by '${this.options.namespace}'`
);
}

Expand Down

0 comments on commit 9729a58

Please sign in to comment.