diff --git a/readme.md b/readme.md index 524848f..5c58b8f 100644 --- a/readme.md +++ b/readme.md @@ -67,12 +67,15 @@ yo polymer:el my-element ### Seed Generates a reusable polymer element based on the [seed-element workflow](http://www.polymer-project.org/docs/start/reusableelements.html). **This should only be used if you're creating a standalone element repo that you intend to share with others via bower.** If you're just building a Polymer app, stick to the [El](#el) generator. -To create a seed-element you'll first need to create a parent directory, then a sub directory to hold your seed-element. All bower dependencies will be installed into the parent directory. Please follow the [seed-element guide](http://www.polymer-project.org/docs/start/reusableelements.html) for more instructions. +The seed-element generator will construct a new element _and_ its directory for +you. Be aware: all bower dependencies will be installed as _siblings_ of the +newly generated element. Make sure that you generate the seed element within a +directory that is intended to contain multiple components! Example: ```bash -mkdir -p components/x-foo && cd $_ -yo polymer:seed +mkdir -p components && cd $_ +yo polymer:seed x-foo ``` ### Gh diff --git a/seed/index.js b/seed/index.js index 12f00e6..dea45f1 100644 --- a/seed/index.js +++ b/seed/index.js @@ -7,6 +7,11 @@ module.exports = yeoman.generators.Base.extend({ constructor: function () { yeoman.generators.Base.apply(this, arguments); + this.argument('element-name', { + desc: 'Tag name of the element and directory to generate.', + required: true, + }); + this.option('skip-install', { desc: 'Whether bower dependencies should be installed', defaults: false, @@ -17,43 +22,39 @@ module.exports = yeoman.generators.Base.extend({ defaults: false, }); }, + validate: function () { + this.elementName = this['element-name']; + if (this.elementName.indexOf('-') === -1) { + this.emit('error', new Error( + 'Element name must contain a dash "-"\n' + + 'ex: yo polymer:seed my-element' + )); + } + + // Construct the element as a subdirectory. + this.destinationRoot(this.elementName); + }, askFor: function () { var done = this.async(); // Have Yeoman greet the user. - this.log(yosay('Out of the box I include the Polymer seed-element.')); + this.log(yosay('Out of the box I follow the seed-element pattern.')); var defaultName = path.basename(process.cwd()); var prompts = [ { name: 'ghUser', message: 'What is your GitHub username?' - }, - { - name: 'elementName', - message: 'What is your element\'s name', - default: defaultName } ]; this.prompt(prompts, function (props) { this.ghUser = props.ghUser; - this.elementName = props.elementName; done(); }.bind(this)); }, seed: function () { - if (this.elementName.indexOf('-') === -1) { - console.error( - 'The element name you provided: ' + this.elementName + ' ' + - 'is invalid.' - ); - console.error('Element name must contain a dash "-"'); - console.error('ex: my-element'); - return; - } - this.copy('gitignore', '.gitignore'); this.copy('gitattributes', '.gitattributes'); this.copy('bowerrc', '.bowerrc'); diff --git a/test/seed-test.js b/test/seed-test.js index 58caf08..3178f75 100644 --- a/test/seed-test.js +++ b/test/seed-test.js @@ -4,8 +4,10 @@ var path = require('path'); var helpers = require('yeoman-generator').test; describe('yo polymer:seed test', function () { + var testDir = path.join(__dirname, 'temp'); + before(function (done) { - helpers.testDirectory(path.join(__dirname, 'temp'), function (err) { + helpers.testDirectory(testDir, function (err) { if (err) { return done(err); } @@ -15,7 +17,7 @@ describe('yo polymer:seed test', function () { helpers.createDummyGenerator(), 'mocha:seed' ] - ]); + ], 'x-foo'); this.polymer.options['skip-install'] = true; done(); @@ -29,19 +31,19 @@ describe('yo polymer:seed test', function () { it('creates expected files', function (done) { var expected = [ - ['bower.json', /"name": "x-foo"/, /"main": "x-foo.html"/], - '.bowerrc', - '.editorconfig', - '.gitignore', - '.jshintrc', - 'demo.html', - 'index.html', - 'README.md', - 'x-foo.css', - 'x-foo.html', - 'test/index.html', - 'test/tests.html', - 'test/x-foo-basic.html', + ['x-foo/bower.json', /"name": "x-foo"/, /"main": "x-foo.html"/], + 'x-foo/.bowerrc', + 'x-foo/.editorconfig', + 'x-foo/.gitignore', + 'x-foo/.jshintrc', + 'x-foo/demo.html', + 'x-foo/index.html', + 'x-foo/README.md', + 'x-foo/x-foo.css', + 'x-foo/x-foo.html', + 'x-foo/test/index.html', + 'x-foo/test/tests.html', + 'x-foo/test/x-foo-basic.html', ]; helpers.mockPrompt(this.polymer, { @@ -50,6 +52,7 @@ describe('yo polymer:seed test', function () { }); this.polymer.run({}, function () { + process.chdir(testDir); helpers.assertFiles(expected); done(); });