From 33d050f361286fde5f1d74551c22829c7e6a349a Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Sat, 20 Feb 2021 10:26:12 -0300 Subject: [PATCH] Implement getFeatures for singleton support. --- lib/index.js | 34 ++++++++++++++++++++++++++++++++++ test/base.js | 17 +++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/index.js b/lib/index.js index 1ee1df6b..c866ede4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 @@ -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 * diff --git a/test/base.js b/test/base.js index 83dd6ebf..8272b81f 100644 --- a/test/base.js +++ b/test/base.js @@ -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'); + }); + }); });