From dc6ca140e76d9a8719ab96cf00a02f35ac6a9192 Mon Sep 17 00:00:00 2001 From: Nex Zhu <4370605+NexZhu@users.noreply.github.com> Date: Thu, 22 Jul 2021 16:21:15 +0800 Subject: [PATCH] feat: add `ts-proto` for new TypeScript SDK --- grpc/README.md | 20 ++++-- grpc/package-lock.json | 157 ++++++++++++++++++++++++++++++++++++++++- grpc/package.json | 3 +- grpc/tasks.py | 35 ++++++--- 4 files changed, 196 insertions(+), 19 deletions(-) diff --git a/grpc/README.md b/grpc/README.md index 2201a55..a8f0865 100644 --- a/grpc/README.md +++ b/grpc/README.md @@ -73,6 +73,20 @@ References: Run: +For generating TypeScript client code with `ts-proto`, run: + +```shell +invoke gen -t ts +``` + +For generating TypeScript definitions to be used with `@grpc/proto-loader`, run: + +```shell +invoke gen -t types +``` + +Others: + ```shell ./scripts/gen [-a, --async] [go] [gohttp] [nodejs] [docs] ``` @@ -82,12 +96,6 @@ Run: Use `-a` or `--async` to speed up generation by asynchronously and concurrently generating all code and documentation ( script will exit without waiting for completion) -For generating TypeScript definitions to be used with `@grpc/proto-loader`, run: - -```shell -invoke gen -t types -``` - #### Generateing documentation Install Go then run: diff --git a/grpc/package-lock.json b/grpc/package-lock.json index 3eb6e65..e5eea68 100644 --- a/grpc/package-lock.json +++ b/grpc/package-lock.json @@ -10,7 +10,8 @@ "devDependencies": { "@grpc/proto-loader": "^0.6.4", "grpc_tools_node_protoc_ts": "^5.3.0", - "grpc-tools": "^1.11.2" + "grpc-tools": "^1.11.2", + "ts-proto": "^1.82.2" } }, "node_modules/@grpc/proto-loader": { @@ -128,6 +129,18 @@ "integrity": "sha512-jJs9ErFLP403I+hMLGnqDRWT0RYKSvArxuBVh2veudHV7ifEC1WAmjJADacZ7mRbA2nWgHtn8xyECMAot0SkAw==", "dev": true }, + "node_modules/@types/object-hash": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-1.3.4.tgz", + "integrity": "sha512-xFdpkAkikBgqBdG9vIlsqffDV8GpvnPEzs0IUtr1v3BEB97ijsFQ4RXVbUZwjFThhB4MDSTUfvmxUD5PGx0wXA==", + "dev": true + }, + "node_modules/@types/prettier": { + "version": "1.19.1", + "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-1.19.1.tgz", + "integrity": "sha512-5qOlnZscTn4xxM5MeGXAMOsIOIKIbh9e85zJWfBRVPlRMEVawzoPhINYbRGkBZCI8LxvBe7tJCdWiarA99OZfQ==", + "dev": true + }, "node_modules/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -311,6 +324,12 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", "dev": true }, + "node_modules/dataloader": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/dataloader/-/dataloader-1.4.0.tgz", + "integrity": "sha512-68s5jYdlvasItOJnCuI2Q9s4q98g0pCyL3HrcKJu8KNugUl8ahgmZYg38ysLTgQjjXX3H8CJLkAvWrclWfcalw==", + "dev": true + }, "node_modules/debug": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", @@ -531,6 +550,12 @@ "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", "dev": true }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "node_modules/lodash.camelcase": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", @@ -700,6 +725,15 @@ "node": ">=0.10.0" } }, + "node_modules/object-hash": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-1.3.1.tgz", + "integrity": "sha512-OSuu/pU4ENM9kmREg0BdNrUDIl1heYa4mBZacJc+vVWz4GtAwu7jO8s4AIt2aGRUTqxykpWzI3Oqnsm13tTMDA==", + "dev": true, + "engines": { + "node": ">= 0.10.0" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -718,6 +752,18 @@ "node": ">=0.10.0" } }, + "node_modules/prettier": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz", + "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -883,6 +929,44 @@ "node": ">= 10" } }, + "node_modules/ts-poet": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/ts-poet/-/ts-poet-4.5.0.tgz", + "integrity": "sha512-Vs2Zsiz3zf5qdFulFTIEpaLdgWeHXKh+4pv+ycVqEh+ZuUOVGrN0i9lbxVx7DB1FBogExytz3OuaBMJfWffpSQ==", + "dev": true, + "dependencies": { + "@types/prettier": "^1.19.0", + "lodash": "^4.17.15", + "prettier": "^2.0.2" + } + }, + "node_modules/ts-proto": { + "version": "1.82.2", + "resolved": "https://registry.npmjs.org/ts-proto/-/ts-proto-1.82.2.tgz", + "integrity": "sha512-Hceptz4oCnUTeGYbGepNNDg7drYOdv0M+m3LSv8V5hehsilsx5Ca8bhUAYGcQad+CDvbeUDM6PrzW4GGfJXi9A==", + "dev": true, + "dependencies": { + "@types/object-hash": "^1.3.0", + "dataloader": "^1.4.0", + "object-hash": "^1.3.1", + "protobufjs": "^6.8.8", + "ts-poet": "^4.5.0", + "ts-proto-descriptors": "^1.2.1" + }, + "bin": { + "protoc-gen-ts_proto": "protoc-gen-ts_proto" + } + }, + "node_modules/ts-proto-descriptors": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/ts-proto-descriptors/-/ts-proto-descriptors-1.3.1.tgz", + "integrity": "sha512-Cybb3fqceMwA6JzHdC32dIo8eVGVmXrM6TWhdk1XQVVHT/6OQqk0ioyX1dIdu3rCIBhRmWUhUE4HsyK+olmgMw==", + "dev": true, + "dependencies": { + "long": "^4.0.0", + "protobufjs": "^6.8.8" + } + }, "node_modules/uglify-js": { "version": "3.13.10", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.13.10.tgz", @@ -1178,6 +1262,18 @@ "integrity": "sha512-jJs9ErFLP403I+hMLGnqDRWT0RYKSvArxuBVh2veudHV7ifEC1WAmjJADacZ7mRbA2nWgHtn8xyECMAot0SkAw==", "dev": true }, + "@types/object-hash": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-1.3.4.tgz", + "integrity": "sha512-xFdpkAkikBgqBdG9vIlsqffDV8GpvnPEzs0IUtr1v3BEB97ijsFQ4RXVbUZwjFThhB4MDSTUfvmxUD5PGx0wXA==", + "dev": true + }, + "@types/prettier": { + "version": "1.19.1", + "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-1.19.1.tgz", + "integrity": "sha512-5qOlnZscTn4xxM5MeGXAMOsIOIKIbh9e85zJWfBRVPlRMEVawzoPhINYbRGkBZCI8LxvBe7tJCdWiarA99OZfQ==", + "dev": true + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -1330,6 +1426,12 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", "dev": true }, + "dataloader": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/dataloader/-/dataloader-1.4.0.tgz", + "integrity": "sha512-68s5jYdlvasItOJnCuI2Q9s4q98g0pCyL3HrcKJu8KNugUl8ahgmZYg38ysLTgQjjXX3H8CJLkAvWrclWfcalw==", + "dev": true + }, "debug": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", @@ -1499,6 +1601,12 @@ "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", "dev": true }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "lodash.camelcase": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", @@ -1628,6 +1736,12 @@ "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", "dev": true }, + "object-hash": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-1.3.1.tgz", + "integrity": "sha512-OSuu/pU4ENM9kmREg0BdNrUDIl1heYa4mBZacJc+vVWz4GtAwu7jO8s4AIt2aGRUTqxykpWzI3Oqnsm13tTMDA==", + "dev": true + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1643,6 +1757,12 @@ "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, + "prettier": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz", + "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==", + "dev": true + }, "process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -1776,6 +1896,41 @@ "yallist": "^4.0.0" } }, + "ts-poet": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/ts-poet/-/ts-poet-4.5.0.tgz", + "integrity": "sha512-Vs2Zsiz3zf5qdFulFTIEpaLdgWeHXKh+4pv+ycVqEh+ZuUOVGrN0i9lbxVx7DB1FBogExytz3OuaBMJfWffpSQ==", + "dev": true, + "requires": { + "@types/prettier": "^1.19.0", + "lodash": "^4.17.15", + "prettier": "^2.0.2" + } + }, + "ts-proto": { + "version": "1.82.2", + "resolved": "https://registry.npmjs.org/ts-proto/-/ts-proto-1.82.2.tgz", + "integrity": "sha512-Hceptz4oCnUTeGYbGepNNDg7drYOdv0M+m3LSv8V5hehsilsx5Ca8bhUAYGcQad+CDvbeUDM6PrzW4GGfJXi9A==", + "dev": true, + "requires": { + "@types/object-hash": "^1.3.0", + "dataloader": "^1.4.0", + "object-hash": "^1.3.1", + "protobufjs": "^6.8.8", + "ts-poet": "^4.5.0", + "ts-proto-descriptors": "^1.2.1" + } + }, + "ts-proto-descriptors": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/ts-proto-descriptors/-/ts-proto-descriptors-1.3.1.tgz", + "integrity": "sha512-Cybb3fqceMwA6JzHdC32dIo8eVGVmXrM6TWhdk1XQVVHT/6OQqk0ioyX1dIdu3rCIBhRmWUhUE4HsyK+olmgMw==", + "dev": true, + "requires": { + "long": "^4.0.0", + "protobufjs": "^6.8.8" + } + }, "uglify-js": { "version": "3.13.10", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.13.10.tgz", diff --git a/grpc/package.json b/grpc/package.json index b1c205e..6e0cbde 100644 --- a/grpc/package.json +++ b/grpc/package.json @@ -16,6 +16,7 @@ "devDependencies": { "@grpc/proto-loader": "^0.6.4", "grpc_tools_node_protoc_ts": "^5.3.0", - "grpc-tools": "^1.11.2" + "grpc-tools": "^1.11.2", + "ts-proto": "^1.82.2" } } diff --git a/grpc/tasks.py b/grpc/tasks.py index 4e8fccc..69a1c4b 100644 --- a/grpc/tasks.py +++ b/grpc/tasks.py @@ -1,3 +1,5 @@ +import os +import platform from pathlib import Path from invoke import task @@ -28,14 +30,25 @@ def gen(c, target="types"): """ Generate gRPC client code. """ - outDir = "gen/" + target - paths = Path(PB_ROOT).glob('**/*.proto') - for path in paths: - # because path is object not string - pathStr = str(path) - print("Generating for", pathStr) - cmd = Path(c.run("npm bin").stdout.rstrip()).joinpath("proto-loader-gen-types") - c.run( - "{} --includeComments --grpcLib @grpc/grpc-js -I pb -O {} {}" - .format(cmd, outDir, pathStr) - ) + out_dir = "gen/" + target + if not os.path.exists(out_dir): + os.mkdir(out_dir) + npmbin_path = Path(c.run("npm bin").stdout.rstrip()) + + if target == "ts": + plugin_path = npmbin_path / ("protoc-gen-ts_proto" + (".cmd" if platform.system() == "Windows" else "")) + cmd = "protoc --plugin=protoc-gen-ts_proto={} --ts_proto_opt=outputServices=grpc-js --ts_proto_out={} -I pb {}".format( + plugin_path, out_dir, "./pb/bdware/bdledger/api/*.proto") + print(cmd) + c.run(cmd) + + elif target == "types": + paths = Path(PB_ROOT).glob('**/*.proto') + for path in paths: + # because path is object not string + path_str = str(path) + print("Generating for", path_str) + cmd = "npx proto-loader-gen-types --includeComments --grpcLib @grpc/grpc-js -I pb -O {} {}".format( + out_dir, path_str) + print(cmd) + c.run(cmd)