Skip to content

Commit

Permalink
🐛 fix: path parameter on service command
Browse files Browse the repository at this point in the history
  • Loading branch information
gleisonkz committed Jun 3, 2022
1 parent b8cc020 commit ddc3cba
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 76 deletions.
5 changes: 3 additions & 2 deletions src/commands/generate/component/common/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';
import { getComponentName, getComponentPath, printCreated } from '../../../../utils/functions.helper';

import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';

const COMMAND: GluegunCommand = {
name: 'common',
Expand All @@ -12,7 +13,7 @@ const COMMAND: GluegunCommand = {
} = parameters;

const componentName = parameters.first ?? (await getComponentName(prompt));
const componentPath = getComponentPath(path, componentName);
const componentPath = getEntityPath(path, componentName);

template.generate({
template: 'component.template.html.ejs',
Expand Down
6 changes: 2 additions & 4 deletions src/commands/generate/component/dialog/dialog.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';

import {
getComponentName, getComponentPath, printCreated
} from '../../../../utils/functions.helper';
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';

const COMMAND: GluegunCommand = {
name: 'dialog',
Expand All @@ -15,7 +13,7 @@ const COMMAND: GluegunCommand = {
} = parameters;

const componentName = parameters.first ?? (await getComponentName(prompt));
const componentPath = getComponentPath(path, componentName);
const componentPath = getEntityPath(path, componentName);

template.generate({
template: 'component.template.html.ejs',
Expand Down
6 changes: 2 additions & 4 deletions src/commands/generate/component/page/page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { GluegunCommand, GluegunToolbox } from 'gluegun';

import {
getComponentName, getComponentPath, printCreated
} from '../../../../utils/functions.helper';
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';

const COMMAND: GluegunCommand = {
name: 'page',
Expand All @@ -15,7 +13,7 @@ const COMMAND: GluegunCommand = {
} = parameters;

const componentName = parameters.first ?? (await getComponentName(prompt));
const componentPath = getComponentPath(path, componentName);
const componentPath = getEntityPath(path, componentName);

template.generate({
template: 'component.template.html.ejs',
Expand Down
6 changes: 2 additions & 4 deletions src/commands/generate/component/widget/widget.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { GluegunCommand, strings } from 'gluegun';

import {
getComponentName, getComponentPath, printCreated
} from '../../../../utils/functions.helper';
import { getComponentName, getEntityPath, printCreated } from '../../../../utils/functions.helper';

const COMMAND: GluegunCommand = {
name: 'widget',
Expand All @@ -15,7 +13,7 @@ const COMMAND: GluegunCommand = {
} = parameters;

const componentName = parameters.first ?? (await getComponentName(prompt));
const componentPath = getComponentPath(path, componentName);
const componentPath = getEntityPath(path, componentName);

template.generate({
template: 'component.template.html.ejs',
Expand Down
20 changes: 18 additions & 2 deletions src/commands/generate/service/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { runNgxdCLI } from '../../../../utils/cli-test-setup';

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

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

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

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

test('should generate a api service at given path', async () => {
const path = `${TESTING_DIR}`;

await runNgxdCLI(`${COMMAND} ${name} --path ${path}`);

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

expect(ts).toBeDefined();
expect(spec).toBeDefined();

filesystem.remove(TESTING_DIR);
});

test('should generate a api service with correct content ', async () => {
const name = 'fruit';

await runNgxdCLI(`g s a ${name}`);
await runNgxdCLI(`${COMMAND} ${name}`);

const ts = filesystem.read(`${name}/${name}.api.ts`);
const spec = filesystem.read(`${name}/${name}.api.spec.ts`);
Expand Down
40 changes: 12 additions & 28 deletions src/commands/generate/service/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
import { GluegunCommand, GluegunToolbox } from 'gluegun';
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';

import { printCreated } from '../../../../utils/functions.helper';
import { getEntityName, getEntityPath, printCreated } from '../../../../utils/functions.helper';

const COMMAND: GluegunCommand = {
name: 'api',
alias: ['a'],
description: 'cria um serviço Angular do tipo Api',
run: async (toolbox: GluegunToolbox) => {
const { parameters, print, prompt, template, strings } = toolbox;
const { parameters, print, prompt, template } = toolbox;
const {
options: { path }
} = parameters;

let serviceName = parameters.first;

if (!serviceName) {
const response: GluegunAskResponse = await prompt.ask({
type: 'input',
name: 'serviceName',
message: 'Qual o nome do serviço?',
validate: (value: string) => {
if (!value) {
return 'O nome do serviço não pode ser vazio';
}

return true;
}
});

serviceName = response.serviceName;
}

const serviceNameKebab = strings.kebabCase(serviceName);
const serviceName = parameters.first ?? (await getEntityName(prompt, 'service'));
const servicePath = getEntityPath(path, serviceName);

template.generate({
template: 'service.template.ts.ejs',
target: `./${serviceNameKebab}/${serviceNameKebab}.api.ts`,
target: `${servicePath}.api.ts`,
props: {
type: 'api',
name: serviceName,
Expand All @@ -43,16 +27,16 @@ const COMMAND: GluegunCommand = {

template.generate({
template: 'service.template.spec.ts.ejs',
target: `./${serviceNameKebab}/${serviceNameKebab}.api.spec.ts`,
target: `${servicePath}.api.spec.ts`,
props: {
type: 'api',
name: serviceName,
...strings
}
});

printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.api.ts`);
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.api.spec.ts`);
printCreated(print, `${servicePath}.api.ts`);
printCreated(print, `${servicePath}.api.spec.ts`);
}
};

Expand Down
20 changes: 18 additions & 2 deletions src/commands/generate/service/common/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { runNgxdCLI } from '../../../../utils/cli-test-setup';

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

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

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

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

test('should generate a common service at given path', async () => {
const path = `${TESTING_DIR}/src/app/services`;

await runNgxdCLI(`${COMMAND} ${name} --path ${path}`);

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

expect(ts).toBeDefined();
expect(spec).toBeDefined();

filesystem.remove(TESTING_DIR);
});

test('should generate a common service with correct content ', async () => {
const name = 'fruit';

await runNgxdCLI(`g s c ${name}`);
await runNgxdCLI(`${COMMAND} ${name}`);

const ts = filesystem.read(`${name}/${name}.service.ts`);
const spec = filesystem.read(`${name}/${name}.service.spec.ts`);
Expand Down
40 changes: 12 additions & 28 deletions src/commands/generate/service/common/common.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
import { GluegunCommand, GluegunToolbox } from 'gluegun';
import { GluegunAskResponse } from 'gluegun/build/types/toolbox/prompt-types';
import { GluegunCommand, GluegunToolbox, strings } from 'gluegun';

import { printCreated } from '../../../../utils/functions.helper';
import { getEntityName, getEntityPath, printCreated } from '../../../../utils/functions.helper';

const COMMAND: GluegunCommand = {
name: 'common',
alias: ['c'],
description: 'cria um serviço Angular',
run: async (toolbox: GluegunToolbox) => {
const { parameters, print, prompt, template, strings } = toolbox;
const { parameters, print, prompt, template } = toolbox;
const {
options: { path }
} = parameters;

let serviceName = parameters.first;

if (!serviceName) {
const response: GluegunAskResponse = await prompt.ask({
type: 'input',
name: 'serviceName',
message: 'Qual o nome do serviço?',
validate: (value: string) => {
if (!value) {
return 'O nome do serviço não pode ser vazio';
}

return true;
}
});

serviceName = response.serviceName;
}

const serviceNameKebab = strings.kebabCase(serviceName);
const serviceName = parameters.first ?? (await getEntityName(prompt, 'service'));
const servicePath = getEntityPath(path, serviceName);

template.generate({
template: 'service.template.ts.ejs',
target: `./${serviceNameKebab}/${serviceNameKebab}.service.ts`,
target: `${servicePath}.service.ts`,
props: {
type: 'service',
name: serviceName,
Expand All @@ -43,16 +27,16 @@ const COMMAND: GluegunCommand = {

template.generate({
template: 'service.template.spec.ts.ejs',
target: `./${serviceNameKebab}/${serviceNameKebab}.service.spec.ts`,
target: `${servicePath}.service.spec.ts`,
props: {
type: 'service',
name: serviceName,
...strings
}
});

printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.service.ts`);
printCreated(print, `${serviceNameKebab}/${serviceNameKebab}.service.spec.ts`);
printCreated(print, `${servicePath}.service.ts`);
printCreated(print, `${servicePath}.service.spec.ts`);
}
};

Expand Down
21 changes: 19 additions & 2 deletions src/utils/functions.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ export async function getComponentName(prompt: GluegunPrompt): Promise<string> {
return strings.kebabCase(response.componentName);
}

export function getComponentPath(path: any, componentName: string): string {
return path ? `${path}/${componentName}/${componentName}` : `./${componentName}/${componentName}`;
export async function getEntityName(prompt: GluegunPrompt, entityType: string): Promise<string> {
const response: GluegunAskResponse = await prompt.ask({
type: 'input',
name: `${entityType}Name`,
message: `Qual o nome do ${entityType}?`,
validate: (value: string) => {
if (!value) {
return `O nome do ${entityType} não pode ser vazio`;
}

return true;
}
});

return strings.kebabCase(response[`${entityType}Name`]);
}

export function getEntityPath(path: any, entityName: string): string {
return path ? `${path}/${entityName}/${entityName}` : `./${entityName}/${entityName}`;
}

0 comments on commit ddc3cba

Please sign in to comment.