Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a Polymer 3 element template to init. #115

Merged
merged 13 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ gulp.task('depcheck', ['build'], () => {
'babel-plugin-external-helpers',
'polymer-bundler',
],
ignoreDirs: [
'templates',
],
})
.then((result) => {
let invalidFiles = Object.keys(result.invalidFiles) || [];
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/init/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function createApplicationGenerator(templateName: string):

constructor(args: string|string[], options: any) {
super(args, options);
this.sourceRoot(path.join(__dirname, 'templates', templateName));
this.sourceRoot(path.join(__dirname, '../../../templates/application', templateName));
}

// This is necessary to prevent an exception in Yeoman when creating
Expand Down Expand Up @@ -116,4 +116,4 @@ export function createApplicationGenerator(templateName: string):
'Check out your new project README for information about what to do next.\n');
}
};
}
}
42 changes: 40 additions & 2 deletions packages/cli/src/init/element/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const logger = logging.getLogger('init');
*/
export function createElementGenerator(templateName: string):
(typeof Generator) {
return class ElementGenerator extends Generator {
class ElementGenerator extends Generator {
props: any;

constructor(args: string|string[], options: any) {
super(args, options);
this.sourceRoot(path.join(__dirname, 'templates', templateName));
this.sourceRoot(path.join(__dirname, '../../../templates/element', templateName));
}

// This is necessary to prevent an exception in Yeoman when creating
Expand Down Expand Up @@ -119,4 +119,42 @@ export function createElementGenerator(templateName: string):
'Check out your new project README for information about what to do next.\n');
}
};

class Polymer3ElementGenerator extends ElementGenerator {
// TODO(bicknellr): Why doesn't this inherit properly? Because `async` is
// compiled out?
async prompting() {
return super.prompting();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check that this doesn't work when this isn't here? I looked at the JS that's produced, and we're producing ES6 here. I can't see any reason why this code would do anything at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like async-await is being compiled out. I'm betting it has something to do with that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, yes, the parent class' prompting function isn't called if this isn't here. I can't figure out where yeoman-generator calls it though and I'm not sure how it gets called due to the async-await thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, telling TypeScript to target ES2017 still has this problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the problem: https://github.com/yeoman/generator/blob/v1.0.1/lib/index.js#L382
Yeoman only checks the object's prototype's own properties, so I think I have to have something like this in here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added pass-through functions for all of the other methods as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hey, you found it too. I just filed yeoman/generator#1065


writing() {
const name = this.props.name;

this.fs.copyTpl(
`${this.templatePath()}/**/?(.)!(_)*`,
this.destinationPath(),
this.props);

this.fs.copyTpl(
this.templatePath('_element.js'), `${name}.js`, this.props);

this.fs.copyTpl(
this.templatePath('test/_element_test.html'),
`test/${name}_test.html`,
this.props);

this.fs.copyTpl(
this.templatePath('test/index.html'), `test/index.html`, this.props);

this.fs.copyTpl(
this.templatePath('.gitignore'), '.gitignore', this.props);
}
}

switch (templateName) {
case 'polymer-3.x':
return Polymer3ElementGenerator;
default:
return ElementGenerator;
}
}
5 changes: 5 additions & 0 deletions packages/cli/src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ interface GeneratorInfo {
}

