Skip to content

Commit 687bfd7

Browse files
srizzongleisonkz
authored andcommitted
✨ feat: generate api and common service
1 parent fae3379 commit 687bfd7

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { GluegunCommand, GluegunToolbox } from 'gluegun';
2+
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
3+
4+
import { printCreated } from '../../../../utils/functions.helper';
5+
6+
const COMMAND: GluegunCommand = {
7+
name: 'api',
8+
alias: ['a'],
9+
description: 'cria um serviço Angular do tipo Api',
10+
run: async (toolbox: GluegunToolbox) => {
11+
const { parameters, print, prompt, template, strings } = toolbox;
12+
13+
let serviceName = parameters.first;
14+
15+
if (!serviceName) {
16+
const response: GluegunAskResponse = await prompt.ask({
17+
type: 'input',
18+
name: 'serviceName',
19+
message: 'Qual o nome do serviço?',
20+
validate: (value: string) => {
21+
if (!value) {
22+
return 'O nome do serviço não pode ser vazio';
23+
}
24+
25+
return true;
26+
}
27+
});
28+
29+
serviceName = response.serviceName;
30+
}
31+
32+
const serviceNameKebab = strings.kebabCase(serviceName);
33+
34+
template.generate({
35+
template: 'service.template.ts.ejs',
36+
target: `./${serviceNameKebab}/${serviceNameKebab}.api.ts`,
37+
props: {
38+
type: 'api',
39+
name: serviceName,
40+
...strings
41+
}
42+
});
43+
44+
template.generate({
45+
template: 'service.template.spec.ts.ejs',
46+
target: `./${serviceNameKebab}/${serviceNameKebab}.api.spec.ts`,
47+
props: {
48+
type: 'api',
49+
name: serviceName,
50+
...strings
51+
}
52+
});
53+
54+
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.api.ts`);
55+
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.api.spec.ts`);
56+
}
57+
};
58+
59+
module.exports = COMMAND;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { GluegunCommand, GluegunToolbox } from 'gluegun';
2+
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
3+
4+
import { printCreated } from '../../../../utils/functions.helper';
5+
6+
const COMMAND: GluegunCommand = {
7+
name: 'common',
8+
alias: ['c'],
9+
description: 'cria um serviço Angular',
10+
run: async (toolbox: GluegunToolbox) => {
11+
const { parameters, print, prompt, template, strings } = toolbox;
12+
13+
let serviceName = parameters.first;
14+
15+
if (!serviceName) {
16+
const response: GluegunAskResponse = await prompt.ask({
17+
type: 'input',
18+
name: 'serviceName',
19+
message: 'Qual o nome do serviço?',
20+
validate: (value: string) => {
21+
if (!value) {
22+
return 'O nome do serviço não pode ser vazio';
23+
}
24+
25+
return true;
26+
}
27+
});
28+
29+
serviceName = response.serviceName;
30+
}
31+
32+
const serviceNameKebab = strings.kebabCase(serviceName);
33+
34+
template.generate({
35+
template: 'service.template.ts.ejs',
36+
target: `./${serviceNameKebab}/${serviceNameKebab}.service.ts`,
37+
props: {
38+
type: 'service',
39+
name: serviceName,
40+
...strings
41+
}
42+
});
43+
44+
template.generate({
45+
template: 'service.template.spec.ts.ejs',
46+
target: `./${serviceNameKebab}/${serviceNameKebab}.service.spec.ts`,
47+
props: {
48+
type: 'service',
49+
name: serviceName,
50+
...strings
51+
}
52+
});
53+
54+
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.service.ts`);
55+
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.service.spec.ts`);
56+
}
57+
};
58+
59+
module.exports = COMMAND;
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { GluegunCommand, GluegunToolbox } from 'gluegun';
2+
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
3+
4+
import { findCommand } from '../../../utils/functions.helper';
5+
6+
const COMMAND: GluegunCommand = {
7+
name: 'service',
8+
alias: ['s'],
9+
description: 'Cria um serviço Angular de tipo específico',
10+
11+
run: async (toolbox: GluegunToolbox) => {
12+
const { parameters, prompt } = toolbox;
13+
14+
let componentName = parameters.first;
15+
16+
const QUESTION = 'Qual tipo de serviço você deseja criar?';
17+
const TYPES = ['common', 'api'];
18+
19+
const componentTypeResponse: GluegunAskResponse = await prompt.ask({
20+
type: 'select',
21+
name: 'type',
22+
message: QUESTION,
23+
choices: TYPES
24+
});
25+
26+
const componentType = componentTypeResponse.type;
27+
const command = findCommand(toolbox, componentType);
28+
29+
toolbox.parameters.first = componentName;
30+
command.run(toolbox);
31+
}
32+
};
33+
34+
module.exports = COMMAND;
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { <%= pascalCase(props.name) %><%= pascalCase(props.type) %> } from './<%= kebabCase(props.name) %>.<%= kebabCase(props.type) %>';
4+
5+
describe('<%= pascalCase(props.name) %><%= pascalCase(props.type) %>', () => {
6+
let service: <%= pascalCase(props.name) %><%= pascalCase(props.type) %>;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(<%= pascalCase(props.name) %><%= pascalCase(props.type) %>);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});

src/templates/service.template.ts.ejs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable({
4+
providedIn: 'root'
5+
})
6+
export class <%= pascalCase(props.name) %><%= pascalCase(props.type) %> {}

0 commit comments

Comments
 (0)