Skip to content

Commit 080e8a3

Browse files
committed
Expose pathsToExclude
1 parent dc734ec commit 080e8a3

File tree

7 files changed

+49
-18
lines changed

7 files changed

+49
-18
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": ["<node_internals>/**"],
12+
"program": "${workspaceFolder}/dist/index.js",
13+
"outFiles": ["${workspaceFolder}/**/*.js"]
14+
}
15+
]
16+
}

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"editor.formatOnSave": true,
3-
"editor.defaultFormatter": "esbenp.prettier-vscode"
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"editor.codeActionsOnSave": {}
45
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cldcvr/protobufjs-typescript-gen",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"main": "dist/index.js",
55
"author": "Nikhil Verma <[email protected]>",
66
"license": "MIT",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function generateNamespace(namespace: Namespace, options: ProtoGenOptions) {
109109
// We have types in this namespace, we need to generate a file
110110
if (types.length > 0 || enums.length > 0 || services.length > 0) {
111111
const typeInfos = types.map((type) => getTypeInfo(type, options));
112-
const serviceInfos = services.map(getServiceInfo);
112+
const serviceInfos = services.map((service_) => getServiceInfo(service_, options));
113113

114114
[...typeInfos, ...serviceInfos].forEach((typeInfo) => {
115115
typeInfo.imports.forEach(({ path, name }) => {
@@ -136,7 +136,7 @@ function generateNamespace(namespace: Namespace, options: ProtoGenOptions) {
136136
);
137137

138138
// Ensure that type's parent has a file
139-
const filePath = namespaceToPath(namespace.fullName);
139+
const filePath = namespaceToPath(namespace.fullName, options);
140140

141141
outputFileSync(`${path.join(options.outDir, filePath)}.ts`, file);
142142
}

src/options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export function vaidateOptions(options?: UserOptions): ProtoGenOptions {
4141
options.ignoreNamespaces = ['grpc', 'validate', 'google'];
4242
}
4343

44+
if (!('pathsToExclude' in options)) {
45+
options.pathsToExclude = ['v0'];
46+
}
47+
4448
return options as ProtoGenOptions;
4549
}
4650

@@ -49,6 +53,7 @@ export type ProtoGenOptions = Required<UserOptions>;
4953
export type UserOptions = {
5054
outDir: string;
5155
protocolDir: string;
56+
pathsToExclude?: string[];
5257
protoCwd?: string;
5358
ignoreNamespaces?: string[];
5459
cwd?: string;

src/services.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Method, Service, Type } from 'protobufjs';
22
import { getCommentBlock, getImport, Import, toLowerCaseFirstLetter } from './utils';
3+
import { ProtoGenOptions } from './options';
34

4-
export function getServiceInfo(service: Service) {
5+
export function getServiceInfo(service: Service, options: ProtoGenOptions) {
56
let typeString = `export interface ${service.name} {\n`;
67

78
let imports: Array<Import> = [];
89

910
Object.values(service.methods).forEach((method) => {
10-
const { code, typeImports } = getMethodCode(method);
11+
const { code, typeImports } = getMethodCode(method, options);
1112
imports = imports.concat(typeImports);
1213
typeString += code;
1314
});
@@ -17,7 +18,7 @@ export function getServiceInfo(service: Service) {
1718
return { imports, typeString };
1819
}
1920

20-
function getMethodCode(method: Method) {
21+
function getMethodCode(method: Method, options: ProtoGenOptions) {
2122
const typeImports: Array<Import> = [];
2223

2324
method.resolve();
@@ -28,12 +29,14 @@ function getMethodCode(method: Method) {
2829

2930
const { code: requestCode, import: requestImport } = getServiceTypeInfo(
3031
method,
31-
method.resolvedRequestType
32+
method.resolvedRequestType,
33+
options
3234
);
3335

3436
const { code: responseCode, import: responseImport } = getServiceTypeInfo(
3537
method,
36-
method.resolvedResponseType
38+
method.resolvedResponseType,
39+
options
3740
);
3841

3942
if (requestImport) {
@@ -53,7 +56,11 @@ function getMethodCode(method: Method) {
5356
};
5457
}
5558

56-
function getServiceTypeInfo(method: Method, type: Type): { code: string; import: Import | null } {
59+
function getServiceTypeInfo(
60+
method: Method,
61+
type: Type,
62+
options: ProtoGenOptions
63+
): { code: string; import: Import | null } {
5764
switch (type.fullName) {
5865
case '.google.protobuf.Value':
5966
return { code: 'any', import: null };
@@ -70,7 +77,7 @@ function getServiceTypeInfo(method: Method, type: Type): { code: string; import:
7077
default:
7178
return {
7279
code: type.name,
73-
import: getImport(method, type),
80+
import: getImport(method, type, options),
7481
};
7582
}
7683
}

src/utils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ export function getCommentBlock(object: ReflectionObject) {
2222
return typeString ? `${typeString}\n` : '';
2323
}
2424

25-
export function getImport(object: ReflectionObject, targetObject: ReflectionObject | null) {
25+
export function getImport(
26+
object: ReflectionObject,
27+
targetObject: ReflectionObject | null,
28+
options: ProtoGenOptions
29+
) {
2630
if (!targetObject) {
2731
return null;
2832
}
@@ -37,7 +41,7 @@ export function getImport(object: ReflectionObject, targetObject: ReflectionObje
3741

3842
return {
3943
name: targetObject.name,
40-
path: namespaceToPath(targetObject.fullName).replace(`/${targetObject.name}`, ''),
44+
path: namespaceToPath(targetObject.fullName, options).replace(`/${targetObject.name}`, ''),
4145
};
4246
}
4347

@@ -55,13 +59,11 @@ export function getClosestNamespace(root: ReflectionObject) {
5559
return null;
5660
}
5761

58-
export function namespaceToPath(namespace: string) {
59-
const pathsToExclude = ['v0'];
60-
62+
export function namespaceToPath(namespace: string, options: ProtoGenOptions) {
6163
return namespace
6264
.split('.')
6365
.filter(Boolean)
64-
.filter((str) => str.length > 0 && !pathsToExclude.includes(str))
66+
.filter((str) => str.length > 0 && !options.pathsToExclude.includes(str))
6567
.join('/');
6668
}
6769

@@ -158,7 +160,7 @@ export function fieldToTypescriptType(field: FieldBase, options: ProtoGenOptions
158160

159161
if (field.resolvedType?.name) {
160162
type = field.resolvedType.name;
161-
typeImport = getImport(field, field.resolvedType);
163+
typeImport = getImport(field, field.resolvedType, options);
162164
} else {
163165
type = 'unknown';
164166
}

0 commit comments

Comments
 (0)