const localGenerators: {[name: string]: GeneratorInfo} = {
'polymer-3-element': {
id: 'polymer-init-polymer-3-element:app',
description: 'A simple Polymer 3.0 element template',
generator: createElementGenerator('polymer-3.x'),
},
'polymer-2-element': {
id: 'polymer-init-polymer-2-element:app',
description: 'A simple Polymer 2.0 element template',
Expand Down
1 change: 1 addition & 0 deletions packages/cli/templates/element/polymer-3.x/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
21 changes: 21 additions & 0 deletions packages/cli/templates/element/polymer-3.x/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# \<<%= name %>\>

<%= description %>

## Install the Polymer-CLI

First, make sure you have the [Polymer CLI](https://www.npmjs.com/package/polymer-cli) and npm (packaged with [Node.js](https://nodejs.org)) installed. Run `npm install` to install your element's dependencies, then run `polymer serve` to serve your element locally.

## Viewing Your Element

```
$ polymer serve
```

## Running Tests

```
$ polymer test
```

Your application is already set up to be tested via [web-component-tester](https://github.com/Polymer/web-component-tester). Run `polymer test` to run your application's test suite locally.
32 changes: 32 additions & 0 deletions packages/cli/templates/element/polymer-3.x/_element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {PolymerElement} from '@polymer/polymer/polymer-element.js';

/**
* `<%= name %>`
* <%= description %>
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class <%= elementClassName %> extends PolymerElement {
static get template() {
return `
<style>
:host {
display: block;
}
</style>
<h2>Hello [[prop1]]!</h2>
`;
}
static get properties() {
return {
prop1: {
type: String,
value: '<%= name %>',
},
};
}
}

window.customElements.define('<%= name %>', <%= elementClassName %>);
30 changes: 30 additions & 0 deletions packages/cli/templates/element/polymer-3.x/demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title><%= name %> demo</title>

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>

<script type="module" src="../node_modules/@polymer/iron-demo-helpers/demo-pages-shared-styles.js"></script>
<script type="module" src="../node_modules/@polymer/iron-demo-helpers/demo-snippet.js"></script>
<script type="module" src="../<%= name %>.js"></script>

<custom-style>
<style is="custom-style" include="demo-pages-shared-styles">
</style>
</custom-style>
</head>
<body>
<div class="vertical-section-container centered">
<h3>Basic <%= name %> demo</h3>
<demo-snippet>
<template>
<<%= name %>></<%= name %>>
</template>
</demo-snippet>
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions packages/cli/templates/element/polymer-3.x/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0;url=demo/" />
<title><%= name %></title>
</head>
<body>
<!--
Visit demo/index.html to see live examples of your element running.
This page will automatically redirect you there when run in the browser
with `polymer serve`.
-->
</body>
</html>
14 changes: 14 additions & 0 deletions packages/cli/templates/element/polymer-3.x/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "<%= name %>",
<% if (description) { -%> "description": "<%= description %>",
<% } -%>
"main": "<%= name %>.js",
"dependencies": {
"@polymer/polymer": "^3.0.0-pre.12"
},
"devDependencies": {
"@polymer/iron-demo-helpers": "^3.0.0-pre.12",
"@webcomponents/webcomponentsjs": "^1.0.0",
"wct-browser-legacy": "^0.0.1-pre.11"
}
}
4 changes: 4 additions & 0 deletions packages/cli/templates/element/polymer-3.x/polymer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"npm": true,
"moduleResolution": "node"
}
52 changes: 52 additions & 0 deletions packages/cli/templates/element/polymer-3.x/test/_element_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title><%= name %> test</title>

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/wct-browser-legacy/browser.js"></script>

<script type="module" src="../<%= name %>.js"></script>
</head>
<body>

<test-fixture id="BasicTestFixture">
<template>
<<%= name %>></<%= name %>>
</template>
</test-fixture>

<test-fixture id="ChangedPropertyTestFixture">
<template>
<<%= name %> prop1="new-prop1"></<%= name %>>
</template>
</test-fixture>

<script type="module">
suite('<%= name %>', () => {

test('instantiating the element with default properties works', () => {
const element = fixture('BasicTestFixture');
assert.equal(element.prop1, '<%= name %>');
const elementShadowRoot = element.shadowRoot;
const elementHeader = elementShadowRoot.querySelector('h2');
assert.equal(elementHeader.innerHTML, 'Hello <%= name %>!');
});

test('setting a property on the element works', () => {
// Create a test fixture
const element = fixture('ChangedPropertyTestFixture');
assert.equal(element.prop1, 'new-prop1');
const elementShadowRoot = element.shadowRoot;
const elementHeader = elementShadowRoot.querySelector('h2');
assert.equal(elementHeader.innerHTML, 'Hello new-prop1!');
});

});
</script>

</body>
</html>
17 changes: 17 additions & 0 deletions packages/cli/templates/element/polymer-3.x/test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">

<script src="../node_modules/wct-browser-legacy/browser.js"></script>
</head>
<body>
<script>
// Load and run all tests (.html, .js):
WCT.loadSuites([
'<%= name %>_test.html'
]);
</script>
</body>
</html>