Skip to content

Commit 224243e

Browse files
authored
feat(close #1): init source (#2)
1 parent 715e384 commit 224243e

File tree

10 files changed

+2965
-24
lines changed

10 files changed

+2965
-24
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: create-github-release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v[0-9]+.[0-9]+.[0-9]+'
7+
8+
jobs:
9+
create-github-release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/create-release@v1
14+
env:
15+
# This token is provided by Actions, you do not need to create your own token
16+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
17+
with:
18+
tag_name: ${{github.ref}}
19+
release_name: Release ${{github.ref}}

.github/workflows/publish-npm.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: publish-npm
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v[0-9]+.[0-9]+.[0-9]+'
7+
8+
jobs:
9+
publish-npm:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/setup-node@v1
14+
with:
15+
node-version: 12
16+
registry-url: https://registry.npmjs.org
17+
- run: yarn publish --access public
18+
env:
19+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

README.md

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,68 @@
11
<div align="center">
2-
<h1>repo-template</h1>
3-
<p>Boring GitHub Repository Template</p>
4-
5-
<div>
6-
<a href="https://github.com/boringcodes/repo-template/commits" aria-label="Commitizen Friendly">
2+
<h1>create-service-component</h1>
3+
<p>Boring Express Microservice Component Generator</p>
4+
5+
<p>
6+
<a href="https://github.com/boringcodes/create-service-component/commits" aria-label="Commitizen Friendly">
77
<img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square">
88
</a>
9-
<a href="https://github.com/boringcodes/repo-template/actions" aria-label="GitHub Workflow Status">
10-
<img src="https://img.shields.io/github/workflow/status/boringcodes/repo-template/main-workflow?style=flat-square">
9+
<a href="https://github.com/boringcodes/create-service-component/actions" aria-label="GitHub Workflow Status">
10+
<img src="https://img.shields.io/github/workflow/status/boringcodes/create-service-component/publish-npm?style=flat-square">
1111
</a>
12-
<a href="https://david-dm.org/boringcodes/repo-template" aria-label="Dependencies Status">
13-
<img src="https://img.shields.io/david/boringcodes/repo-template?style=flat-square">
12+
<a href="https://david-dm.org/boringcodes/create-service-component" aria-label="Dependencies Status">
13+
<img src="https://img.shields.io/david/boringcodes/create-service-component?style=flat-square">
1414
</a>
15-
<a href="https://www.npmjs.com/package/@boringcodes/repo-template" aria-label="NPM Version">
16-
<img src="https://img.shields.io/npm/v/@boringcodes/repo-template?color=brightgreen&style=flat-square">
15+
<a href="https://www.npmjs.com/package/generator-create-service-component" aria-label="NPM Version">
16+
<img src="https://img.shields.io/npm/v/generator-create-service-component?color=brightgreen&style=flat-square">
1717
</a>
18-
<a href="https://www.npmjs.com/package/@boringcodes/repo-template" aria-label="NPM Downloads">
19-
<img src="https://img.shields.io/npm/dm/@boringcodes/repo-template?style=flat-square">
18+
<a href="https://www.npmjs.com/package/generator-create-service-component" aria-label="NPM Downloads">
19+
<img src="https://img.shields.io/npm/dm/generator-create-service-component?style=flat-square">
2020
</a>
21-
<a href="https://github.com/boringcodes/repo-template/blob/master/LICENSE" aria-label="MIT License">
22-
<img src="https://img.shields.io/github/license/boringcodes/repo-template?color=brightgreen&style=flat-square">
21+
<a href="https://github.com/boringcodes/create-service-component/blob/master/LICENSE" aria-label="MIT License">
22+
<img src="https://img.shields.io/github/license/boringcodes/create-service-component?color=brightgreen&style=flat-square">
2323
</a>
2424
<a href="https://github.com/boringcodes" aria-label="BoringCodes Verified">
2525
<img src="https://img.shields.io/badge/boringcodes-verified-brightgreen?style=flat-square">
2626
</a>
27-
</div>
27+
</p>
28+
29+
<img src="banner.png">
2830
</div>
2931

30-
## Installation
32+
## Install
33+
34+
Make sure `yeoman` is installed
35+
36+
```sh
37+
$ yarn global add yo
38+
```
3139

32-
Use the package manager to install foobar.
40+
Then install the generator
3341

34-
```bash
35-
yarn install
42+
```sh
43+
$ yarn global add generator-create-service-component
3644
```
3745

3846
## Usage
3947

40-
```javascript
41-
import foobar from 'foobar';
48+
Navigate to the created service root dir (using [@boringcodes/create-service](https://github.com/boringcodes/create-service)) and run the following command to generate new component
4249

43-
foobar.start();
50+
```sh
51+
$ yo create-service-component
52+
```
53+
54+
This scaffolds out:
55+
56+
```
57+
├── src
58+
│ ├── components
59+
│ │   ├── componentNames
60+
│ │   │   ├── constants.ts
61+
│ │   │   ├── controller.ts
62+
│ │   │   ├── index.ts
63+
│ │   └── ...
64+
│ ├── ...
65+
├── ...
4466
```
4567

4668
## Contributing
@@ -55,4 +77,4 @@ Please make sure to update tests as appropriate.
5577

5678
## License
5779

58-
[MIT](https://github.com/boringcodes/repo-template/blob/master/LICENSE)
80+
[MIT](https://github.com/boringcodes/create-service-component/blob/master/LICENSE)

banner.png

43.9 KB
Loading

generators/app/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const Generator = require('yeoman-generator');
2+
const chalk = require('chalk');
3+
const yosay = require('yosay');
4+
const pluralize = require('pluralize');
5+
6+
const pkg = require('../../package.json');
7+
8+
module.exports = class extends Generator {
9+
async prompting() {
10+
this.log(yosay(`Welcome to the ${chalk.red(pkg.name)} generator!`));
11+
12+
const prompts = [
13+
{
14+
type: 'input',
15+
name: 'elementComponentName',
16+
message: 'Name of the new component (singular)?',
17+
default: 'thing',
18+
},
19+
];
20+
21+
return this.prompt(prompts).then(props => {
22+
this.props = {
23+
...props,
24+
elementComponentPluralName: pluralize(props.elementComponentName),
25+
};
26+
});
27+
}
28+
29+
writing() {
30+
this.fs.copyTpl(
31+
[this.templatePath('**/*'), this.templatePath('**/.*')],
32+
this.destinationPath('src/components'),
33+
this.props,
34+
);
35+
}
36+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const NAME = '<%= elementComponentName %>';
2+
const PLURAL_NAME = '<%= elementComponentPluralName %>';
3+
4+
export { NAME, PLURAL_NAME };
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Request as ExpressRequest, Response, NextFunction } from 'express';
2+
import {
3+
BAD_REQUEST,
4+
NOT_FOUND,
5+
INTERNAL_SERVER_ERROR,
6+
} from 'http-status-codes';
7+
import { HttpError } from '@boringcodes/utils/error';
8+
9+
import { NAME } from './constants';
10+
11+
interface Request extends ExpressRequest {
12+
readonly [NAME]: any;
13+
}
14+
15+
const list = async (_: Request, res: Response, next: NextFunction) => {
16+
try {
17+
// TODO: list objects
18+
const objects = [];
19+
20+
res.send(objects);
21+
} catch (err) {
22+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
23+
}
24+
};
25+
26+
const count = async (_: Request, res: Response, next: NextFunction) => {
27+
try {
28+
// TODO: count objects
29+
const count = 0;
30+
31+
res.send({ count });
32+
} catch (err) {
33+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
34+
}
35+
};
36+
37+
const create = async (_: Request, res: Response, next: NextFunction) => {
38+
try {
39+
// TODO: create object
40+
const object = {};
41+
42+
res.send(object);
43+
} catch (err) {
44+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
45+
}
46+
};
47+
48+
const getById = async (req: Request, _: Response, next: NextFunction) => {
49+
if (!req.params.id) {
50+
next(new HttpError(BAD_REQUEST, 'Invalid resource Id'));
51+
52+
return;
53+
}
54+
55+
try {
56+
// TODO: get object
57+
const object = {};
58+
59+
if (!object) {
60+
next(new HttpError(NOT_FOUND, 'Resource not found'));
61+
62+
return;
63+
}
64+
// tslint:disable-next-line:no-object-mutation
65+
Object.assign(req, { [NAME]: object });
66+
67+
next();
68+
} catch (err) {
69+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
70+
}
71+
};
72+
73+
const get = (req: Request, res: Response) => {
74+
res.send(req[NAME]);
75+
};
76+
77+
const update = async (req: Request, res: Response, next: NextFunction) => {
78+
try {
79+
// TODO: update object
80+
const object = req[NAME];
81+
82+
res.send(object);
83+
} catch (err) {
84+
next(err);
85+
}
86+
};
87+
88+
const del = async (req: Request, res: Response, next: NextFunction) => {
89+
try {
90+
// TODO: delete object
91+
const object = req[NAME];
92+
93+
res.send(object);
94+
} catch (err) {
95+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
96+
}
97+
};
98+
99+
export { list, create, count, getById, get, update, del };
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Router } from 'express';
2+
3+
import { Routes } from '../types';
4+
import { PLURAL_NAME } from './constants';
5+
import { list, count, create, getById, get, update, del } from './controller';
6+
7+
const path = `/${PLURAL_NAME}`;
8+
9+
const routes = (_: Routes) => {
10+
const router = Router();
11+
12+
router.param('id', getById);
13+
14+
router
15+
.route('/')
16+
.get(list)
17+
.post(create);
18+
19+
router
20+
.route('/count')
21+
.get(count);
22+
23+
router
24+
.route('/:id')
25+
.get(get)
26+
.put(update)
27+
.delete(del);
28+
29+
return router;
30+
};
31+
32+
export default { path, routes };

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "generator-create-service-component",
3+
"version": "1.1.1",
4+
"description": "Boring Express Microservice Component Generator",
5+
"homepage": "https://github.com/boringcodes/create-service-component",
6+
"bugs": "https://github.com/boringcodes/create-service-component/issues",
7+
"license": "MIT",
8+
"author": "BoringCodes <hi@boringcodes.com> (https://boringcodes.com)",
9+
"repository": "https://github.com/boringcodes/create-service-component.git",
10+
"main": "generators/app/index.js",
11+
"files": [
12+
"generators"
13+
],
14+
"scripts": {
15+
"purge": "rm -rf node_modules",
16+
"release": "standard-version",
17+
"release:major": "yarn release --release-as major",
18+
"release:minor": "yarn release --release-as minor",
19+
"release:patch": "yarn release --release-as patch"
20+
},
21+
"keywords": [
22+
"yeoman-generator",
23+
"create-service-component"
24+
],
25+
"dependencies": {
26+
"chalk": "^3.0.0",
27+
"pluralize": "^8.0.0",
28+
"yeoman-generator": "^4.5.0",
29+
"yosay": "^2.0.2"
30+
},
31+
"devDependencies": {
32+
"standard-version": "^7.1.0"
33+
}
34+
}

0 commit comments

Comments
 (0)