From 35de7693bc083e42ff6d15106229d9635f0f1502 Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Wed, 5 Feb 2020 17:06:50 -0200 Subject: [PATCH] Implement createStorage convenience method (#1168) --- lib/index.js | 13 ++++++++++++- lib/util/storage.js | 2 +- test/generators.js | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9f65b46e..a2b1411f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -799,7 +799,18 @@ class Generator extends EventEmitter { /** * Return a storage instance. - * @param {String} rootName The rootName in which is stored inside .yo-rc.json + * @param {String} storePath The path of the json file + * @param {String} [rootName] The rootName in which is stored inside the json + * @return {Storage} json storage + * @private + */ + createStorage(storePath, rootName) { + return new Storage(rootName, this.fs, storePath); + } + + /** + * Return a storage instance. + * @param {String} [rootName] The rootName in which is stored inside .yo-rc.json * @return {Storage} Generator storage * @private */ diff --git a/lib/util/storage.js b/lib/util/storage.js index 088a1f54..274cb47a 100644 --- a/lib/util/storage.js +++ b/lib/util/storage.js @@ -21,7 +21,7 @@ const _ = require('lodash'); */ class Storage { constructor(name, fs, configPath) { - if (typeof name !== 'string') { + if (name !== undefined && typeof name !== 'string') { configPath = fs; fs = name; name = undefined; diff --git a/test/generators.js b/test/generators.js index c91e8a76..143393b2 100644 --- a/test/generators.js +++ b/test/generators.js @@ -51,4 +51,28 @@ describe('Generators module', () => { this.generator._globalConfig.path ); }); + + describe('#createStorage', function() { + before(function() { + this.generator = new Base({ + env: this.env, + resolved: 'test', + localConfigOnly: true + }); + }); + + it('with path and name', function() { + const global = path.join(this.env.cwd, '.yo-rc-global.json'); + const customStorage = this.generator.createStorage(global, '*'); + assert.equal(global, customStorage.path); + assert.equal('*', customStorage.name); + }); + + it('with path', function() { + const global = path.join(this.env.cwd, '.yo-rc-global.json'); + const customStorage = this.generator.createStorage(global); + assert.equal(global, customStorage.path); + assert.equal(undefined, customStorage.name); + }); + }); });