Skip to content

Commit

Permalink
[cli]: rework logic to support nested packages
Browse files Browse the repository at this point in the history
  • Loading branch information
zgid123 committed Apr 14, 2024
1 parent 78d0820 commit d8bda80
Show file tree
Hide file tree
Showing 32 changed files with 818 additions and 353 deletions.
3 changes: 3 additions & 0 deletions examples/fastify/src/protobufTypings/example.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

import type { INested as INestedV1Nested } from './nested_example.interface';

export const PACKAGE_NAME = 'example.v1';

export const messageEnum = {
Expand All @@ -17,6 +19,7 @@ export type TMessageEnum = 'unknown' | 'info';
export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
nested: INestedV1Nested;
}

export interface ISendMessageRequest {
Expand Down
12 changes: 10 additions & 2 deletions examples/fastify/src/protobufTypings/example2.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

import type {
IMessage as IExampleV1Message,
ISendMessageRequest as IExampleV1SendMessageRequest,
} from './example.interface';
import type { INested as INestedV1Nested } from './nested_example.interface';

export const PACKAGE_NAME = 'example2.v1';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
exampleMessage: IExampleV1Message;
nested: INestedV1Nested;
}

export interface ISendMessageRequest {
Expand All @@ -20,11 +28,11 @@ export const EXAMPLE_SERVICE = 'ExampleService';

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
params: IExampleV1SendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
params: IExampleV1SendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
3 changes: 3 additions & 0 deletions examples/fastify/src/protobufTypings/example3.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable @typescript-eslint/ban-types */
import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

import type { INested as INestedV1Nested } from './nested_example.interface';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
nested: INestedV1Nested;
}

export interface ISendMessageRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { GrpcTimestamp } from '@grpc.ts/core';

export const PACKAGE_NAME = 'nested.v1';

export interface INested {
nested: string;
createdAt: GrpcTimestamp;
}
3 changes: 3 additions & 0 deletions examples/nestjs/protobufTypings/example.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

import type { INested as INestedV1Nested } from './nested_example.interface';

export const PACKAGE_NAME = 'example.v1';

export const messageEnum = {
Expand All @@ -17,6 +19,7 @@ export type TMessageEnum = 'unknown' | 'info';
export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
nested: INestedV1Nested;
}

export interface ISendMessageRequest {
Expand Down
12 changes: 10 additions & 2 deletions examples/nestjs/protobufTypings/example2.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

import type {
IMessage as IExampleV1Message,
ISendMessageRequest as IExampleV1SendMessageRequest,
} from './example.interface';
import type { INested as INestedV1Nested } from './nested_example.interface';

export const PACKAGE_NAME = 'example2.v1';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
exampleMessage: IExampleV1Message;
nested: INestedV1Nested;
}

export interface ISendMessageRequest {
Expand All @@ -20,11 +28,11 @@ export const EXAMPLE_SERVICE = 'ExampleService';

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
params: IExampleV1SendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
params: IExampleV1SendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
3 changes: 3 additions & 0 deletions examples/nestjs/protobufTypings/example3.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable @typescript-eslint/ban-types */
import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

import type { INested as INestedV1Nested } from './nested_example.interface';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
nested: INestedV1Nested;
}

