-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli]: rework logic to support nested packages
- Loading branch information
Showing
32 changed files
with
818 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
examples/fastify/src/protobufTypings/nested_example.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.