refactor(proto-loader-gen-types): refactor for more consistent code style

This commit is contained in:
tenkirin 2025-03-04 07:21:07 +09:00
parent b43225d6a6
commit c5b96a9054
1 changed files with 10 additions and 10 deletions

View File

@ -107,8 +107,8 @@ function getImportPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service): st
return stripLeadingPeriod(to.fullName).replace(/\./g, '/'); return stripLeadingPeriod(to.fullName).replace(/\./g, '/');
} }
function getPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service, extension: string = '.ts') { function getPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service, options: GeneratorOptions) {
return stripLeadingPeriod(to.fullName).replace(/\./g, '/') + extension; return stripLeadingPeriod(to.fullName).replace(/\./g, '/') + options.targetFileExtension;
} }
function getPathToRoot(from: Protobuf.NamespaceBase) { function getPathToRoot(from: Protobuf.NamespaceBase) {
@ -155,7 +155,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
throw new Error('Invalid object passed to getImportLine'); throw new Error('Invalid object passed to getImportLine');
} }
} }
return `import type { ${importedTypes} } from '${filePath}${options.importFileExtension ?? ''}';` return `import type { ${importedTypes} } from '${filePath}${options.importFileExtension}';`
} }
function getChildMessagesAndEnums(namespace: Protobuf.NamespaceBase): (Protobuf.Type | Protobuf.Enum)[] { function getChildMessagesAndEnums(namespace: Protobuf.NamespaceBase): (Protobuf.Type | Protobuf.Enum)[] {
@ -789,21 +789,21 @@ function generateFilesForNamespace(namespace: Protobuf.NamespaceBase, options: G
if (nested instanceof Protobuf.Type) { if (nested instanceof Protobuf.Type) {
generateMessageInterfaces(fileFormatter, nested, options); generateMessageInterfaces(fileFormatter, nested, options);
if (options.verbose) { if (options.verbose) {
console.log(`Writing ${options.outDir}/${getPath(nested, options.targetFileExtension)} from file ${nested.filename}`); console.log(`Writing ${options.outDir}/${getPath(nested, options)} from file ${nested.filename}`);
} }
filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options.targetFileExtension)}`, fileFormatter.getFullText())); filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options)}`, fileFormatter.getFullText()));
} else if (nested instanceof Protobuf.Enum) { } else if (nested instanceof Protobuf.Enum) {
generateEnumInterface(fileFormatter, nested, options); generateEnumInterface(fileFormatter, nested, options);
if (options.verbose) { if (options.verbose) {
console.log(`Writing ${options.outDir}/${getPath(nested, options.targetFileExtension)} from file ${nested.filename}`); console.log(`Writing ${options.outDir}/${getPath(nested, options)} from file ${nested.filename}`);
} }
filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options.targetFileExtension)}`, fileFormatter.getFullText())); filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options)}`, fileFormatter.getFullText()));
} else if (nested instanceof Protobuf.Service) { } else if (nested instanceof Protobuf.Service) {
generateServiceInterfaces(fileFormatter, nested, options); generateServiceInterfaces(fileFormatter, nested, options);
if (options.verbose) { if (options.verbose) {
console.log(`Writing ${options.outDir}/${getPath(nested, options.targetFileExtension)} from file ${nested.filename}`); console.log(`Writing ${options.outDir}/${getPath(nested, options)} from file ${nested.filename}`);
} }
filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options.targetFileExtension)}`, fileFormatter.getFullText())); filePromises.push(writeFile(`${options.outDir}/${getPath(nested, options)}`, fileFormatter.getFullText()));
} else if (isNamespaceBase(nested)) { } else if (isNamespaceBase(nested)) {
filePromises.push(...generateFilesForNamespace(nested, options)); filePromises.push(...generateFilesForNamespace(nested, options));
} }
@ -880,7 +880,7 @@ async function runScript() {
.option('inputBranded', boolDefaultFalseOption) .option('inputBranded', boolDefaultFalseOption)
.option('outputBranded', boolDefaultFalseOption) .option('outputBranded', boolDefaultFalseOption)
.option('targetFileExtension', { string: true, default: '.ts' }) .option('targetFileExtension', { string: true, default: '.ts' })
.option('importFileExtension', { string: true }) .option('importFileExtension', { string: true, default: '' })
.coerce('longs', value => { .coerce('longs', value => {
switch (value) { switch (value) {
case 'String': return String; case 'String': return String;