Skip to content

Commit

Permalink
✨ feat: add entity and service commands
Browse files Browse the repository at this point in the history
gleisonkz committed Jun 1, 2022
1 parent a6b1ff9 commit 6a5fa1c
Showing 14 changed files with 153 additions and 108 deletions.
32 changes: 16 additions & 16 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -25,17 +25,35 @@
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "ngxd-console-test.generate-entity",
"title": "Ngxd: Generate Entity"
},
{
"command": "ngxd-console-test.generate-component",
"title": "Ngxd: Generate Component"
},
{
"command": "ngxd-console-test.generate-service",
"title": "Ngxd: Generate Service"
}
],
"menus": {
"explorer/context": [
{
"command": "ngxd-console-test.generate-entity",
"when": "explorerResourceIsFolder",
"group": "ngxd-console"
},
{
"command": "ngxd-console-test.generate-component",
"when": "explorerResourceIsFolder",
"group": "ngxd-console"
},
{
"command": "ngxd-console-test.generate-service",
"when": "explorerResourceIsFolder",
"group": "ngxd-console"
}
]
}
16 changes: 8 additions & 8 deletions src/commands/generate-component.function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';

import { eError } from '../enums/error.enum';
import { ALL_COMPONENT_TYPES, ALL_RESOURCE_TYPES, eResourceType } from '../enums/type.enum';
import { ALL_COMPONENT_TYPES } from '../enums/type.enum';
import { execShell } from '../functions/exec-shell.function';

export async function generateComponent(uri: vscode.Uri) {
@@ -15,15 +15,15 @@ export async function generateComponent(uri: vscode.Uri) {
return vscode.window.showErrorMessage(eError.NAME_REQUIRED);
}

const resourceType = await vscode.window.showQuickPick(ALL_RESOURCE_TYPES);
if (!resourceType) {
return vscode.window.showErrorMessage(eError.RESOURCE_TYPE_REQUIRED);
}
const componentType = await vscode.window.showQuickPick(ALL_COMPONENT_TYPES);

if (resourceType !== eResourceType.COMPONENT) return;
if (!componentType) {
return vscode.window.showErrorMessage(eError.COMPONENT_TYPE_REQUIRED);
}

const componentType = await vscode.window.showQuickPick(ALL_COMPONENT_TYPES);
await execShell(`ngxd g c ${componentType} ${componentName} --path ${path}`);
const command = `ngxd g c ${componentType} ${componentName} --path ${path}`;
console.log({ command });
await execShell(command);

vscode.window.showInformationMessage(`Component ${componentName} created in ${path}`);
}
33 changes: 33 additions & 0 deletions src/commands/generate-entity.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from 'vscode';

import { eError } from '../enums/error.enum';
import { ALL_ENTITY_TYPES, ALL_ENTITY_TYPES_VALUES, eEntityType } from '../enums/type.enum';
import { execShell } from '../functions/exec-shell.function';
import { capitalizeFirstLetter } from '../functions/utils.function';

export async function generateEntity(uri: vscode.Uri) {
const path = uri.fsPath;

const entityType: eEntityType = (await vscode.window.showQuickPick(ALL_ENTITY_TYPES_VALUES)) as eEntityType;

if (!entityType) {
return vscode.window.showErrorMessage(eError.RESOURCE_TYPE_REQUIRED);
}

const entityName = await vscode.window.showInputBox({
placeHolder: `Enter the ${entityType} name`,
});

if (!entityName) {
return vscode.window.showErrorMessage(eError.NAME_REQUIRED);
}

const entityTypes = ALL_ENTITY_TYPES[entityType];
const serviceType = await vscode.window.showQuickPick(entityTypes);

await execShell(`ngxd g ${entityType} ${serviceType} ${entityName} --path ${path}`);

const capitalizedEntityType = capitalizeFirstLetter(entityType);

vscode.window.showInformationMessage(`${capitalizedEntityType} ${entityName} created in ${path}`);
}
27 changes: 27 additions & 0 deletions src/commands/generate-service.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as vscode from 'vscode';

import { eError } from '../enums/error.enum';
import { ALL_SERVICE_TYPES } from '../enums/type.enum';
import { execShell } from '../functions/exec-shell.function';

