diff --git a/__tests__/app.js b/__tests__/app.js index c7aee37..08aa371 100644 --- a/__tests__/app.js +++ b/__tests__/app.js @@ -69,4 +69,38 @@ describe('generator:app', () => { assert.fileContent('.eslintignore', '**/templates\n'); }); }); + + describe('scoped name', () => { + beforeEach(() => { + return helpers.run(path.join(__dirname, '../app')).withPrompts({ + name: '@yeoman/generator-temp', + description: 'A node generator', + homepage: 'http://yeoman.io', + githubAccount: 'yeoman', + authorName: 'The Yeoman Team', + authorEmail: 'hi@yeoman.io', + authorUrl: 'http://yeoman.io', + keywords: [], + license: 'MIT' + }); + }); + + it('created and CD into a folder named like the generator', () => { + assert.equal(path.basename(process.cwd()), 'generator-temp'); + }); + + it('fills package.json with correct information', () => { + // eslint-disable-next-line new-cap + assert.JSONFileContent('package.json', { + name: '@yeoman/generator-temp' + }); + }); + + it('fills the README with project data', () => { + assert.fileContent('README.md', '# @yeoman/generator-temp'); + assert.fileContent('README.md', 'npm install -g yo'); + assert.fileContent('README.md', 'npm install -g @yeoman/generator-temp'); + assert.fileContent('README.md', 'yo @yeoman/temp'); + }); + }); }); diff --git a/app/index.js b/app/index.js index 6c55144..d9a8691 100644 --- a/app/index.js +++ b/app/index.js @@ -6,10 +6,27 @@ const _ = require('lodash'); const extend = require('deep-extend'); const mkdirp = require('mkdirp'); +function parseScopedName(name) { + const nameFragments = name.split('/'); + const parseResult = { + scopeName: '', + localName: name + }; + + if (nameFragments.length > 1) { + parseResult.scopeName = nameFragments[0]; + parseResult.localName = nameFragments[1]; + } + + return parseResult; +} + function makeGeneratorName(name) { + const parsedName = parseScopedName(name); + name = parsedName.localName; name = _.kebabCase(name); name = name.indexOf('generator-') === 0 ? name : 'generator-' + name; - return name; + return parsedName.scopeName ? `${parsedName.scopeName}/${name}` : name; } module.exports = class extends Generator { @@ -31,16 +48,17 @@ module.exports = class extends Generator { this ).then(props => { this.props.name = props.name; + Object.assign(this.props, parseScopedName(props.name)); }); } default() { - if (path.basename(this.destinationPath()) !== this.props.name) { + if (path.basename(this.destinationPath()) !== this.props.localName) { this.log( - `Your generator must be inside a folder named ${this.props.name}\nI'll automatically create this folder.` + `Your generator must be inside a folder named ${this.props.localName}\nI'll automatically create this folder.` ); - mkdirp.sync(this.props.name); - this.destinationRoot(this.destinationPath(this.props.name)); + mkdirp.sync(this.props.localName); + this.destinationRoot(this.destinationPath(this.props.localName)); } const readmeTpl = _.template(this.fs.read(this.templatePath('README.md')));