Skip to content

Commit

Permalink
✨ [F] Add scaffolding tool (#507)
Browse files Browse the repository at this point in the history
* ✨ Add new scaffolding tool

The tool is called Hygen, and we can use it to automate file creations in Homeday Blocks.

* ♻️ Remove unneeded fallback path in test.ejs.t

* ♻️ Remove unexisting attribute in Hygen help command
  • Loading branch information
kuroski authored Aug 17, 2020
1 parent 3eb6a83 commit d2fb6de
Show file tree
Hide file tree
Showing 13 changed files with 607 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ You can also follow build statuses in https://percy.io/Homeday/homeday-blocks
- `tests/` is an alias for `<rootDir>/tests/`
- The form components share many classes (mostly `field--*`), to avoid repeatedly defining some of them in each form test, [FIELD_CLASSES.js](https://github.com/homeday-de/homeday-blocks/blob/develop/tests/unit/components/form/FIELD_CLASSES.js) contains the most common classes we need for testing. Feel free to add more classes.

## Generators

We use [Hygen](https://www.hygen.io/) as a code generator tool to save time when we need to scaffold some structure.

Just run:

```
npm run new component
npm run new service
```

And follow the wizzard in order to generate a base component structure or a service.

## Contribution guide

This project follows [forking workflow](https://www.atlassian.com/git/tutorials/comparing-workflows/forking-workflow). See [project setup](#project-setup) to get started locally. That means that all code changes enter the project by PR to `develop` branch. Once you open the PR with suggested changes, the checks for `build` and `coverage` will run. If those fail, your PR needs some more work. :)
Expand Down
8 changes: 8 additions & 0 deletions _templates/new/component/main.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
inject: true
before: "Services"
eof_last: false
skip_if: <%= name %>
to: main.js
---
export { default as <%= name %> } from './src/<%= path %>/<%= name %>.vue';
27 changes: 27 additions & 0 deletions _templates/new/component/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const path = require('path');
const _ = require('lodash');

module.exports = [
{
type: 'input',
name: 'name',
message: "What's your component name?",
validate(value) {
if (!value.length) {
return 'Components must have a name.';
}
const fileName = _.kebabCase(value);
if (fileName.indexOf('-') === -1) {
return 'Component names should contain at least two words to avoid conflicts with existing and future HTML elements.';
}
return true;
},
},
{
type: 'input',
name: 'path',
message: "The path to the component? (default: 'components')",
initial: 'components',
result: value => path.normalize(value),
},
];
15 changes: 15 additions & 0 deletions _templates/new/component/story.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
to: src/stories/<%= name %>.stories.js
---
/* eslint-disable import/no-extraneous-dependencies */
import { storiesOf } from '@storybook/vue';
import { <%= name %> } from 'homeday-blocks';

const stories = storiesOf('Components|<%= name %>', module);

stories.add('Default', () => ({
components: { <%= name %> },
template: `
<<%= name %> />
`,
}));
26 changes: 26 additions & 0 deletions _templates/new/component/test.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
to: tests/unit/<%= path %>/<%= name %>.spec.js
---
import { wrapperFactoryBuilder } from 'tests/unit/helpers';
import <%= name %> from '@/<%= path %>/<%= name %>.vue';

const wrapperBuilder = wrapperFactoryBuilder(<%= name %>);

describe('<%= name %>', () => {
const build = (overrideProps = {}) => {
const props = {
...overrideProps,
};

const wrapper = wrapperBuilder({ props });

return {
wrapper,
};
};

it('should render', () => {
const { wrapper } = build();
expect(wrapper.html()).toMatchSnapshot();
});
});
18 changes: 18 additions & 0 deletions _templates/new/component/vue.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
to: src/<%= path %>/<%= name %>.vue
---
<template>
<div>
Your new awesome component: <%= name %>
</div>
</template>

<script>
export default {
name: '<%= name %>',
};
</script>

<style lang="scss" scoped>
@import 'homeday-blocks/src/styles/mixins.scss';
</style>
5 changes: 5 additions & 0 deletions _templates/new/help/index.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
message: |
- hygen new {bold component} --name [NAME] --path [PATH] (default: components)
- hygen new {bold service} --name [NAME]
---
7 changes: 7 additions & 0 deletions _templates/new/service/main.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
inject: true
append: true
skip_if: <%= name %>
to: main.js
---
export { default as <%= h.changeCase.camel(name) %>Service } from './src/services/<%= name %>';
13 changes: 13 additions & 0 deletions _templates/new/service/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = [
{
type: 'input',
name: 'name',
message: "What's your service name?",
validate(value) {
if (!value.length) {
return 'A service must have a name.';
}
return true;
},
},
];
8 changes: 8 additions & 0 deletions _templates/new/service/service.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
to: src/services/<%= name %>.js
---
export const myMethod = () => true;

export default {
myMethod,
};
10 changes: 10 additions & 0 deletions _templates/new/service/test.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
to: tests/unit/services/<%= name %>.spec.js
---
import { myMethod } from '@/services/<%= name %>';

describe('<%= h.changeCase.camel(name) %> service', () => {
it('works', () => {
expect(myMethod()).toBe(true);
});
});
Loading

0 comments on commit d2fb6de

Please sign in to comment.