export async function generateService(uri: vscode.Uri) {
const path = uri.fsPath;

const serviceName = await vscode.window.showInputBox({
placeHolder: "Enter the service name",
});

if (!serviceName) {
return vscode.window.showErrorMessage(eError.NAME_REQUIRED);
}

const serviceType = await vscode.window.showQuickPick(ALL_SERVICE_TYPES);

if (!serviceType) {
return vscode.window.showErrorMessage(eError.SERVICE_TYPE_REQUIRED);
}

await execShell(`ngxd g s ${serviceType} ${serviceName} --path ${path}`);

vscode.window.showInformationMessage(`Service ${serviceName} created in ${path}`);
}
7 changes: 0 additions & 7 deletions src/enums/command.enum.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/enums/command.enum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export enum eCommandKey {
GENERATE_COMPONENT = "ngxd-console-test.generate-component",
GENERATE_SERVICE = "ngxd-console-test.generate-service",
GENERATE_ENTITY = "ngxd-console-test.generate-entity",
}
2 changes: 2 additions & 0 deletions src/enums/error.enum.ts
Original file line number Diff line number Diff line change
@@ -2,4 +2,6 @@ export const enum eError {
NAME_REQUIRED = "[NGXD Console] Component name is required",
RESOURCE_TYPE_REQUIRED = "[NGXD Console] Resource type is required",
CLI_REQUIRED = "[NGXD Console] @ngx-devs/cli is required to run this extension",
COMPONENT_TYPE_REQUIRED = "[NGXD Console] Component type is required",
SERVICE_TYPE_REQUIRED = "[NGXD Console] Service type is required",
}
18 changes: 16 additions & 2 deletions src/enums/type.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum eResourceType {
export enum eEntityType {
COMPONENT = "component",
DIRECTIVE = "directive",
SERVICE = "service",
@@ -12,5 +12,19 @@ export enum eComponentType {
WIDGET = "widget",
}

export const ALL_RESOURCE_TYPES = Object.values(eResourceType);
export enum eServiceType {
COMMON = "common",
API = "api",
}

export const ALL_ENTITY_TYPES_VALUES = Object.values(eEntityType);

export const ALL_COMPONENT_TYPES = Object.values(eComponentType);
export const ALL_SERVICE_TYPES = Object.values(eServiceType);

export const ALL_ENTITY_TYPES = {
[eEntityType.COMPONENT]: ALL_COMPONENT_TYPES,
[eEntityType.DIRECTIVE]: [],
[eEntityType.SERVICE]: ALL_SERVICE_TYPES,
[eEntityType.PIPE]: [],
};
23 changes: 18 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as vscode from 'vscode';

import { generateComponent } from './commands/generate-component.function';
import { generateEntity } from './commands/generate-entity.function';
import { generateService } from './commands/generate-service.function';
import { eCommandKey } from './enums/command.enum';
import { eMessage } from './enums/message.enum';
import { installCliIfNotInstalled } from './functions/cli.function';
@@ -9,12 +11,23 @@ export async function activate(context: vscode.ExtensionContext) {
await installCliIfNotInstalled();
console.info(eMessage.EXTENSION_ACTIVATED);

const generateComponentDisposable = vscode.commands.registerCommand(
eCommandKey.GENERATE_COMPONENT,
generateComponent
);
const commands = [
{
command: eCommandKey.GENERATE_COMPONENT,
callback: generateComponent,
},
{
command: eCommandKey.GENERATE_SERVICE,
callback: generateService,
},
{
command: eCommandKey.GENERATE_ENTITY,
callback: generateEntity,
},
];

context.subscriptions.push(generateComponentDisposable);
const disposables = commands.map(({ command, callback }) => vscode.commands.registerCommand(command, callback));
context.subscriptions.push(...disposables);
}

export function deactivate() {
1 change: 1 addition & 0 deletions src/functions/exec-shell.function.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ export const execShell = (cmd: string) =>
new Promise<string>((resolve, reject) => {
cp.exec(cmd, (err, out) => {
if (err) {
console.error(err);
return reject(err);
}
return resolve(out);
3 changes: 3 additions & 0 deletions src/functions/utils.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function capitalizeFirstLetter(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
70 changes: 0 additions & 70 deletions src/scripts/precompile.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/scripts/precompile.ts
Original file line number Diff line number Diff line change
@@ -3,15 +3,24 @@ import { menusFactory } from '../functions/menu-factory.function';
import { NgxdCommand } from '../interfaces/command.interface';

const COMMANDS: NgxdCommand[] = [
{
command: eCommandKey.GENERATE_ENTITY,
title: "Ngxd: Generate Entity",
},
{
command: eCommandKey.GENERATE_COMPONENT,
title: "Ngxd: Generate Component",
},
{
command: eCommandKey.GENERATE_SERVICE,
title: "Ngxd: Generate Service",
},
];

(async function main() {
const npmCliPackageJson = require("@npmcli/package-json");
const pkgJson = await npmCliPackageJson.load("./");

pkgJson.update({
contributes: {
commands: COMMANDS,

0 comments on commit 6a5fa1c

Please sign in to comment.