Skip to content

Commit

Permalink
Implement getFeatures for singleton support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 20, 2021
1 parent 99ac2c5 commit 33d050f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ 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
Expand Down Expand Up @@ -266,6 +269,37 @@ class Generator extends Base {
}
}

/**
* Specifications for Environment features.
*
* @return {Object}
*/
getFeatures() {
if (this.options.unique) {
const {namespace} = this.options;
let uniqueBy;
if (this.options.unique === true || this.options.unique === 'namespace') {
uniqueBy = namespace;
} else if (
this.options.unique === 'argument' &&
this._args.length === 1
) {
const namespaceId = this.env
.requireNamespace(namespace)
.with({instanceId: this._args[0]});
uniqueBy = namespaceId.id;
} else {
throw new Error(
`Error generating a uniqueBy value. Uniqueness ${this.options.unique} is not supported`
);
}

return {uniqueBy};
}

return {};
}

/**
* Register priorities for this generator
*
Expand Down
17 changes: 17 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1784,4 +1784,21 @@ describe('Base', () => {
});
});
});

describe.only('#getFeatures', () => {
it('should return namespace as uniqueBy when unique is true', function () {
const gen = new Base([], {unique: true, namespace: 'foo', env: this.env});
assert.equal(gen.getFeatures().uniqueBy, 'foo');
});

it("should return namespace as uniqueBy when unique is 'namespace'", function () {
const gen = new Base([], {unique: 'namespace', namespace: 'foo', env: this.env});
assert.equal(gen.getFeatures().uniqueBy, 'foo');
});

it("should return namespace with first argument as uniqueBy when unique is 'namespace'", function () {
const gen = new Base(['bar'], {unique: 'argument', namespace: 'foo', env: this.env});
assert.equal(gen.getFeatures().uniqueBy, 'foo#bar');
});
});
});

0 comments on commit 33d050f

Please sign in to comment.