Skip to content

Commit

Permalink
Implement createStorage convenience method (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 5, 2020
1 parent 73271b8 commit 35de769
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions test/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 35de769

Please sign in to comment.