Skip to content

Commit 8499c7b

Browse files
committed
feat(proto-loader-gen-types): add options for specifying file extensions
1 parent 55b31f6 commit 8499c7b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/proto-loader/bin/proto-loader-gen-types.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
4747
outputTemplate: string;
4848
inputBranded: boolean;
4949
outputBranded: boolean;
50+
targetFileExtension?: string;
51+
importFileExtension?: string;
5052
}
5153

5254
class TextFormatter {
@@ -105,8 +107,8 @@ function getImportPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service): st
105107
return stripLeadingPeriod(to.fullName).replace(/\./g, '/');
106108
}
107109

108-
function getPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service) {
109-
return stripLeadingPeriod(to.fullName).replace(/\./g, '/') + '.ts';
110+
function getPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service, extension: string = '.ts') {
111+
return stripLeadingPeriod(to.fullName).replace(/\./g, '/') + extension;
110112
}
111113

112114
function getPathToRoot(from: Protobuf.NamespaceBase) {
@@ -153,7 +155,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
153155
throw new Error('Invalid object passed to getImportLine');
154156
}
155157
}
156-
return `import type { ${importedTypes} } from '${filePath}';`
158+
return `import type { ${importedTypes} } from '${filePath}${options.importFileExtension ?? ''}';`
157159
}
158160

159161
function getChildMessagesAndEnums(namespace: Protobuf.NamespaceBase): (Protobuf.Type | Protobuf.Enum)[] {
@@ -787,21 +789,21 @@ function generateFilesForNamespace(namespace: Protobuf.NamespaceBase, options: G
787789
if (nested instanceof Protobuf.Type) {
788790
generateMessageInterfaces(fileFormatter, nested, options);
789791
if (options.verbose) {
790-
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`);
792+
console.log(`Writing ${options.outDir}/${getPath(nested, options.targetFileExtension)} from file ${nested.filename}`);
791793
}
792-
filePromises.push(writeFile(`${options.outDir}/${getPath(nested)}`, fileFormatter.getFullText()));
794+
filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options.targetFileExtension)}`, fileFormatter.getFullText()));
793795
} else if (nested instanceof Protobuf.Enum) {
794796
generateEnumInterface(fileFormatter, nested, options);
795797
if (options.verbose) {
796-
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`);
798+
console.log(`Writing ${options.outDir}/${getPath(nested, options.targetFileExtension)} from file ${nested.filename}`);
797799
}
798-
filePromises.push(writeFile(`${options.outDir}/${getPath(nested)}`, fileFormatter.getFullText()));
800+
filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options.targetFileExtension)}`, fileFormatter.getFullText()));
799801
} else if (nested instanceof Protobuf.Service) {
800802
generateServiceInterfaces(fileFormatter, nested, options);
801803
if (options.verbose) {
802-
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`);
804+
console.log(`Writing ${options.outDir}/${getPath(nested, options.targetFileExtension)} from file ${nested.filename}`);
803805
}
804-
filePromises.push(writeFile(`${options.outDir}/${getPath(nested)}`, fileFormatter.getFullText()));
806+
filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options.targetFileExtension)}`, fileFormatter.getFullText()));
805807
} else if (isNamespaceBase(nested)) {
806808
filePromises.push(...generateFilesForNamespace(nested, options));
807809
}
@@ -877,6 +879,8 @@ async function runScript() {
877879
.option('outputTemplate', { string: true, default: `${templateStr}__Output` })
878880
.option('inputBranded', boolDefaultFalseOption)
879881
.option('outputBranded', boolDefaultFalseOption)
882+
.option('targetFileExtension', { string: true, default: '.ts' })
883+
.option('importFileExtension', { string: true })
880884
.coerce('longs', value => {
881885
switch (value) {
882886
case 'String': return String;
@@ -916,6 +920,8 @@ async function runScript() {
916920
outputTemplate: 'Template for mapping output or "restricted" type names',
917921
inputBranded: 'Output property for branded type for "permissive" types with fullName of the Message as its value',
918922
outputBranded: 'Output property for branded type for "restricted" types with fullName of the Message as its value',
923+
targetFileExtension: 'File extension for generated files. Defaults to .ts',
924+
importFileExtension: 'File extension for import specifiers in generated code. Defaults to none (omitted)'
919925
}).demandOption(['outDir'])
920926
.demand(1)
921927
.usage('$0 [options] filenames...')

0 commit comments

Comments
 (0)