Skip to content

Commit ddc3cba

Browse files
committed
🐛 fix: path parameter on service command
1 parent b8cc020 commit ddc3cba

File tree

9 files changed

+88
-76
lines changed

9 files changed

+88
-76
lines changed

src/commands/generate/component/common/common.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';
2-
import { getComponentName, getComponentPath, printCreated } from '../../../../utils/functions.helper';
2+
3+
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';
34

45
const COMMAND: GluegunCommand = {
56
name: 'common',
@@ -12,7 +13,7 @@ const COMMAND: GluegunCommand = {
1213
} = parameters;
1314

1415
const componentName = parameters.first ?? (await getComponentName(prompt));
15-
const componentPath = getComponentPath(path, componentName);
16+
const componentPath = getEntityPath(path, componentName);
1617

1718
template.generate({
1819
template: 'component.template.html.ejs',

src/commands/generate/component/dialog/dialog.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';
22

3-
import {
4-
getComponentName, getComponentPath, printCreated
5-
} from '../../../../utils/functions.helper';
3+
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';
64

75
const COMMAND: GluegunCommand = {
86
name: 'dialog',
@@ -15,7 +13,7 @@ const COMMAND: GluegunCommand = {
1513
} = parameters;
1614

1715
const componentName = parameters.first ?? (await getComponentName(prompt));
18-
const componentPath = getComponentPath(path, componentName);
16+
const componentPath = getEntityPath(path, componentName);
1917

2018
template.generate({
2119
template: 'component.template.html.ejs',

src/commands/generate/component/page/page.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { GluegunCommand, GluegunToolbox } from 'gluegun';
22

3-
import {
4-
getComponentName, getComponentPath, printCreated
5-
} from '../../../../utils/functions.helper';
3+
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';
64

75
const COMMAND: GluegunCommand = {
86
name: 'page',
@@ -15,7 +13,7 @@ const COMMAND: GluegunCommand = {
1513
} = parameters;
1614

1715
const componentName = parameters.first ?? (await getComponentName(prompt));
18-
const componentPath = getComponentPath(path, componentName);
16+
const componentPath = getEntityPath(path, componentName);
1917

2018
template.generate({
2119
template: 'component.template.html.ejs',

src/commands/generate/component/widget/widget.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { GluegunCommand, strings } from 'gluegun';
22

3-
import {
4-
getComponentName, getComponentPath, printCreated
5-
} from '../../../../utils/functions.helper';
3+
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';
64

75
const COMMAND: GluegunCommand = {
86
name: 'widget',
@@ -15,7 +13,7 @@ const COMMAND: GluegunCommand = {
1513
} = parameters;
1614

1715
const componentName = parameters.first ?? (await getComponentName(prompt));
18-
const componentPath = getComponentPath(path, componentName);
16+
const componentPath = getEntityPath(path, componentName);
1917

2018
template.generate({
2119
template: 'component.template.html.ejs',

src/commands/generate/service/api/api.test.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { runNgxdCLI } from '../../../../utils/cli-test-setup';
44

55
describe('Commands: [Generate] => [Service] => [Api]', () => {
66
const name = 'gsa';
7+
const TESTING_DIR = '__GSA_TEST__';
8+
const COMMAND = 'g s a';
79

810
beforeEach(() => {
911
jest.useFakeTimers();
@@ -15,7 +17,7 @@ describe('Commands: [Generate] => [Service] => [Api]', () => {
1517
});
1618

1719
test('should generate a api service with 2 files', async () => {
18-
await runNgxdCLI(`g s a ${name}`);
20+
await runNgxdCLI(`${COMMAND} ${name}`);
1921

2022
const ts = filesystem.read(`${name}/${name}.api.ts`);
2123
const spec = filesystem.read(`${name}/${name}.api.spec.ts`);
@@ -25,10 +27,24 @@ describe('Commands: [Generate] => [Service] => [Api]', () => {
2527
filesystem.remove(`${name}`);
2628
});
2729

30+
test('should generate a api service at given path', async () => {
31+
const path = `${TESTING_DIR}`;
32+
33+
await runNgxdCLI(`${COMMAND} ${name} --path ${path}`);
34+
35+
const ts = filesystem.read(`${path}/${name}/${name}.api.ts`);
36+
const spec = filesystem.read(`${path}/${name}/${name}.api.spec.ts`);
37+
38+
expect(ts).toBeDefined();
39+
expect(spec).toBeDefined();
40+
41+
filesystem.remove(TESTING_DIR);
42+
});
43+
2844
test('should generate a api service with correct content ', async () => {
2945
const name = 'fruit';
3046

31-
await runNgxdCLI(`g s a ${name}`);
47+
await runNgxdCLI(`${COMMAND} ${name}`);
3248

3349
const ts = filesystem.read(`${name}/${name}.api.ts`);
3450
const spec = filesystem.read(`${name}/${name}.api.spec.ts`);

src/commands/generate/service/api/api.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import { GluegunCommand, GluegunToolbox } from 'gluegun';
2-
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
1+
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';
32

4-
import { printCreated } from '../../../../utils/functions.helper';
3+
import { getEntityName, getEntityPath, printCreated } from '../../../../utils/functions.helper';
54

65
const COMMAND: GluegunCommand = {
76
name: 'api',
87
alias: ['a'],
98
description: 'cria um serviço Angular do tipo Api',
109
run: async (toolbox: GluegunToolbox) => {
11-
const { parameters, print, prompt, template, strings } = toolbox;
10+
const { parameters, print, prompt, template } = toolbox;
11+
const {
12+
options: { path }
13+
} = parameters;
1214

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);
15+
const serviceName = parameters.first ?? (await getEntityName(prompt, 'service'));
16+
const servicePath = getEntityPath(path, serviceName);
3317

3418
template.generate({
3519
template: 'service.template.ts.ejs',
36-
target: `./${serviceNameKebab}/${serviceNameKebab}.api.ts`,
20+
target: `${servicePath}.api.ts`,
3721
props: {
3822
type: 'api',
3923
name: serviceName,
@@ -43,16 +27,16 @@ const COMMAND: GluegunCommand = {
4327

4428
template.generate({
4529
template: 'service.template.spec.ts.ejs',
46-
target: `./${serviceNameKebab}/${serviceNameKebab}.api.spec.ts`,
30+
target: `${servicePath}.api.spec.ts`,
4731
props: {
4832
type: 'api',
4933
name: serviceName,
5034
...strings
5135
}
5236
});
5337

54-
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.api.ts`);
55-
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.api.spec.ts`);
38+
printCreated(print, `${servicePath}.api.ts`);
39+
printCreated(print, `${servicePath}.api.spec.ts`);
5640
}
5741
};
5842

src/commands/generate/service/common/common.test.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { runNgxdCLI } from '../../../../utils/cli-test-setup';
44

55
describe('Commands: [Generate] => [Service] => [Common]', () => {
66
const name = 'gsc';
7+
const TESTING_DIR = '__GSC_TEST__';
8+
const COMMAND = 'g s c';
79

810
beforeEach(() => {
911
jest.useFakeTimers();
@@ -15,7 +17,7 @@ describe('Commands: [Generate] => [Service] => [Common]', () => {
1517
});
1618

1719
test('should generate a common service with 2 files', async () => {
18-
await runNgxdCLI(`g s c ${name}`);
20+
await runNgxdCLI(`${COMMAND} ${name}`);
1921

2022
const ts = filesystem.read(`${name}/${name}.service.ts`);
2123
const spec = filesystem.read(`${name}/${name}.service.spec.ts`);
@@ -25,10 +27,24 @@ describe('Commands: [Generate] => [Service] => [Common]', () => {
2527
filesystem.remove(`${name}`);
2628
});
2729

30+
test('should generate a common service at given path', async () => {
31+
const path = `${TESTING_DIR}/src/app/services`;
32+
33+
await runNgxdCLI(`${COMMAND} ${name} --path ${path}`);
34+
35+
const ts = filesystem.read(`${path}/${name}/${name}.service.ts`);
36+
const spec = filesystem.read(`${path}/${name}/${name}.service.spec.ts`);
37+
38+
expect(ts).toBeDefined();
39+
expect(spec).toBeDefined();
40+
41+
filesystem.remove(TESTING_DIR);
42+
});
43+
2844
test('should generate a common service with correct content ', async () => {
2945
const name = 'fruit';
3046

31-
await runNgxdCLI(`g s c ${name}`);
47+
await runNgxdCLI(`${COMMAND} ${name}`);
3248

3349
const ts = filesystem.read(`${name}/${name}.service.ts`);
3450
const spec = filesystem.read(`${name}/${name}.service.spec.ts`);

src/commands/generate/service/common/common.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import { GluegunCommand, GluegunToolbox } from 'gluegun';
2-
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
1+
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';
32

4-
import { printCreated } from '../../../../utils/functions.helper';
3+
import { getEntityName, getEntityPath, printCreated } from '../../../../utils/functions.helper';
54

65
const COMMAND: GluegunCommand = {
76
name: 'common',
87
alias: ['c'],
98
description: 'cria um serviço Angular',
109
run: async (toolbox: GluegunToolbox) => {
11-
const { parameters, print, prompt, template, strings } = toolbox;
10+
const { parameters, print, prompt, template } = toolbox;
11+
const {
12+
options: { path }
13+
} = parameters;
1214

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);
15+
const serviceName = parameters.first ?? (await getEntityName(prompt, 'service'));
16+
const servicePath = getEntityPath(path, serviceName);
3317

3418
template.generate({
3519
template: 'service.template.ts.ejs',
36-
target: `./${serviceNameKebab}/${serviceNameKebab}.service.ts`,
20+
target: `${servicePath}.service.ts`,
3721
props: {
3822
type: 'service',
3923
name: serviceName,
@@ -43,16 +27,16 @@ const COMMAND: GluegunCommand = {
4327

4428
template.generate({
4529
template: 'service.template.spec.ts.ejs',
46-
target: `./${serviceNameKebab}/${serviceNameKebab}.service.spec.ts`,
30+
target: `${servicePath}.service.spec.ts`,
4731
props: {
4832
type: 'service',
4933
name: serviceName,
5034
...strings
5135
}
5236
});
5337

54-
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.service.ts`);
55-
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.service.spec.ts`);
38+
printCreated(print, `${servicePath}.service.ts`);
39+
printCreated(print, `${servicePath}.service.spec.ts`);
5640
}
5741
};
5842

src/utils/functions.helper.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ export async function getComponentName(prompt: GluegunPrompt): Promise<string> {
3333
return strings.kebabCase(response.componentName);
3434
}
3535

36-
export function getComponentPath(path: any, componentName: string): string {
37-
return path ? `${path}/${componentName}/${componentName}` : `./${componentName}/${componentName}`;
36+
export async function getEntityName(prompt: GluegunPrompt, entityType: string): Promise<string> {
37+
const response: GluegunAskResponse = await prompt.ask({
38+
type: 'input',
39+
name: `${entityType}Name`,
40+
message: `Qual o nome do ${entityType}?`,
41+
validate: (value: string) => {
42+
if (!value) {
43+
return `O nome do ${entityType} não pode ser vazio`;
44+
}
45+
46+
return true;
47+
}
48+
});
49+
50+
return strings.kebabCase(response[`${entityType}Name`]);
51+
}
52+
53+
export function getEntityPath(path: any, entityName: string): string {
54+
return path ? `${path}/${entityName}/${entityName}` : `./${entityName}/${entityName}`;
3855
}

0 commit comments

Comments
 (0)