export interface ISendMessageRequest {
Expand Down
8 changes: 8 additions & 0 deletions examples/nestjs/protobufTypings/nested_example.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { GrpcTimestamp } from '@grpc.ts/core';

export const PACKAGE_NAME = 'nested.v1';

export interface INested {
nested: string;
createdAt: GrpcTimestamp;
}
3 changes: 3 additions & 0 deletions examples/proto/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

import "google/protobuf/timestamp.proto";

import "nested_example.proto";

package example.v1;

enum MessageEnum {
Expand All @@ -12,6 +14,7 @@ enum MessageEnum {
message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
nested.v1.Nested nested = 3;
}

message SendMessageRequest {
Expand Down
7 changes: 6 additions & 1 deletion examples/proto/example2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ syntax = "proto3";

import "google/protobuf/timestamp.proto";

import "example.proto";
import "nested_example.proto";

package example2.v1;

message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
example.v1.Message example_message = 3;
nested.v1.Nested nested = 4;
}

message SendMessageRequest {
Expand All @@ -17,5 +22,5 @@ message SendMessageRequest {
message GetMessageResponse { Message message = 1; }

service ExampleService {
rpc SendMessage(SendMessageRequest) returns (GetMessageResponse);
rpc SendMessage(example.v1.SendMessageRequest) returns (GetMessageResponse);
}
3 changes: 3 additions & 0 deletions examples/proto/example3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ syntax = "proto3";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

import "nested_example.proto";

message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
nested.v1.Nested nested = 3;
}

message SendMessageRequest {
Expand Down
10 changes: 10 additions & 0 deletions examples/proto/nested_example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

package nested.v1;

message Nested {
string nested = 1;
google.protobuf.Timestamp created_at = 2;
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grpc.ts/cli",
"version": "1.0.3",
"version": "1.1.0",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down
13 changes: 7 additions & 6 deletions packages/cli/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ export interface IConfigProps {
paths?: string | string[];
}

export interface IOptionsProps {
external?: string[];
}

export interface IMessageProps {
name: string;
type: string;
Expand Down Expand Up @@ -39,6 +35,11 @@ export interface INamespaceDataProps {

export type TParseNamespaceReturn = [string, INamespaceDataProps];

export interface ICachedEnumsProps {
[key: string]: boolean;
export interface IProtoDataProps {
filePath: string;
packageName: string;
noDependency: boolean;
data: INamespaceDataProps;
ownedMessages: Record<string, string>;
dependencies: Record<string, INamespaceDataProps>;
}
117 changes: 9 additions & 108 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,15 @@
import { glob } from 'glob';
import { resolve } from 'path';
import { format } from 'prettier';
import { load } from 'protobufjs';
import { cwd } from 'node:process';
import { writeFile, mkdir } from 'node:fs/promises';
import { loadData } from './tasks/loadData';
import { loadConfig } from './utils/configuration';
import { createContent } from './tasks/createContent';
import { topologicalGroup } from './tasks/topologicalGroup';

import { parse, wrapArray, loadConfig, combine } from './utils';
import {
createImportType,
createExportEnums,
createExportMessages,
createExportServices,
createExportPackageName,
} from './utils/contentHelper';
async function generate(): Promise<void> {
const config = await loadConfig();
const protoData = await loadData(config);

async function generate() {
const { external, output, paths } = await loadConfig();
const filePaths = await glob(
wrapArray(paths).map((path) => resolve(cwd(), path)),
);
const groupedProtoData = topologicalGroup(protoData);

Promise.all(
filePaths.map(async (filePath) => {
const root = await load(filePath);

const data = root.toJSON().nested;

if (!data) {
console.warn('No data from proto file');

return;
}

const result = parse(data, {
external,
});

let fileContent = createExportPackageName(result[0][0]);
let hasMetadata = false;
let hasGrpcTimestamp = false;
let hasService = false;
let hasBanType = false;

result.forEach(([_, packageData]) => {
const [enumsContent, cachedEnums] = createExportEnums(
packageData.enums,
);

const [messagesContent, containsGrpcTimestamp] = createExportMessages(
packageData.messages,
cachedEnums,
);

if (containsGrpcTimestamp) {
hasGrpcTimestamp = containsGrpcTimestamp;
}

if (packageData.services.length) {
hasService = true;
hasMetadata = true;
}

const [servicesContent, containsBanType] = createExportServices(
packageData.services,
);

if (containsBanType) {
hasBanType = containsBanType;
}

fileContent = combine(
{
joinWith: '\n',
},
fileContent,
enumsContent,
messagesContent,
servicesContent,
);
});

const fileName = filePath.slice(
filePath.lastIndexOf('/') + 1,
filePath.length - '.proto'.length,
);

fileContent = await createImportType(fileContent, {
hasService,
hasBanType,
hasMetadata,
hasGrpcTimestamp,
});

await mkdir(output, { recursive: true });

writeFile(
`${output}/${fileName}.interface.ts`,
await format(fileContent, {
singleQuote: true,
trailingComma: 'all',
jsxSingleQuote: true,
parser: 'typescript',
arrowParens: 'always',
}),
{
encoding: 'utf8',
},
);
}),
);
createContent(groupedProtoData, config);
}

generate();
Loading

0 comments on commit d8bda80

Please sign in to comment.